GithubHelp home page GithubHelp logo

doytsujin / jsonwheel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from romanboehm/jsonwheel

0.0 2.0 0.0 71 KB

A 200 lines single-source-file hackable JSON deserializer for the JVM. Reinventing the JSON wheel.

License: Apache License 2.0

Java 98.79% Shell 1.21%

jsonwheel's Introduction

JSON Wheel

Have you ever written scripts in Java 11+ and needed to operate on some JSON string? Have you ever needed to extract just that one deeply-nested value of an application/json response without modelling the whole hierarchy? Have you ever felt someone should indeed reinvent that wheel and provide the JVM universe with yet another JSON deserializer?

If the answer to any of those is "yes" then look no further, JSON Wheel's got you covered! It's a 200 lines single-source-file hackable JSON deserializer written in plain Java.

If the answer is "no" I still had fun writing it. :)

It is ...

  • small
  • deserialization-only
  • hackable
  • compatible to JDK 11+
  • dependency-less
  • not tooo slow

It is not ...

  • typed (except for its own API's types)
  • safe for malformed input
  • safe for malicious input (at least I don't give that guarantee)

Usage

Either

a) copy the content of JsonWheel.java as-is into your existing class (remove imports if colliding with your pre-existing ones), or

b) copy the whole JsonWheel.java over into your app (adjust the package declaration if needed).

Then you can ...

1) Deserialize JSON objects

var json = """
    {
        "foo": 1,
        "bar": "baz",
        "qux": null
    }""";

var node = JsonWheel.read(json);
var a = node.get("foo").val(Integer.class); // 1
var b = node.get("bar").val(String.class);  // "baz"
var b = node.get("qux").val(String.class);  // null

2) Deserialize JSON arrays

var json = """
    [
        1,
        2,
        3
    ]""";

var node = JsonWheel.read(json);
var list = node.elements()
        .stream()
        .map(e -> e.val(Integer.class))
        .toList();  // 1 2 3

3) Deserialize "complex" JSON

var json = """
    {
        "foo": {
            "bar" : [
                1,
                2,
                3
            ],
            "baz": "qux"
        }
    }""";

var node = JsonWheel.read(json);
var bar = node.get("foo").get("bar").elements()
        .stream()
        .map(e -> e.val(Integer.class))
        .toList(); //   1 2 3
var baz = node.get("foo").get("baz").val(String.class); //  "qux"

jsonwheel's People

Contributors

romanboehm avatar

Watchers

James Cloos avatar  avatar

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.