Thursday, 23 February 2012

stream a single value as JSON in a whole JSON map

Problem statement 
I have a JSON of following format
{ "a":"b" , "Content" : <Content of file FILEA> , "x" : y" }
and so on.
FILEA is too big that i cant open and load it to main memory. Is there any option where i can stream this json to a webservice without using much of main memory using Java.

So what i did was , i flicked a java file from gson , added features in it and then used it.
Code -

usage -
    writer = new JsonStreamWriter(new   OutputStreamWriter(urlconnection.getOutputStream()));

    writer.beginObject(); // {"name").value("mkyong"); // "name" : "mkyong""age").value(29); // "age" : 29"messages"); // "messages" :
    writer.streamValue("Stream1"); // "msg 1"
    writer.streamValue("Stream2"); // Keep on writing lines here from a file
    writer.endObject(); // }

No comments:

Post a Comment