Package de.marhali.json5
Class Json5
- java.lang.Object
-
- de.marhali.json5.Json5
-
public final class Json5 extends java.lang.ObjectThis is the main class for using Json5. This class provides methods to parse and serialize Json5 data according to the specification and the configuredoptions.You can create a Json5 instance by invoking
Json5(Json5Options)or by usingbuilder(Function).This class contains several utility methods to parse and serialize json5 data by passing
Reader,Writeror simpleStringinstances.// Create Json5 instance using builder pattern to configure desired options Json5 json5 = Json5.builder(builder -> builder .quoteless() .quoteSingle() .parseComments() .writeComments() .prettyPrinting() .build() ); // Parse from a String Json5Element element = json5.parse("{ 'key': 'value', 'array': ['first val','second val'] }"); // Or parse from a Reader or InputStream try (InputStream stream = ...) { Json5Element element = json5.parse(stream); // ... } catch (IOException e) { // ... } // Serialize to a String String json5String = json5.serialize(element); // Serialize to a Writer or OutputStream try (OutputStream stream = ...) { json5.serialize(element, stream); // ... } catch (IOException e) { // ... }- See Also:
- Json5 Specification,
Json5Parser,Json5Writer
-
-
Constructor Summary
Constructors Constructor Description Json5()Constructs a json5 instance by usingJson5Options.DEFAULTas configuration.Json5(Json5Options options)Constructs a new json5 instance with custom configuration for parsing and serialization.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Json5builder(java.util.function.Function<Json5Options.Builder,Json5Options> builder)Constructs a new json5 instance by using theJson5Options.builder().Json5Elementparse(java.io.InputStream in)Parses the data from theInputStreaminto a tree ofJson5Element's.Json5Elementparse(java.io.Reader reader)Parses the provided read-stream into a tree ofJson5Element's.Json5Elementparse(java.lang.String string)Parses the provided json5-encodedStringinto a parse tree ofJson5Element's.java.lang.Stringserialize(Json5Element element)Encodes the provided element into its character literal representation.voidserialize(Json5Element element, java.io.OutputStream out)Encodes the provided element into its character literal representation by using an output-stream.voidserialize(Json5Element element, java.io.Writer writer)Encodes the provided element into its character literal representation by using a write-stream.
-
-
-
Constructor Detail
-
Json5
public Json5(Json5Options options)
Constructs a new json5 instance with custom configuration for parsing and serialization.- Parameters:
options- Configuration options- See Also:
builder(Function)
-
Json5
public Json5()
Constructs a json5 instance by usingJson5Options.DEFAULTas configuration.- See Also:
Json5(Json5Options)
-
-
Method Detail
-
builder
public static Json5 builder(java.util.function.Function<Json5Options.Builder,Json5Options> builder)
Constructs a new json5 instance by using theJson5Options.builder().- Parameters:
builder- Options builder- Returns:
- Built options
-
parse
public Json5Element parse(java.io.InputStream in)
Parses the data from theInputStreaminto a tree ofJson5Element's.Note: The stream must be closed after operation
- Parameters:
in- Can be any applicableInputStream- Returns:
- Parsed json5 tree. Can be
nullif the provided stream does not contain any data - See Also:
parse(Reader)
-
parse
public Json5Element parse(java.io.Reader reader)
Parses the provided read-stream into a tree ofJson5Element's.Note: The reader must be closed after operation
- Parameters:
reader- Can be any applicableReader- Returns:
- Parsed json5 tree. Can be
nullif the provided stream does not contain any data - See Also:
Json5Parser.parse(Json5Lexer)
-
parse
public Json5Element parse(java.lang.String string)
Parses the provided json5-encodedStringinto a parse tree ofJson5Element's.- Parameters:
string- Json5 encodedString- Returns:
- Parsed json5 tree. Can be
nullif the providedStringis empty - See Also:
parse(Reader)
-
serialize
public void serialize(Json5Element element, java.io.OutputStream out) throws java.io.IOException
Encodes the provided element into its character literal representation by using an output-stream.Note: The stream must be closed after operation (
OutputStream.close())!- Parameters:
element-Json5Elementto serializeout- Can be any applicableOutputStream- Throws:
java.io.IOException- If an I/O error occurs- See Also:
serialize(Json5Element, Writer)
-
serialize
public void serialize(Json5Element element, java.io.Writer writer) throws java.io.IOException
Encodes the provided element into its character literal representation by using a write-stream.Note: The writer must be closed after operation (
Writer.close())!- Parameters:
element-Json5Elementto serializewriter- Can be any applicableWriter- Throws:
java.io.IOException- If an I/O error occurs- See Also:
Json5Writer.write(Json5Element)
-
serialize
public java.lang.String serialize(Json5Element element) throws java.io.IOException
Encodes the provided element into its character literal representation.- Parameters:
element-Json5Elementto serialize- Returns:
- Json5 encoded
String - Throws:
java.io.IOException- If an I/O error occurs- See Also:
serialize(Json5Element, Writer)
-
-