HomeServicesExpertiseOpen source supportAboutStoryReferencesCareerInsightsContact
Thursday, 21 December, 2017
Miikka Koskinen
Technology

Faster JSON processing with jsonista

Post cover image

We are happy to announce the initial release of our JSON encoding and decoding library for Clojure, jsonista 0.1.0. It’s fast and it has explicit configuration – no global mutable state!

Yet another JSON library?!Link to Yet another JSON library?!

Yes, you read that right: we’ve created yet another JSON library for Clojure. There are a couple of them, but probably the most popular one is Cheshire. It’s fast and full of features, so why did we create a new library?

A year ago, Tommi started to focus on the performance of the Clojure web stack and as a result, we got Muuntaja, a ground-up rewrite of ring-middleware-format. Muuntaja is a Ring middleware and Pedestal interceptor library for parsing and decoding data in HTTP requests and responses. It handles the content type negotiation, but it defers to libraries like Cheshire and transit-clj for the actual serialization and deserialization.

Muuntaja is much faster than it’s predecessor, but this got us thinking. Could we make the JSON processing itself go faster?

Tommi suggested that we build on jackson-databind. It’s a high-level library for mapping Java objects to JSON. This suggestion didn’t make any sense to me: since Cheshire is built on the low-level jackson-core, there’s no way using databind would be faster than that. To demonstrate Tommi how wrong he was, I whipped up a quick implementation and some benchmarks. Guess what happened?

The databind version was faster. Clearly my intuition was off! Here are the results from a more recent benchmark:

Benchmarking is hard and likely we made mistakes. Together with Tommi and Kalle, we decided to put the code in a library anyway. That’s how we got jsonista.

What makes it faster than Cheshire? We believe these are the main reasons:

  • Jsonista pushes as much work as possible to Jackson and to Java, whereas Cheshire's performance-critical parts are in Clojure.
  • Cheshire's big functions and use of definline prevent JVM inlining. Jsonista's methods are small.
  • Unlike Jsonista, Cheshire uses dynamic variables. They always come with a performance penalty.

Jackson's authors have taken care to make Jackson fast and Jsonista tries to make full use of that.

UsageLink to Usage

The coordinates are [metosin/jsonista "0.1.0"]. The basic operations are straightforward:

(require '[jsonista.core :as j])

(j/write-value-as-string {"hello" 1})
;; => "{\"hello\":1}"

(j/read-value *1)
;; => {"hello" 1}

Muuntaja has built-in support for jsonista. You can enable it in your Muuntaja configuration like this:

(require '[muuntaja.format.jsonista :as json-format])

(def m
  (muuntaja/create
    (-> muuntaja/default-options
        json-format/with-json-format)))

For more information about configuration and custom encoders, see GitHub and the API reference. Once you have tried it out, please let us know how it worked for you!

Miikka Koskinen
Thursday, 21 December, 2017

Related posts

Three Levels of AI Impact

by Tapio Nissilä
Cover Image for Three Levels of AI Impact

A Guide for Technology Executives

by Tapio Nissilä
Cover Image for A Guide for Technology Executives

Building Humane AI Adoption

by Tapio Nissilä
Cover Image for Building Humane AI Adoption
More

Contact

HomeServicesExpertiseOpen source supportAboutStoryReferencesCareerInsightsContact

Tampere
Hämeenkatu 13 A 5
33100 Tampere

Helsinki
Kalevankatu 13, 3rd floor
00100 Helsinki

Jyväskylä
Väinönkatu 30, 5th floor
40100 Jyväskylä

Oulu
Kirkkokatu 4 A 51
90100 Oulu

Business-id: 3374640-7
E-invoice: 003733746407
Operator: 003723327487 / Apix
metosin@skannaus.apix.fi

first.last@metosin.fi

Github
Instagram
LinkedIn
YouTube

Cookie settings