Giter Club home page Giter Club logo

oojson's Introduction

Build Status Coverage Status

Object-oriented Java API for JSON

Simple object-oriented Java API for JSON transformation. It is a wrapper around JSON-P.

Creating JSON arrays

From values

new JsonArr<>(
    true,
    "a",
    1,
    new JsonArr(),
    new JsonObj()
);

or from a string

new JsonArr(
    new StringReader("[false,\"x\",1,[],{}]")
);

or from JSON-P array

new JsonArr<>(
    Json.createArrayBuilder().add(0).build()
);

Creating JSON objects

From object attributes

new JsonObj(
    new Attr<>("bool", true),
    new Attr<>("str", "a"),
    new Attr<>("num", 0),
    new Attr<>("arr", new JsonArr()),
    new Attr<>("obj", new JsonObj())
);

or from a string

new JsonObj(
    new StringReader(
        "{\"bool\":false,\"str\":\"A\",\"num\":1,\"arr\":[],\"obj\":{}}"
    )
);

or from JSON-P object

new JsonObj(
    Json.createObjectBuilder().add("name", "John").build()
);

Transforming JSON arrays (with help of Cactoos library)

In the example, numerical values are filtered and mapped to string values

JsonArr<Integer> array = new JsonArr<>(
    10,
    20
);
new JsonArr<>(
    new Mapped<>(
        elem -> elem + " points",
        new Filtered<>(
            elem -> elem > 15,
            array.value()
        )
    )
).jsonValue().toString();

The result is array ["20 points"].

Making JSON objects fit

In the example, value of num attribute is multiplied by 2.

JsonObj object = new JsonObj(
    new Attr<>("str", "A"),
    new Attr<>("num", 1)
);
new FitValUpd(
    "num",
    (object.<Integer>get("num")) * 2
).make(object).jsonValue().toString();

The result is object {"str":"A","num":2}.

In the example, attribute with name delete is removed and attribute with name info is replaced with attribute "moreInfo" and value true.

JsonObj object = new JsonObj(
    new Attr<>("delete", "private"),
    new Attr<>("info", "public info")
);
new FitChain<>(
    new FitAttrDel("delete"),
    new FitAttrRepl("info", new Attr<>("moreInfo", true))
).make(object).jsonValue().toString();

The result is object {"moreInfo":true}.

Library supports JSON specification.

Please, note the library is still in early development and it's API can frequently change.

To get started, add dependency to your project:

<dependency>
    <groupId>com.github.piotrkot</groupId>
    <artifactId>oojson</artifactId>
    <version>1.6.6</version>
</dependency>

You may need to add JSON-P dependency:

<dependency>
    <groupId>javax.json</groupId>
    <artifactId>javax.json-api</artifactId>
    <version>1.1.4</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1.4</version>
    <scope>runtime</scope>
</dependency>

Feel free to fork me on GitHub, report bugs or post comments.

For Pull Requests, please run mvn clean package, first.

oojson's People

Contributors

golszewski86 avatar piotrkot 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.