0

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.

For example , it would be awesome if there is something like writer = new JsonWriter(new FileWriter("user.json"));

writer.beginObject(); // {
writer.name("name");
writer.value("messagesPart1"); // "messages" : 
    writer.flush();
    writer.value("messagesPart2"); // "messages" :
    writer.flush(); 
writer.value("messagesPart3"); // "messages" : 
writer.endObject();
    }

And the content of user.json fie is

{ "name" : "messagesPart1messagesPart2messagesPart3"}

Vineeth Mohan
  • 18,633
  • 8
  • 63
  • 77
  • You are probably looking for something like http://stackoverflow.com/questions/444380/is-there-a-streaming-api-for-json post? – Pavan Feb 21 '12 at 09:22

2 Answers2

0

I recommend you to use the Jackson library (one of the most powerful for Json in Java).

It has the functionality to stream Json to OutpuStream (so you could have a web socket or an open connection to output the content...). Here is the high level doc: Generator.

There is also a use case (Twittter) that makes use of this generator, check this post

But the Jakson quick start has also a good introduction

Andy Petrella
  • 4,345
  • 26
  • 29
  • I had gone tru Jackson , but then its not clear how to stream just a value part of a key value pair. – Vineeth Mohan Feb 21 '12 at 10:36
  • I was hoping to see something like writer.name("Content").value(BufferedStream) – Vineeth Mohan Feb 21 '12 at 10:47
  • actually this has no sense since it's too huge to be hold in memory. My guess is that you must stream the whole object, though. – Andy Petrella Feb 21 '12 at 11:01
  • That is where i am stuck. Its possible to stream the whole object if the whole json string is written to a single file. but here just on value has to be entire json is present in file. – Vineeth Mohan Feb 21 '12 at 11:11
  • 1
    The preferable way would be to serialize the whole object not only the big property, because I don't expect Json instance handling such Stream property. Especially when it will be rendered in response (the memory will blow also). So what if you pipe directly the response with the json – Andy Petrella Feb 21 '12 at 11:16
0

This blog link have some usefull info on the same - http://architectvm.blogspot.in/2012/02/stream-single-value-as-json-in-whole.html

Vineeth Mohan
  • 18,633
  • 8
  • 63
  • 77