DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. A Fast, Flexible JSON Library for Java

A Fast, Flexible JSON Library for Java

There's a new JSON library on the block that promises high performance with tricks like dynamic class shadowing and quick data binding.

Tao Wen user avatar by
Tao Wen
·
Jan. 05, 17 · News
Like (35)
Save
Tweet
Share
55.75K Views

Join the DZone community and get the full member experience.

Join For Free

Don't we already have objectMapper.readValue? Why another JSON library? I found myself stuck in a couple of situations, and jsoniter (json-iterator) really worked for me:

  • Working with PHP: You want to int, so they might give you 100 or "100". You want an object, so they might give you [] as an empty object.

  • Parsing large JSON streams: Parsing a large stream of JSON, then extracting only what you need from the jungle.

  • Cannot bind: The JSON is organized in a key/value form, and it cannot bind directly to my object model.

Also, jsoniter is much much faster than existing libraries (jackson, gson, you name it). Third party benchmarking is welcome. Here are the shameless self-benchmarking results for data binding 1kb of JSON:

Image title

The advantages of jsoniter come from these innovations:

  • Any type: Capture raw bytes as the Any type. Parsing is done lazily. Any can be used like a PHP array or JavaScript Object and be weakly typed.

  • Iterator abstraction: Take the JSON input stream as an iterator like object. You can walk through the graph in a streaming way, just like iterating collections. It's similar to the gson API, but greatly simplified.

  • Trie-tree: The biggest drawback (and maybe its biggest benefit) of JSON is the string typed field name. It is time-consuming to bind object fields by comparing strings. Jsoniter uses tri-tree to boost the performance.

  • Code generation: All decoder/encoder logic can be code generated. You have plenty of options available, such as reflection/dynamic codegen/static codgen. 

  • Only pay for the feature you want: Taking InputStream as an input is slower than byte[]. Traditional parsers use a virtual method or feature flags to generalize, which is a performance killer. Jsoniter uses dynamic class shadowing to switch implementations.

  • Required field validation: When you parse an object of int field. You can not tell the field is zero because no input from JSON or the field is indeeded specified as zero. Jsoniter implemented required field tracking using bit mask, now you can know.

A lot of work has been done to make sure jsoniter is the fastest out there. Benchmarking aside, what most people truly want is to get their job done fast. Here is an example to show you how flexible the API is:

[1024, {"product_id": 100, "start": "beijing"}]
["1025", {"product_id": 101, "start": "shanghai"}]
// many many more lines


Each line is an object. The first element is the order ID, and the second element is the order details. Notice:

  • There are many lines, and reading them all in once will lead to memory issues.

  • Some order IDs are ints and some are strings. This is very common when working with PHP.

  • The order details have many fields and need object binding.

In 6 lines, we have solved all the problems.

JsonIterator iter = JsonIterator.parse(input); // input stream
OrderDetails orderDetails = new OrderDetails(); // reused
while(iter.whatIsNext() != ValueType.INVALID) {
    Any order = iter.readAny(); // lazy
    int orderId = order.toInt(0); // weakly typed
    String start = order.get(1).bindTo(orderDetails).start; // data binding
}
  • JsonIterator.parse takes InputStream as the input and parses everything in a stream.

  • ReadAny returns an instance of Any. The parsing is lazily done when actually getting the field, which makes it simple and performant.

  • BindTo(orderDetails): Data binding can reuse existing objects

This example is just a demo of the flexibility. It might seems overly complex, it will be handy when you need it though. For everyday use, just remember two lines:

JsonIterator.deserialize("[1,2,3]"); // JSON => object
JsonStream.serialize(new int[]{1,2,3}) // object => JSON

I hope you are interested. This library is new, so bug reports or pull requests should be submitted to https://github.com/json-iterator/java. The Golang version will be available soon.

JSON Library Object (computer science) Data binding Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why the World Is Moving Towards Serverless Computing
  • When Scrum Feels Like Dressing for Dinner
  • Pair Testing in Software Development
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: