Coder Social home page Coder Social logo

flatjson's Introduction

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.

flatjson's People

Contributors

tlossen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

flatjson's Issues

Allow reading JSON strings as char[]

In many REST APIs, login & signup operations accept JSON objects with username and password properties. JSON parsers, however, always seem to create String instances for any JSON string property - even when you use an object mapping facility to map it to a char[] field.

This, of course, goes against the long-standing principle of using char[] for passwords so you can wipe the contents to make sure they don't linger in memory longer than needed (e.g. see all common password hasher APIs).

flatjson's approach of using an index and only allocating objects when neccessary seems uniquely suited to offer an alternative: if my code knows it's reading sensitive data, it could call an asCharArray() method instead of asString().

Is this something you could and would implement?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.