All Projects → zalando-incubator → Flatjson

zalando-incubator / Flatjson

Licence: mit
A fast JSON parser (and builder)

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Flatjson

Coolie
Coolie(苦力) helps you to create models (& their constructors) from a JSON file.
Stars: ✭ 508 (+1202.56%)
Mutual labels:  json, json-parser
Jsonui
jsonui is an interactive JSON explorer on your command line
Stars: ✭ 583 (+1394.87%)
Mutual labels:  json, json-parser
Fastjson
A fast JSON parser/generator for Java.
Stars: ✭ 23,997 (+61430.77%)
Mutual labels:  json, json-parser
Flatcc
FlatBuffers Compiler and Library in C for C
Stars: ✭ 434 (+1012.82%)
Mutual labels:  json, json-parser
Xml Js
Converter utility between XML text and Javascript object / JSON text.
Stars: ✭ 874 (+2141.03%)
Mutual labels:  json, json-parser
Swiftyjson
The better way to deal with JSON data in Swift.
Stars: ✭ 21,042 (+53853.85%)
Mutual labels:  json, json-parser
Pikkr
JSON parser which picks up values directly without performing tokenization in Rust
Stars: ✭ 580 (+1387.18%)
Mutual labels:  json, json-parser
Jsoncons
A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
Stars: ✭ 400 (+925.64%)
Mutual labels:  json, json-parser
Spray Json
A lightweight, clean and simple JSON implementation in Scala
Stars: ✭ 917 (+2251.28%)
Mutual labels:  json, json-parser
Boltons
🔩 Like builtins, but boltons. 250+ constructs, recipes, and snippets which extend (and rely on nothing but) the Python standard library. Nothing like Michael Bolton.
Stars: ✭ 5,671 (+14441.03%)
Mutual labels:  json, utilities
Jstream
Streaming JSON parser for Go
Stars: ✭ 427 (+994.87%)
Mutual labels:  json, json-parser
Jsonpath Rs
JSONPath for Rust
Stars: ✭ 31 (-20.51%)
Mutual labels:  json, json-parser
Jtc
JSON processing utility
Stars: ✭ 425 (+989.74%)
Mutual labels:  json, json-parser
Argonaut
Purely functional JSON parser and library in scala.
Stars: ✭ 501 (+1184.62%)
Mutual labels:  json, json-parser
Jsonparser
One of the fastest alternative JSON parser for Go that does not require schema
Stars: ✭ 4,323 (+10984.62%)
Mutual labels:  json, json-parser
Json
JSON for Modern C++
Stars: ✭ 27,824 (+71243.59%)
Mutual labels:  json, json-parser
Easyjson
Fast JSON serializer for golang.
Stars: ✭ 3,512 (+8905.13%)
Mutual labels:  json, json-parser
Bad json parsers
Exposing problems in json parsers of several programming languages.
Stars: ✭ 351 (+800%)
Mutual labels:  json, json-parser
Djson
Fast Go decoder for dynamic JSON
Stars: ✭ 588 (+1407.69%)
Mutual labels:  json, json-parser
Jsondoc
JSON object for Delphi based on IUnknown and Variant
Stars: ✭ 20 (-48.72%)
Mutual labels:  json, json-parser

flatjson

A fast json parser (and builder), written in java.

uyubi salt flats photo: yoann supertramp [CC-BY]

Features

  • efficient — allocates as few objects as possible
  • easy to use — simple api, inspired by minimal-json
  • fast — like a bat out of hell!

Performance

the following chart shows benchmark results for parsing a 72K sample file on my macbook pro (2,7 ghz intel core i5).

benchmark chart

flatjson outperforms some popular json parsers (gson, jackson) by 2x to 3x, and is even faster than boon (which is known to be pretty fast).

normally, we want to do something with json, once we've parsed it. the second benchmark simulates an event processing use case: parse the event, process the data (= reverse an array which makes up the bulk of the json document), then serialize the result back to a string.

benchmark chart

as the graph shows, flatjson shines even more here.

you can run these benchmark yourself with ./gradlew jmh — a full run takes a bit over an hour.

So, what's the trick?

flatjson does not build a parse tree, just an index overlay, which is stored in an integer array. json nodes are constructed on demand (= on first access). this way, lots of objects allocations are saved.

when serializing json, flatjson handles unchanged subtrees by copying substrings directly from input to output, bypassing actual serialization as much as possible.

Installation

flatjson is on Maven Central, simply add it as a gradle dependency:

compile 'org.zalando:flatjson:1.1.0'

Usage

Json json = Json.parse("[42, true, \"hello\"]");

we can check which type of entity the returned Json object represents:

json.isNumber(); // --> false
json.isObject(); // --> false
json.isArray(); // --> true

for each isFoo method, there is a matching asFoo accessor. arrays are represented by lists of Json objects.

List<Json> array = json.asArray();
array.size(); // --> 3
array.get(0).asLong(); // --> 42
array.get(1).asBoolean(); // --> true
array.get(2).asString(); // --> "hello"

this list is mutable and allows manipulation of the json DOM:

array.add(Json.value(false));
json.toString(); // --> "[42,true,\"hello\",false]"

you can also build objects from scratch:

Json test = Json.object();
Map<String, Json> object = test.asObject();
object.put("color", Json.value("blue"));
object.put("size", Json.value(39));
test.toString(); // --> "{\"color\":\"blue\",\"size\":39}"

same with arrays:

Json test = Json.array();
List<Json> array = test.asArray();
array.add(Json.value("hello"));
array.add(Json.value(42));
test.toString(); // --> "[\"hello\",42]"

Contributing

contributions are welcome — especially

  • reporting bugs
  • running the benchmarks in different environments (AMD, Azure, AWS, Google Cloud ...) and sharing the results
  • adding your favorite json parser to the benchmarks — can it beat flatjson?
  • adding more unit tests.

i will not easily be persuaded to merge in major new features, though.

History

1.1.0 — 2017-04-03
  • implemented Visitor pattern to interact with json
  • added json.prettyPrint() (implemented as Visitor)
  • json.equals() now based on json.prettyPrint()
1.0.1 — 2017-03-17
  • support additional number types: int, float, BigInteger, BigDecimal
1.0 — 2017-03-10
  • initial public release

License

The MIT License (MIT)

Copyright (c) 2017 Zalando SE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].