Skip to content
lare96 edited this page Jul 18, 2016 · 4 revisions

Boon has JSON support.

This is a very early version of the JSON docs.

I think what you really want is here:

https://github.com/boonproject/boon/wiki/Boon-JSON-in-five-minutes

Boon JSON support is fast and flexible.


Benchmark                                    Mode Thr     Count  Sec         Mean   Mean error    Units

i.g.j.s.BoonSerializer.roundTriper          thrpt   8         4    1   442410.617    10370.558    ops/s


i.g.j.s.JacksonSerializer.roundTriper       thrpt   8         4    1   183792.721    95187.259    ops/s



i.g.j.s.BoonSerializer.serializeSmall       thrpt   8         4    1   735688.146    12604.568    ops/s

i.g.j.s.JacksonSerializer.serializeSmall    thrpt   8         4    1   493650.013   447762.556    ops/s

See some test results here for general JSON parse speed: http://rick-hightower.blogspot.com/2013/12/boon-json-parser-seems-to-be-fastest.html

The Factory allows you to override serialization per property, per type, and also override property filtering.

        final JsonSerializer serializer = new JsonSerializerFactory()
                      .addFilter(...)
                      .addPropertySerializer(...)
                      .addTypeSerializer(...).create();

Most users will be able to use the serializer as follows:

        final JsonSerializer serializer = new JsonSerializerFactory().create();

To customize which properties are sent out you can register a property filter as follows:

factory.addFilter ( new Function<FieldSerializationData, Boolean> () {
            @Override
            public Boolean apply ( FieldSerializationData fieldSerializationData ) {
                if (fieldSerializationData.fieldName.equals (  "ignoreMe3" ) ) {
                    return true;
                } else {
                    return false;
                }
            }
        } )

To customize how a property is sent out you can register a property serializer as follows:

factory.addPropertySerializer ( new Function<FieldSerializationData, Boolean> () {
            @Override
            public Boolean apply ( FieldSerializationData fieldSerializationData ) {

                if ( fieldSerializationData.type.equals ( long.class ) &&
                        fieldSerializationData.fieldName.endsWith ( "Date" ) ) {

                    Date date = Conversions.toDate ( fieldSerializationData.value );

                    final String jsonDateString = Dates.jsonDate ( date );

                    fieldSerializationData.output.add ( jsonDateString);
                    return true;
                } else {
                    return false;
                }

            }
        } )

To customize how a type is written to the output stream, you can add a type serializer as follows:

factory.addTypeSerializer ( FooBasket.class, new Function<ObjectSerializationData, Boolean> () {
            @Override
            public Boolean apply ( ObjectSerializationData objectSerializationData ) {
                objectSerializationData.output.add("[\"wiki\",\"wiki\",\"wiki\"]");
                return true;
            }
        } )

Boon supports the annotations JsonIgnore, JsonInclude and JsonIgnoreProperties.

Here is a sample POJO you can write out to a JSON String:

...

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@JsonIgnoreProperties ("ignoreMe2")
public class AllTypes {



    String ignoreMe3;

    String ignoreMe2;

    int myInt;
    boolean myBoolean;
    short myShort;
    long myLong;
    String string;
    String string2;
    BigDecimal bigDecimal;
    BigInteger bigInteger;
    Date date;

    float myFloat;
    double myDouble;
    byte myByte;

    FooEnum foo;
    FooEnum bar;

    @JsonIgnore
    String ingnoreMe;


    long someDate = new Date (  ).getTime ();


    FooBasket fooBasket = new FooBasket ();

    AllTypes allType;

    List<AllTypes> allTypes = new ArrayList<> (  );


    public String getString2 () {
        return string2;
    }

    public void setString2 ( String string2 ) {
        this.string2 = string2;
    }

    public List<AllTypes> getAllTypes () {
        return allTypes;
    }

    public void setAllTypes ( List<AllTypes> allTypes ) {
        this.allTypes = allTypes;
    }

    public AllTypes getAllType () {
        return allType;
    }

    public void setAllType ( AllTypes allType ) {
        this.allType = allType;
    }

    public byte getMyByte () {
        return myByte;
    }

    public void setMyByte ( byte myByte ) {
        this.myByte = myByte;
    }

    public int getMyInt () {
        return myInt;
    }

    public void setMyInt ( int myInt ) {
        this.myInt = myInt;
    }

    public boolean isMyBoolean () {
        return myBoolean;
    }

    public void setMyBoolean ( boolean myBoolean ) {
        this.myBoolean = myBoolean;
    }

    public short getMyShort () {
        return myShort;
    }

    public void setMyShort ( short myShort ) {
        this.myShort = myShort;
    }

    public long getMyLong () {
        return myLong;
    }

    public void setMyLong ( long myLong ) {
        this.myLong = myLong;
    }

    public String getString () {
        return string;
    }

    public void setString ( String string ) {
        this.string = string;
    }


    public float getMyFloat () {
        return myFloat;
    }

    public void setMyFloat ( float myFloat ) {
        this.myFloat = myFloat;
    }

    public double getMyDouble () {
        return myDouble;
    }

    public void setMyDouble ( double myDouble ) {
        this.myDouble = myDouble;
    }


    public BigDecimal getBigDecimal () {
        return bigDecimal;
    }

    public void setBigDecimal ( BigDecimal bigDecimal ) {
        this.bigDecimal = bigDecimal;
    }

    public BigInteger getBigInteger () {
        return bigInteger;
    }

    public void setBigInteger ( BigInteger bigInteger ) {
        this.bigInteger = bigInteger;
    }


    public Date getDate () {
        return date;
    }

    public void setDate ( Date date ) {
        this.date = date;
    }


    public FooEnum getFoo () {
        return foo;
    }

    public void setFoo ( FooEnum foo ) {
        this.foo = foo;
    }

    public FooEnum getBar () {
        return bar;
    }

    public void setBar ( FooEnum bar ) {
        this.bar = bar;
    }
...
}

Here is some sample code to write and the read this object:

        AllTypes foo = new AllTypes ();
        foo.setDate ( new Date() );
        foo.setBar ( FooEnum.BAR );
        foo.setFoo ( FooEnum.FOO );
        foo.setString ( "Hi Mom" );

        JsonSerializer serializer = new JsonSerializerFactory().create();

        String json = serializer.serialize ( foo ).toString ();

To read the JSON String back, you need the following:

        JsonParser parser = new JsonParserFactory().create();
        AllTypes testMe = jsonParser.parse( AllTypes.class, json);

JsonParsers and JsonSerializers are stateful, and will reuse output buffers for speed. You can hold on to them and reuse them as long as you make sure they are only accessed by one thread at a time.

Thoughts

Thoughts? Write me at richard high tower AT g mail dot c-o-m (Rick Hightower).

Further Reading:

If you are new to boon start here:

Why Boon?

Easily read in files into lines or a giant string with one method call. Works with files, URLs, class-path, etc. Boon IO support will surprise you how easy it is. Boon has Slice notation for dealing with Strings, Lists, primitive arrays, Tree Maps, etc. If you are from Groovy land, Ruby land, Python land, or whatever land, and you have to use Java then Boon might give you some relief from API bloat. If you are like me, and you like to use Java, then Boon is for you too. Boon lets Java be Java, but adds the missing productive APIs from Python, Ruby, and Groovy. Boon may not be Ruby or Groovy, but its a real Boon to Java development.

Core Boon Philosophy

Core Boon will never have any dependencies. It will always be able to run as a single jar. This is not just NIH, but it is partly. My view of what Java needs is more inline with what Python, Ruby and Groovy provide. Boon is an addition on top of the JVM to make up the difference between the harder to use APIs that come with Java and the types of utilities that are built into Ruby, Python, PHP, Groovy etc. Boon is a Java centric view of those libs. The vision of Boon and the current implementation is really far apart.

===

Contact Info

blog|[twitter](https://twitter.com/RickHigh|[infoq]http://www.infoq.com/author/Rick-Hightower|[stackoverflow](http://stackoverflow.com/users/2876739/rickhigh)|[java lobby](http://java.dzone.com/users/rhightower)|Other | richard high tower AT g mail dot c-o-m (Rick Hightower)|work|cloud|nosql

Clone this wiki locally