Java json streaming parser

Gson JsonReader

Learn to work with Gson JsonReader class which is a pull-based streaming JSON parser. It helps in reading a JSON as a stream of tokens.

  • The JsonReader is the streaming JSON parser and an example of pull parser. A push parser parses through the JSON tokens and pushes them into an event handler.
  • It helps read a JSON (RFC 7159) encoded value as a stream of tokens.
  • It reads both literal values (strings, numbers, booleans, and nulls) as well as the begin and end delimiters of objects and arrays.
  • The tokens are traversed in depth-first order, the same order that they appear in the JSON document.

In streaming mode, every JSON data is considered an individual token. When we use JsonReader to process it, each token will be processed sequentially. For example,

While parsing with JsonReader, above JSON will generate 4 tokens:

We can create a JsonReader instance using its simple constructor which accepts java.io.Reader type input stream.

String json = "<>"; JsonReader jsonReader = new JsonReader( new StringReader(json) );

We can use one of the following readers based on the source of JSON stream:

  • BufferedReader
  • LineNumberReader
  • CharArrayReader
  • InputStreamReader
  • FileReader
  • FilterReader
  • PushbackReader
  • PipedReader
  • StringReader

After creating JsonReader wrapping a valid JSON source, we can start iterating over stream tokens and look into token values.

Читайте также:  Laport kdl ru laport index html

Following is an example of reading a simple JSON using JsonReader in the form of tokens.

import java.io.IOException; import java.io.StringReader; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; public class Main < public static void main(String[] args) throws Exception < String json = ""; JsonReader jsonReader = new JsonReader(new StringReader(json)); jsonReader.setLenient(true); try < while (jsonReader.hasNext()) < JsonToken nextToken = jsonReader.peek(); if (JsonToken.BEGIN_OBJECT.equals(nextToken)) < jsonReader.beginObject(); >else if (JsonToken.NAME.equals(nextToken)) < String name = jsonReader.nextName(); System.out.println("Token KEY >>>> " + name); > else if (JsonToken.STRING.equals(nextToken)) < String value = jsonReader.nextString(); System.out.println("Token Value >>>> " + value); > else if (JsonToken.NUMBER.equals(nextToken)) < long value = jsonReader.nextLong(); System.out.println("Token Value >>>> " + value); > else if (JsonToken.NULL.equals(nextToken)) < jsonReader.nextNull(); System.out.println("Token Value >>>> null"); > else if (JsonToken.END_OBJECT.equals(nextToken)) < jsonReader.endObject(); >> > catch (IOException e) < e.printStackTrace(); >finally < jsonReader.close(); >> > 
Token KEY >>>> id Token Value >>>> 1001 Token KEY >>>> firstName Token Value >>>> Lokesh Token KEY >>>> lastName Token Value >>>> Gupta Token KEY >>>> email Token Value >>>> null
  • The hasNext() method of the JsonReader returns true if it has more tokens.
  • The peek() method returns the next JSON token, but without moving over it.
  • Multiple calls to peek() will subsequently return the same JSON token.
  • The type of return token can be checked with constants of JsonToken class.
  • Array’s opening and closing brackets ‘[‘ and ‘]’ are checked with beginArray() and endArray() methods.
  • Object’s opening and closing brackets » are checked with beginObject() and endObject() methods.

The key of the token is of type JsonToken.NAME. Use nextName() method to get the key name.

  • After determining the type of token, get the value of token using method like nextLong() , nextString() , nextInt() etc.
  • Null literals can be consumed using either nextNull() or skipValue() .
  • All next. () method returns the value of the current token and moves the internal pointer to the next.
  • When an unknown name is encountered, strict parsers should fail with an exception. Lenient parsers should call skipValue() to recursively skip the value’s nested tokens, which may otherwise conflict.
Читайте также:  Php scripts in cgi bin

Drop me your questions related to Gson JsonReader.

Источник

Parse JSON to Java using Streaming Jackson Parser

Parse JSON to Java – Streaming Parser and Generator

parse JSON to Java Example

package com.studytrails.json.jackson; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; /** * The aim of this class is to get the list of albums from free music archive * (limit to 5) * */ public class StreamParser1 < public static void main(String[] args) throws MalformedURLException, IOException < // Get a list of albums from free music archive. limit the results to 5 String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=5"; // get an instance of the json parser from the json factory JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(new URL(url)); // continue parsing the token till the end of input is reached while (!parser.isClosed()) < // get the token JsonToken token = parser.nextToken(); // if its the last token then we are done if (token == null) break; // we want to look for a field that says dataset if (JsonToken.FIELD_NAME.equals(token) && "dataset".equals(parser.getCurrentName())) < // we are entering the datasets now. The first token should be // start of array token = parser.nextToken(); if (!JsonToken.START_ARRAY.equals(token)) < // bail out break; >// each element of the array is an album so the next token // should be < token = parser.nextToken(); if (!JsonToken.START_OBJECT.equals(token)) < break; >// we are now looking for a field that says "album_title". We // continue looking till we find all such fields. This is // probably not a best way to parse this json, but this will // suffice for this example. while (true) < token = parser.nextToken(); if (token == null) break; if (JsonToken.FIELD_NAME.equals(token) && "album_title".equals(parser.getCurrentName())) < token = parser.nextToken(); System.out.println(parser.getText()); >> > > > >

JSON Generation Example

package com.studytrails.json.jackson; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; /** * * In This example we look at generating a json using the jsongenerator. we will * be creating a json similar to * http://freemusicarchive.org/api/get/albums.json? * api_key=60BLHNQCAOUFPIBZ&limit=1, but use only a couple of fields * */ public class StreamGenerator1 < public static void main(String[] args) throws IOException < JsonFactory factory = new JsonFactory(); JsonGenerator generator = factory.createGenerator(new FileWriter(new File("albums.json"))); // start writing with < generator.writeStartObject(); generator.writeFieldName("title"); generator.writeString("Free Music Archive - Albums"); generator.writeFieldName("dataset"); // start an array generator.writeStartArray(); generator.writeStartObject(); generator.writeStringField("album_title", "A.B.A.Y.A.M"); generator.writeEndObject(); generator.writeEndArray(); generator.writeEndObject(); generator.close(); >>

Источник

Jackson Streaming API Example | Read and Write JSON

Jackson reads and writes JSON through a high-performance Jackson Streaming API, with a low memory and process overhead. The only problem with Streaming API is that we need to take care of all the tokens while parsing JSON data. All the JSON values must be read/write in the same order in which it arrives.

Let’s take an example if we have a JSON string as

then we will be getting the tokens in the below order.

Due to this complexity in nature, Jackson Streaming API is generally used in frameworks internally.

Folder Structure:

    1. Create a new JavaProjectJacksonJSONTutorial” and create a package for our src files com.javainterviewpoint
    2. Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.

    if you are running on maven add the below dependency to your pom.xml

     org.codehaus.jackson jackson-core-asl 1.9.13  org.codehaus.jackson jackson-mapper-asl 1.9.13  
    1. Create the Java classes Jackson_Streaming_Read_Example.javaandJackson_Streaming_Write_Example.java under com.javainterviewpointfolder.
    • Jackson 2 JSON Parser – Convert JSON to/from Java Object
    • How to Convert JSON to / from Java Map using JACKSON
    • Jackson Tree Model Example – JsonNode
    • Jackson Streaming API Example | Read and Write JSON
    • Jackson JSON Example | ObjectMapper and @JSONView
    • How to Parse JSON to/from Java Object using Boon JSON Parser
    • How to Read and Write JSON using GSON
    • How to write JSON object to File in Java?
    • How to read JSON file in Java – JSONObject and JSONArray
    • Jersey Jackson JSON Tutorial
    • Spring REST Hello World Example – JSON and XML responses
    • How to Convert Java Object to JSON using JAXB
    • JAX-RS REST @Consumes both XML and JSON Example
    • JAX-RS REST @Produces both XML and JSON Example

    Jackson Streaming API Example

    In this tutorial, we will learn how to use following Jackson streaming API to read and write JSON data.

    1. JsonGenerator – Write Java String to JSON
    2. JsonParser – Parse JSON to Java.

    Convert Java to JSON Using Jackson JsonParser

    package com.javainterviewpoint; import java.io.File; import java.io.IOException; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonToken; public class Jackson_Streaming_Read_Example < public static void main(String args[]) < try < //Create a JsonFactory JsonFactory factory = new JsonFactory(); //JsonParser for reading the JSON File JsonParser parser = factory. createJsonParser(new File("C:\\user.json")); //Pointing to the first token parser.nextToken(); while(parser.nextToken()!= JsonToken.END_OBJECT) < //Getting the token name String key = parser.getCurrentName(); if(key.equals("Name")) < /**Here current token is "Name" * nextToken() will point to the next token, * which will be Name's value */ parser.nextToken(); System.out.println(key+" : "+parser.getText()); >if(key.equals("Age")) < parser.nextToken(); System.out.println(key+" : "+parser.getText()); >if(key.equals("Countries")) < parser.nextToken(); System.out.println(key); while(parser.nextToken()!=JsonToken.END_ARRAY) < System.out.println("\t"+parser.getText()); >> > > catch (JsonParseException e) < e.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >> >
    JsonFactory factory = new JsonFactory();
    • The createJsonParser() method of the factory returns the JsonParser object, we need to pass the input file which you want to read. Here it is “User.json”.
    JsonParser parser = factory. createJsonParser(new File("C:\\user.json"));
    • nextToken() method will point to the next token, we need to loop it till END_OBJECT (“>”).
    • getCurrentName() method will return you the token name, check it with each field of the JSON
    String key = parser.getCurrentName(); if(key.equals("Name"))
    System.out.println(key+" : "+parser.getText());

    Jackson Streaming API Example

    Convert JSON to Java object using Jackson JsonGenerator

    package com.javainterviewpoint; import java.io.File; import java.io.IOException; import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParseException; public class Jackson_Streaming_Write_Example < public static void main(String args[]) < try < //Create a JsonFactory JsonFactory factory = new JsonFactory(); //JsonGenerator for writing the JSON File JsonGenerator generator = factory. createJsonGenerator(new File("C:\\user.json"), JsonEncoding.UTF8); //Writing a JSON opening brace < generator.writeStartObject(); //Writing String key and String value - "Name":"JIP" generator.writeStringField("Name","JIP"); //Writing String key and Integer value - "Age":111 generator.writeNumberField("Age",111); generator.writeFieldName("FavouriteSports"); //Writing a JSON array opening brace [ generator.writeStartArray(); //Writing array value generator.writeString("Football"); generator.writeString("Cricket"); generator.writeString("Chess"); generator.writeString("Baseball"); //Writing a JSON array closing brace ] generator.writeEndArray(); //Writing a JSON closing brace >generator.writeEndObject(); generator.close(); > catch (JsonParseException e) < e.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >> >
    • Create a JsonFactory, from which we will be getting JsonGenerator
    JsonFactory factory = new JsonFactory();
    • The createJsonGenerator() method of the factory returns the JsonGenertor object, we need to pass the input file which you want to read and encoding.
    JsonGenerator generator = factory. createJsonGenerator(new File("C:\\user.json"), JsonEncoding.UTF8);
    • Now let us start writing the JSON using JsonGenerator, we need to write each and every token
      1. generator.writeStartObject() –> Write the starting brace
      2. generator.writeStringField(“Name”,”JIP”) –> Write String key(Name) and String value(JIP)
      3. generator.writeNumberField(“Age”,111) –> Writing String key(Age) and Integer value(111)
      4. generator.writeFieldName(“FavouriteSports”) –> Writing String key alone(FavouriteSports)
      5. generator.writeStartArray() –> Writing open brace of the array “[“
      6. generator.writeString(“Football”) –> Writing the values of the array (Football, Cricket,Chess,Baseball).
      7. generator.writeEndArray() –> Writing the end brace of the array “]”
      8. generator.writeEndObject() –> Write the end brace “>”
    • Finally close the generator Object using the close() method

    Output :
    In the user.json file we will be having the below content

    Источник

Оцените статью