Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non blocking JSON parser #2106

Merged
merged 5 commits into from
Aug 31, 2017
Merged

Non blocking JSON parser #2106

merged 5 commits into from
Aug 31, 2017

Conversation

vietj
Copy link
Member

@vietj vietj commented Aug 29, 2017

Provide an event driven and non blocking JSON parser that can also be used as a read stream. The parser can emit fine grained events for JSON structures or emit them as values in a dynamic fashio.

@EmadAlblueshi
Copy link
Contributor

+1

Copy link
Contributor

@tsegismont tsegismont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments/concerns. Otherwise, great work!

@@ -117,10 +117,12 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we update the BOM (vertx-dependencies) rather than this POM?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it will be, before merging that PR, I'll update the vertx-dependencies, for now it's hardcoded here.

And it's not a BOM :-)

</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, BOM vs POM

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a BOM again :-)

== Json Parser

You can easily parse JSON structures but that requires to provide the JSON string at once, but it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: JSON content instead of JSON string. It's not always a string, it can be a buffer as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the buffer is encoded as a JSON string though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, I see what you mean :-)

----

The parser is non-blocking and emitted events are driven by the input buffers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: missing final dot.

// start object event
// "firstName":"Bob" event
parser.handle(Buffer.buffer("[{\"firstName\":\"Bob\"},"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the end parenthesis shouldn't be there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's valid java code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the Java code compiles. I was wondering if the parenthesis inside the JSON content would not trigger an end object event. Am I wrong?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see parenthesis in JSON content

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean curly brace ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha. Yeah, I meant curly brace. Sorry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the content was wrong, I think I fixed it now

----

Event driven parsing provides more control but comes at the price of flexibility. The parser allows you
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why at the price of flexibility? If the parser gives your more control, doesn't this make it more flexible?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to convey that if you deal with fine grained events, it becomes harder to parse because you have to care about lot of individual events which can be inconvenient, I will try to say that differently.

switch (event.type()) {
case START_OBJECT:
// Set object value mode to handle each entry, from now on the parser won't emit start object events
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, when object value mode is enabled, all nested objects/arrays are parsed at once?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just reviewed the parser impl, it seems to be the case. Could you add tests for nested objects/arrays?

* @throws java.lang.ClassCastException if the value is not a String
* @throws java.time.format.DateTimeParseException if the String value is not a legal ISO 8601 encoded value
*/
Instant instantValue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought Instant didn't work with codegen

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I'm going to add "@GenIgnore", I was testing using the IDE that does not run the annotation processor but in reality it fails :-)

START_ARRAY,

/**
* Signals the end of a JSON object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object => array

* @return a reference to this, so the API can be used fluently
*/
@Fluent
JsonParser arrayValueMode();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add doc/example for this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@vietj
Copy link
Member Author

vietj commented Aug 31, 2017

PTAL

Event driven parsing provides more control but comes at the price of flexibility. The parser allows you
to handle JSON structures as values when it is desired:
Event driven parsing provides more control but comes at the price of dealing with fine grained events, which can be
inconvenient sometimes. The JSON parser allows you to handle JSON structures as values when it is desired:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -103,17 +103,17 @@ JsonParser parser = JsonParser.newParser();
// start array event
// start object event
// "firstName":"Bob" event
parser.handle(Buffer.buffer("[{\"firstName\":\"Bob\"},"));
parser.handle(Buffer.buffer("[{\"firstName\":\"Bob\","));
Copy link
Contributor

@tsegismont tsegismont Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@tsegismont
Copy link
Contributor

LGTM. I can't merge (permissions issue we talk about yesterday) but anyway you have to update the BOM vertx-dependencies module first ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants