tquadrat's Foundation Library org.tquadrat.foundation.jsonbuilder 0.25.12 API
This library provides a pure and straight-forward JSON Builder. It does
not provide any parser capabilities, it is not an object mapper in any
way. Basically you can look at it as a type-safe replacement of
StringBuilder
for JSON output.
The main class is
JSONBuilder;
it is used to create the various other JSON values:
Call the factory
JSONBuilder.getInstance()
to get an instance of JSONBuilder.
Aside the
indentation
for the pretty printing is JSONBuilder stateless and therefore thread-safe.
The library is used like this:
001…
002final var builder = JSONBuilder.getInstance();
003
004final var rootObject = builder.createObject();
005rootObject.set( "booleanValue", true );
006rootObject.set( "integerValue", 123 );
007rootObject.set( "stringValue", """
008 FirstLine
009 SecondLine
010 ThirdLine""" );
011
012final var object = rootObject.setObject( "objectValue" );
013object.set( "otherStringValue", "Simple String" );
014final var array = rootObject.setArray( "arrayValue" );
015for( var i; i < 10; ++1 )
016{
017 array.add( i );
018}
019…To return a plain String representation of rootObject, just call
toString
on it. For a formatted (pretty printed) output, use
format()
like this:
101…
102final var prettyJSON = String.format( "%s", rootObject );
103…It is important not to use rootObject.toString() in line 102.
