Class Json5


  • public final class Json5
    extends java.lang.Object
    This is the main class for using Json5. This class provides methods to parse and serialize Json5 data according to the specification and the configured options.

    You can create a Json5 instance by invoking Json5(Json5Options) or by using builder(Function).

    This class contains several utility methods to parse and serialize json5 data by passing Reader, Writer or simple String instances.

     
     // 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 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)
    • Method Detail

      • parse

        public Json5Element parse​(java.io.InputStream in)
        Parses the data from the InputStream into a tree of Json5Element's.

        Note: The stream must be closed after operation

        Parameters:
        in - Can be any applicable InputStream
        Returns:
        Parsed json5 tree. Can be null if 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 of Json5Element's.

        Note: The reader must be closed after operation

        Parameters:
        reader - Can be any applicable Reader
        Returns:
        Parsed json5 tree. Can be null if 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-encoded String into a parse tree of Json5Element's.
        Parameters:
        string - Json5 encoded String
        Returns:
        Parsed json5 tree. Can be null if the provided String is 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 - Json5Element to serialize
        out - Can be any applicable OutputStream
        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 - Json5Element to serialize
        writer - Can be any applicable Writer
        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 - Json5Element to serialize
        Returns:
        Json5 encoded String
        Throws:
        java.io.IOException - If an I/O error occurs
        See Also:
        serialize(Json5Element, Writer)