Moxy reading in a stream of a large JSON file object by object. [message #1740791] |
Thu, 18 August 2016 14:41 |
Michael Pawlowsky Messages: 1 Registered: August 2016 |
Junior Member |
|
|
I have a couple of questions I am unable to find the answer to:
1) When unmarshalling a document using JAXB/Moxy, will the whole document (Several GigaBytes) be placed in memory all at once or is there any type of lazy loading being done in the background?
2) Is there a method in Moxy to say what Object Array element in the JSON file I am interested in iterating over? And can it be done by reading in the stream 1 complete object at a time. It seems like such a logical way of doing it, that I can't imagine it is not possible, but I just can't seem to find it.
My concern here is these JSON files will become larger over time and I do not want to load it into memory all at one.
I started out using a JsonParser, that I can go through to get to my elements, but it seems somewhat of an un-elegant solution. I am wondering if there is a better way to accomplish this.
Current solution is something like:
try (FileInputStream json = new FileInputStream( "file.json" )) {
JsonParser jr = Json.createParser(json);
Event event = null;
// Advance to "details" key
while(jr.hasNext()) {
event = jr.next();
if( event == Event.KEY_NAME && "details".equals( jr.getString() )){
event = jr.next();
break;
}
}
And then I loop through until I hit the end of the array.
// process contents of "details" object
while( event != Event.END_ARRAY) {
switch(event) {
And build my object based on the values of the strings.
I can of course also unmarshall the whole file, but again, I do not want to use that much memory.
What I am looking for is a mix between the two.
Something where I can say what object array in the JSON I am interested in iterating over. And then have Moxy return the JSON object as a Java Object each iteration that I can then process in some manner.
Does this exists? If anyone can point me to the right documentation I would really appreciate it.
Thank you.
|
|
|
Powered by
FUDForum. Page generated in 0.04283 seconds