Class JsonStream

java.lang.Object
sk.antons.json.parse.JsonStream

public class JsonStream extends Object
Json parer, which traverse json tree and produces stream of json values according to given path matcher. Useful, when you have big json and you want to extract only small json sub tree at once. Imagine you have jsom like <pre> { "items" : [ {"name": "name-1", "value": 1}, {"name": "name-2", "value": 2}, {"name": "name-3", "value": 3}, {"name": "name-4", "value": 4}, ... ] } </pre> So you can parse whole json and iterate parts. In this case whole json is loaded before traversal. (It is effective for small jsons only) <pre> JsonValue root = JsonParser.parse(inputstream); root.find(SPM.path("items", "*")).stream() // or if you want traverse only values // root.find(SPM.path("items", "*", "value")).stream() </pre> This class allows you to read only parts you want. But it is little bit slower. <pre> JsonStream.instance(inputstream, SPM.path("items", "*")).stream(); // or if you want traverse only values // JsonStream.instance(inputstream, SPM.path("items", "*", "value")).stream(); </pre> So if you have pretty big json which is json array and you are gounig to process it item by item, you can use JsonStream with path "*".
Author:
antons
  • Constructor Details

    • JsonStream

      public JsonStream(JsonSource source, PathMatcher matcher)
      Instance of JsonSource
      Parameters:
      source - source for reading json
      matcher - path matcher to identify returned values
  • Method Details

    • instance

      public static JsonStream instance(JsonSource source, PathMatcher matcher)
      Instance of JsonStream.
      Parameters:
      source - source for reading json
      matcher - path matcher to identify returned values
      Returns:
      new instance
    • instance

      public static JsonStream instance(String json, PathMatcher matcher)
      Instance of JsonStream.
      Parameters:
      json - string with json
      matcher - path matcher to identify returned values
      Returns:
      new instance
    • instance

      public static JsonStream instance(Reader reader, PathMatcher matcher)
      Instance of JsonStream.
      Parameters:
      reader - source for reading json
      matcher - path matcher to identify returned values
      Returns:
      new instance
    • iterator

      public Iterator<JsonValue> iterator()
      Creates iterator for json subparts identifies by given matcher. After calling the method JsonStream instance is not valid anymore.
      Returns:
      iterator
    • stream

      public Stream<JsonValue> stream()
      Creates stream of json subparts identifies by given matcher. After calling the method JsonStream instance is not valid anymore.
      Returns:
      stream
    • main

      public static void main(String[] argv) throws Exception
      Throws:
      Exception