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 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>
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.
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);
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) {
// ...
}
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) {
// ...
}
Detailed javadoc documentation can be found at javadoc.io.
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
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.