Json Streaming

Json Streaming / JsonPath Matcher

License

License

Categories

Categories

JSON Data
GroupId

GroupId

com.github.nithril
ArtifactId

ArtifactId

json-stream
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Json Streaming
Json Streaming / JsonPath Matcher
Project URL

Project URL

https://github.com/nithril/xml-stream-css
Source Code Management

Source Code Management

https://github.com/nithril/json-stream.git

Download json-stream

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.nithril/json-stream/ -->
<dependency>
    <groupId>com.github.nithril</groupId>
    <artifactId>json-stream</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.nithril/json-stream/
implementation 'com.github.nithril:json-stream:1.0.1'
// https://jarcasting.com/artifacts/com.github.nithril/json-stream/
implementation ("com.github.nithril:json-stream:1.0.1")
'com.github.nithril:json-stream:jar:1.0.1'
<dependency org="com.github.nithril" name="json-stream" rev="1.0.1">
  <artifact name="json-stream" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.nithril', module='json-stream', version='1.0.1')
)
libraryDependencies += "com.github.nithril" % "json-stream" % "1.0.1"
[com.github.nithril/json-stream "1.0.1"]

Dependencies

compile (6)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-core jar 2.9.7
com.fasterxml.jackson.core : jackson-databind jar 2.9.7
com.fasterxml.jackson.core : jackson-annotations jar 2.9.7
org.apache.commons : commons-lang3 jar 3.3.2
org.jooq : jool jar 0.9.10
org.parboiled : parboiled-java jar 1.1.7

test (3)

Group / Artifact Type Version
commons-io : commons-io jar 2.4
junit : junit jar 4.11
com.google.guava : guava jar 19.0

Project Modules

There are no modules declared in this project.

Json Streaming / JsonPath Matcher

Streaming Json is fast but at the cost of lack of context (previous node / attribute...) and matcher. Matching a json with a path barely more complex than parent/child is cumbersome and imply to implement context saving and matching.

Thanks to JsonPath, a string expression will help to parse the json.

Field matcher

The following code:

try(InputStream is = new FileInputStream("foo.json")){
    JsonFactory factory = new JsonFactory();
    JsonParser parser = factory.createParser(is);
    while(parser.nextToken() != null){
        if (JsonToken.FIELD_NAME == parser.getCurrentToken() && "foo".equals(parser.getText())){
            //Do something
        }
    }
}        

Can be replaced with a more friendly lambda stream based approach:

JsonStreams.stream("src/test/resources/foo.json")
        .field("foo")
        .forEach(c -> {
            //Do something                
        });

JsonPath matcher

Things can get tough when the path is complex

Pseudo java code:

try(InputStream is = new FileInputStream("foo.json")){
    JsonFactory factory = new JsonFactory();
    JsonParser parser = factory.createParser(is);
    // Pseudo class to handle a json path 
    JsonPath jsonPath = new JsonPath();
    while(parser.nextToken() != null) {
        jsonPath.manageToken(parser.getCurrentToken());
        if ("$.foo.bar".equals(jsonPath.toString())) {
            //Do something
        }


    }
}   

The JsonPath matcher allows to keep the code clean and focused:

JsonStreams.stream("foo.xml")
        .jsonPath("$.foo.bar")
        .forEach(c -> {
            //Do something
        });

Predicates

All matchers are Java 8 Predicate that can be combined.

JsonStreams.consumer("foo.xml"))
        .match(jsonPath("@.book").or(jsonPath("@.movie")), c -> {})
        .consume();

See the Predicates helper for the list of supported predicates.

Nested Consumer and Streamer

Consumer and Streamer can be nested.

In the following example, a first stream match all the wikipedia page tag. Starting from this tag, a nester consumer extract the title, id, timestamp and contributor name:

    try (InputStream fis = new FileInputStream("src/test/resources/wikipedia.json");
        JsonStreams.stream("wikipedia.json")
            .jsonPath("$.page")
            .map(context -> {
                Page page = new Page();
                context.partialConsumer()
                        .matchJsonPath("@.title", c -> page.title = c.getText())
                        .matchJsonPath("@.id", c -> page.id = c.getText())
                        .matchJsonPath("@.revision.timestamp", c -> page.lastRevision = c.getText())
                        .matchJsonPath("@.revision.contributor.username", c -> page.lastContributor = c.getText())
                        .consume();
                return page;
            })
            .forEach(p -> p.toString())
         

Supported JsonPath

TBD

Versions

Version
1.0.1
1.0.0