Skip to content

This is a lightweight library to parse and serialize JSON5 data.

License

Notifications You must be signed in to change notification settings

marhali/json5-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json5-java

Build JavaDoc Coverage Donate

This is a reference implementation of the JSON5 standard in Java 11+, capable of parsing and serialization of JSON5 data.

This library is an enhanced version of Synt4xErr0r4 / json5, which provides a better full-fledged API inspired by the GSON library.

Download

Download the latest release manually or add a Maven dependency. Don't worry the project is already in the Maven Central Repository. Just add the following configuration:

<dependencies>
    <dependency>
        <groupId>de.marhali</groupId>
        <artifactId>json5-java</artifactId>
        <version>2.0.0</version>
    </dependency>
</dependencies>

Usage

This library can be used by either configuring a Json5 instance or by using the underlying Json5Parser and Json5Writer.

The following section describes how to use this library with the Json5 core class.

Configure Json5 instance

See Parsing & Serialization Options to see a list of possible configuration options.

// Using builder pattern
Json5 json5 = Json5.builder(options ->
        options.allowInvalidSurrogate().quoteSingle().prettyPrinting().build());

// Using configuration object
Json5Options options = new Json5Options(true, true, true, 2);
Json5 json5 = new Json5(options);

Parsing

Json5 json5 = ...

// Parse from a String literal
Json5Element element = 
        json5.parse("{ 'key': 'value', 'array': ['first val','second val'] }");

// ...

// Parse from a Reader or InputStream
try(InputStream stream = ...) {
    Json5Element element = json5.parse(stream);
    // ...
} catch (IOException e) {
    // ...
}

Serialization

Json5Element element = ...

// Serialize to a String literal
String jsonString = json5.serialize(element);

// ...

// Serialize to a Writer or OutputStream        
try(OutputStream stream = ...) {
    json5.serialize(element, stream);
    // ...
} catch (IOException e) {
    // ...
}

Documentation

Detailed javadoc documentation can be found at javadoc.io.

Parsing & Serialization Options

This library supports a few customizations to adjust the behaviour of parsing and serialization. For a detailed explanation see the Json5Options class.

  • allowInvalidSurrogates
  • quoteSingle
  • trailingComma
  • indentFactor

License

This library is released under the Apache 2.0 license.

Partial parts of the project are based on GSON and Synt4xErr0r4 / json5. The affected classes contain the respective license notice.