Package com.tccc.kos.commons.util.json
Class StreamingJsonReader
java.lang.Object
com.tccc.kos.commons.util.json.StreamingJsonReader
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCallback
(String path, StreamingJsonReaderCallback callback) Add a new callback to the specified json path.void
addValueCallback
(String path, StreamingJsonReaderCallback callback) Add a new callback to the specified json path.void
Read the specified json file.void
read
(InputStream stream) Read the specified json inputStream.
-
Field Details
-
WILDCARD
- See Also:
-
-
Constructor Details
-
StreamingJsonReader
public StreamingJsonReader()
-
-
Method Details
-
addCallback
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 matchcallback
- the callback to perform
-
addValueCallback
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 matchcallback
- the callback to perform
-
read
Read the specified json file.- Parameters:
file
- the json file to read- Throws:
IOException
- if there is an error
-
read
Read the specified json inputStream.- Parameters:
stream
- the input stream- Throws:
IOException
- if there is an error
-