I wanted to share my question/answer with using the new JSR-353 which is the Java API for JSON Processing. Specifically you can manipulate JSON data in 2 different "API" the Streaming and the Object API.
If you type in "jsr-353 tutorial" into google you will get many results.
https://jcp.org/en/jsr/detail?id=353 ~ the details on the specific request.
https://jsonp.java.net/ ~ main site for the API, also links you to the Oracle Tutorial under "documentation" located here https://docs.oracle.com/javaee/7/tutorial/doc/jsonp.htm I will talk more about this tutorial later.
and finally this one
http://www.javabeat.net/java-json-api-jsr-353/
I want to talk about the last link first, as it's one which gave a lot of good detail for me to start, and one of the only real tutorials out there (there are others, but they are basically the same).
What I really tried to learn about the API from being new to not only this API but JSON in general is.
When To Use Streaming API and Object Model API?
If you want the JSON data to be written to a character stream like a file or to a byte stream then the Streaming API will be the best choice as it directly perform the writing operation into the buffer without constructing the object tree in the memory i.e there is not intermediate form generated before the final JSON data is created.
If you want to hold the JSON data as an object tree in the memory i.e not write to any stream but store the tree in the memory so that one can reuse the JSON data without need to reparse it or one can also serialize the object tree to persist/save the JSON data. Here the JSON data will be represented in the form of an object tree.
Now the Streaming API description made a lot of sense to me and I needed to save a file so this made sense to me.
As for the Object API it also made sense what it was doing, save the object so I can reuse it later in my code, awesome.
The problem is that I didn't get an answer to my question, which I will explain what exactly I am looking for now.
My Question is:
I basically have 1 object that contains other objects/arrays.
Originally I was using a BufferedWriter to writer the data to a new line into a text file.
my format looks something like this.
bw.write(1);
bw.newLine();
bw.write(2);
bw.newLine();
for(int i = 0; i < 4; i++)
{
bw.write(i);
bw.newLine();
}
bw.write(2);
bw.newLine();
for(int j = 0; j < 2; j++)
{
bw.write(j);
bw.newLine();
bw.write(j+5);
bw.newLine();
bw.write(2);
bw.newLine();
bw.write(j*4);
bw.newLine();
}
bw.write(12);
bw.newLine();
for(int k = 0; k < 82; k++)
{
bw.write(k);
bw.newLine();
bw.write(k*5);
bw.newLine();
//do some additional code here
bw.write(2);
bw.newLine();
bw.write(k*4);
bw.newLine();
}
then finish up. Granted the numbers and such are placeholders and in reality everything from the write data, to the loop amounts are variable data I read from another file.
As you can see I cannot use the traditional "method-chaining" that comes with JSR-353.
As for what method chaining is, take a look at wiki http://en.wikipedia.org/wiki/Method_chaining. An example of Method Chaining using the Streaming API would be this, as shown in the above tutorial:
FileWriter writer = new FileWriter("c:\\example.txt");
JsonGenerator gen = Json.createGenerator(writer);
gen.writeStartObject()
.write("firstName", "Duke")
.write("lastName", "Java")
.write("age", 18)
.write("street/Address", "100 Internet Dr")
.write("city", "JavaTown")
.write("state", "JA")
.write("postalCode", "12345")
.writeStartArray("phoneNumbers")
.writeStartObject()
.write("type", "mobile")
.write("number", "111-111-1111")
.writeEnd()
.writeStartObject()
.write("type", "home")
.write("number", "222-222-2222")
.writeEnd()
.writeEnd()
.writeEnd();
gen.close();
I also looked at the Oracle Tutorial which confused me a bit where I saw "Generating/Parsing" as I was looking for a way to save files.
https://docs.oracle.com/javaee/7/tutorial/doc/jsonp001.htm
19.1.3 Generating and Parsing JSON Data
For generating and parsing JSON data, there are two programming models, which are similar to those used for XML documents.
The streaming model uses an event-based parser that reads JSON data one element at a time. The parser generates events and stops for processing when an object or an array begins or ends, when it finds a key, or when it finds a value. Each element can be processed or discarded by the application code, and then the parser proceeds to the next event. This approach is adequate for local processing, in which the processing of an element does not require information from the rest of the data. The streaming model generates JSON output to a given stream by making a function call with one element at a time.
The tutorial mentions this but it was confusing exactly what this mean, especially me thinking this was for writing and not reading. When it mentioned the last line (in bold) it didn't make much sense why it would do it one at a time, and made it seem that it only dealt with part of the object, not the whole, as the Object API mentions dealing with the entire tree.
So instead of dealing with the Streaming API, I started with the Object API. I tried saving the file at first to a FileWriter, but nothing would be saved. Eventually I switched to a StringWriter and was using that in my project. I decided to switch back to the FileWriter after finishing up my structure and it somehow saved to the file, but I realize that part of my code was cut off at the end. I tried to do a tiny structure and it would print nothing.
gen.writeStartObject().write("firstName", "Duke")
can be rewritten asgen.writeSta...(); gen.write(..);
. Method chaining is a matter of taste. It isn't an architecture. Anything you can write with method chaining, you can write without. I don't understand what your goal or problem is. – Plangent