GithubHelp home page GithubHelp logo

json-parse's Introduction

Json Parse Build Status

A tool to quickly parse JSON into Java maps and lists. Significantly faster than FasterXML's Jackson with an extremely small memory and size footprint.

Lower is better:

Wiki page explaining source of results

Usage

String mapString = "{\"fast\":true, \"super-neat\":true}";
String listString = "[1, 2, false]";
String stringString = "\"yo dawg\"";
String numberString = "-1.2e-3";
String boolString = "true";

Map<String, Object> map = JsonParse.map(mapString);
List<Object> list = JsonParse.list(listString);
String string = JsonParse.string(stringString);
Number number = JsonParse.number(numberString);
Boolean bool = JsonParse.bool(boolString);

// Will still parse if type isn't explicitly specified
Object unknown = JsonParse.parse(scanner.nextLine());
System.out.println(unkown.getClass());

Getting the dependency

Maven

<dependency>
    <groupId>ca.fuzzlesoft</groupId>
    <artifactId>json-parse</artifactId>
    <version>1.3.4</version>
</dependency>

Gradle

compile 'ca.fuzzlesoft:json-parse:1.3.4'

Features

Shows path to syntax error if given invalid JSON

If incorrect JSON is passed to JsonParse, the thrown exception will explain where the JSON is invalid.

{"outer": {"inner": {"a": true, "b": nulll}}}
<root>.outer.inner.b: "nulll" is not a valid constant. Missing quotes?

{"outer": {"inner": {"a": true "b": false}}}
<root>.outer.inner.a: wasn't followed by a comma

{"outer": {"no-colon" true}}
<root>.outer.no-colon: "no-colon" wasn't followed by a colon

{"object": {no-quotes: true}}
<root>.object: unexpected character 'n' where a property name is expected. Missing quotes?

{"outer": {"sick-list": [{}, {"inner": [1, [1, 2, 3, [fasle]]]}]}}
<root>.outer.sick-list.[1].inner.[1].[3].[0]: "fasle" is not a valid constant. Missing quotes?

Thread safe

Can be used from multiple thread contexts without losing data integrity or failing.

public static void main(String[] args) throws InterruptedException {
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final int id = i;
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    List<Object> ints = JsonParse.list("[1, 2, 3]");
                    System.out.printf("%d: %d,%d,%d\n", id, ints.get(0), ints.get(1), ints.get(2));
                } catch (JsonParseException e) {
                    System.err.println("Exception thrown");
                }
            }
        });
        thread.start();
        threads.add(thread);
    }
    for (int i = 0; i < 10; i++) {
        threads.get(i).join();
    }
}
0: 1,2,3
9: 1,2,3
8: 1,2,3
7: 1,2,3
5: 1,2,3
6: 1,2,3
1: 1,2,3
3: 1,2,3
2: 1,2,3
4: 1,2,3

Convert JSON strings to a Java objects.

// For example, arrays.
String listString = "[1, 2, false]";
List<Object> list = JsonParse.list(listString);

FAQ

  • Can this convert from JSON directly to Plain Old Java Objects?

Unfortunately not. JSON doesn't have any type information, so explicit knowledge is required before JSON can be converted. That would be the job of a "binding framework"

  • Why is the code squashed so much?

I wanted to minimize the number of function calls. Though the cost is minimal, it will still affect performance on the large scale. Besides, my goal is for this to be a "finish and forget" project, where the speed benefits add up over time, while the maintenance cost is nonexistent because the code "just works". We'll see how naive this is over time ;)

License

MIT License (Expat)

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.