Class StreamingJsonReader

java.lang.Object
com.tccc.kos.commons.util.json.StreamingJsonReader

public class StreamingJsonReader extends Object
This streaming JSON reader allows callbacks to be attached to logical paths of data in a JSON payload, which means that data can be extracted while the JSON is being parsed.

This is particularly useful when extracting a small set of data from a large JSON structure. For example, when pulling BrandSet data out of a large JSON payload that is primarily intended to be consumed by the UI code. This avoids the need to parse the entire payload into memory and then crawl the graph to extract the data.

Example #1

  [
    {
      "beverageId": 123,
      "name": {
        "full": "Coke",
        "abbr": "C"
      }
    },
    {
      "beverageId": 456,
      "name": {
        "full": "Diet Coke",
        "abbr": "DC"
      }
    }
  ]
 
 public void init() {

  // Generates a callback every time an object in the top level list is opened:
  addCallback("[{", jsonParser -> beverage = new Beverage());

  // Generates a callback every time an object in the top level list is closed:
  addCallback("[}", jsonParser -> addBeverage());

  // Adds the "beverageId" to the current Beverage object:
  addValueCallback("[{beverageId", jsonParser -> beverage.setId(jsonParser.getValueAsInt()));

  // Retrieves the "name" components:
  addValueCallback("[{name{full", jsonParser -> fullName = jsonParser.getValueAsString());
  addValueCallback("[{name{abbr", jsonParser -> abbrName = jsonParser.getValueAsString());
 

Example #2

 {
   elements: [
     {
       rules: [
         {
           sourceIngredientRefId: 1,
           targetIngredientRefId: 2,
           . . .
 
Use:
 "{elements[{rules[{sourceIngredientRefId"
 "{elements[{rules[{targetIngredientRefId"
to access the values.
Since:
1.0
Version:
2019-09-19
  • Field Details

  • Constructor Details

    • StreamingJsonReader

      public StreamingJsonReader()
  • Method Details

    • addCallback

      public void addCallback(String path, StreamingJsonReaderCallback callback)
      Add a new callback to the specified json path. This will be called every time the parser finds a token that matches the path. The json parser will be pointing at the matching token.
      Parameters:
      path - the json path to match
      callback - the callback to perform
    • addValueCallback

      public void addValueCallback(String path, StreamingJsonReaderCallback callback)
      Add a new callback to the specified json path. This will be called every time the parser finds a token that matches the path. The json parser will be pointing to the value token after the matching token. Do not use this for paths that don't reference a value as it can cause tokens to be skipped.
      Parameters:
      path - the json path to match
      callback - the callback to perform
    • read

      public void read(File file) throws IOException
      Read the specified json file.
      Parameters:
      file - the json file to read
      Throws:
      IOException - if there is an error
    • read

      public void read(InputStream stream) throws IOException
      Read the specified json inputStream.
      Parameters:
      stream - the input stream
      Throws:
      IOException - if there is an error