Class JSONSerializer

  • All Implemented Interfaces:
    Serializer<java.lang.Object>

    public class JSONSerializer
    extends java.lang.Object
    implements Serializer<java.lang.Object>
    Implementation of the Serializer interface that reads data from and writes data to a JavaScript Object Notation (JSON) file.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean getAllowMacros()
      Returns the flag indicating whether this serializer will allow macros in the text.
      boolean getAlwaysDelimitMapKeys()
      Returns a flag indicating whether or not map keys will always be quote-delimited.
      java.nio.charset.Charset getCharset()
      Returns the character set used to encode/decode the JSON data.
      ListenerList<JSONSerializerListener> getJSONSerializerListeners()  
      java.lang.String getMIMEType​(java.lang.Object object)
      Returns the MIME type of the data read and written by this serializer.
      java.lang.reflect.Type getType()
      Returns the type of the object that will be returned by readObject(Reader).
      boolean isVerbose()
      Returns the serializer's verbosity flag.
      static java.lang.Object parse​(java.lang.String json)
      Converts a JSON value to a Java object.
      static java.lang.Boolean parseBoolean​(java.lang.String json)
      Converts a JSON value to a boolean.
      static java.lang.Double parseDouble​(java.lang.String json)
      Converts a JSON value to a double.
      static java.lang.Float parseFloat​(java.lang.String json)
      Converts a JSON value to a float.
      static java.lang.Integer parseInteger​(java.lang.String json)
      Converts a JSON value to a integer.
      static List<?> parseList​(java.lang.String json)
      Converts a JSON value to a list.
      static java.lang.Long parseLong​(java.lang.String json)
      Converts a JSON value to a long.
      static Map<java.lang.String,​?> parseMap​(java.lang.String json)
      Converts a JSON value to a map.
      static java.lang.Number parseNumber​(java.lang.String json)
      Converts a JSON value to a number.
      static java.lang.Short parseShort​(java.lang.String json)
      Converts a JSON value to a short.
      static java.lang.String parseString​(java.lang.String json)
      Converts a JSON value to a string.
      java.lang.Object readObject​(java.io.InputStream inputStream)
      Reads data from a JSON stream.
      java.lang.Object readObject​(java.io.Reader reader)
      Reads data from a JSON stream.
      void setAllowMacros​(boolean allowMacros)
      Sets the flag indicating whether macros are allowed in the text.
      void setAlwaysDelimitMapKeys​(boolean delimitKeys)
      Sets a flag indicating that map keys should always be quote-delimited.
      void setVerbose​(boolean verboseValue)
      Sets the serializer's verbosity flag.
      static java.lang.String toString​(java.lang.Object value)
      Converts a object to a JSON string representation.
      static java.lang.String toString​(java.lang.Object value, boolean alwaysDelimitMapKeys)
      Converts a object to a JSON string representation.
      void writeObject​(java.lang.Object object, java.io.OutputStream outputStream)
      Writes data to a JSON stream.
      void writeObject​(java.lang.Object object, java.io.Writer writer)
      Writes data to a JSON stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_CHARSET

        public static final java.nio.charset.Charset DEFAULT_CHARSET
      • DEFAULT_TYPE

        public static final java.lang.reflect.Type DEFAULT_TYPE
    • Constructor Detail

      • JSONSerializer

        public JSONSerializer()
      • JSONSerializer

        public JSONSerializer​(java.nio.charset.Charset cs)
      • JSONSerializer

        public JSONSerializer​(java.lang.reflect.Type objType)
      • JSONSerializer

        public JSONSerializer​(java.nio.charset.Charset cs,
                              java.lang.reflect.Type objType)
    • Method Detail

      • getCharset

        public java.nio.charset.Charset getCharset()
        Returns the character set used to encode/decode the JSON data.
        Returns:
        The current character set.
      • getType

        public java.lang.reflect.Type getType()
        Returns the type of the object that will be returned by readObject(Reader).
        Returns:
        The current object type.
      • getAlwaysDelimitMapKeys

        public boolean getAlwaysDelimitMapKeys()
        Returns a flag indicating whether or not map keys will always be quote-delimited.

        Note: the JSON "standard" requires keys to be delimited.

        Returns:
        true if map keys must always be delimited (that is, enclosed in double quotes), false for the default behavior that does not require double quotes.
      • setAlwaysDelimitMapKeys

        public void setAlwaysDelimitMapKeys​(boolean delimitKeys)
        Sets a flag indicating that map keys should always be quote-delimited.
        Parameters:
        delimitKeys - true to bound map keys in double quotes; false to only quote-delimit keys as necessary.
      • isVerbose

        public boolean isVerbose()
        Returns the serializer's verbosity flag.
        Returns:
        The verbosity flag for this serializer.
      • setVerbose

        public void setVerbose​(boolean verboseValue)
        Sets the serializer's verbosity flag. When verbosity is enabled, all data read or written will be echoed to the console.
        Parameters:
        verboseValue - true to set verbose mode, false to disable.
      • getAllowMacros

        public boolean getAllowMacros()
        Returns the flag indicating whether this serializer will allow macros in the text.
        Returns:
        The "macros allowed" flag.
      • setAllowMacros

        public void setAllowMacros​(boolean allowMacros)
        Sets the flag indicating whether macros are allowed in the text. This is definitely a non-standard feature. See the documentation in MacroReader for more details on the specification of macros.

        Note: must be called before readObject(java.io.InputStream) is called.

        Parameters:
        allowMacros - Flag indicating whether macros are allowed (default is false). The flag must be set to true in order to activate this feature, because there is a definitely measured 25x slowdown when using it, even if no macros are defined.
      • readObject

        public java.lang.Object readObject​(java.io.InputStream inputStream)
                                    throws java.io.IOException,
                                           SerializationException
        Reads data from a JSON stream.
        Specified by:
        readObject in interface Serializer<java.lang.Object>
        Parameters:
        inputStream - The input stream from which data will be read.
        Returns:
        The deserialized object.
        Throws:
        java.io.IOException - for any errors accessing or reading the object.
        SerializationException - for any formatting errors encountered while deserializing the object.
        See Also:
        readObject(Reader)
      • readObject

        public java.lang.Object readObject​(java.io.Reader reader)
                                    throws java.io.IOException,
                                           SerializationException
        Reads data from a JSON stream.

        Processes macros at this level using MacroReader.

        Parameters:
        reader - The reader from which data will be read.
        Returns:
        One of the following types, depending on the content of the stream and the value of getType():
        • pivot.collections.Dictionary
        • pivot.collections.Sequence
        • java.lang.String
        • java.lang.Number
        • java.lang.Boolean
        • null
        • A JavaBean object
        Throws:
        java.io.IOException - for any I/O related errors while reading.
        SerializationException - for any formatting errors in the data.
      • writeObject

        public void writeObject​(java.lang.Object object,
                                java.io.OutputStream outputStream)
                         throws java.io.IOException,
                                SerializationException
        Writes data to a JSON stream.
        Specified by:
        writeObject in interface Serializer<java.lang.Object>
        Parameters:
        object - The root object to be written.
        outputStream - The output stream to which data will be written.
        Throws:
        java.io.IOException - for any errors during the writing process.
        SerializationException - for any formatting errors in the data.
        See Also:
        writeObject(Object, Writer)
      • writeObject

        public void writeObject​(java.lang.Object object,
                                java.io.Writer writer)
                         throws java.io.IOException,
                                SerializationException
        Writes data to a JSON stream.
        Parameters:
        object - The object to serialize. Must be one of the following types:
        • pivot.collections.Map
        • pivot.collections.List
        • java.lang.String
        • java.lang.Number
        • java.lang.Boolean
        • null
        writer - The writer to which data will be written.
        Throws:
        java.io.IOException - for any errors during the writing process.
        SerializationException - for any formatting errors in the data.
      • getMIMEType

        public java.lang.String getMIMEType​(java.lang.Object object)
        Description copied from interface: Serializer
        Returns the MIME type of the data read and written by this serializer.
        Specified by:
        getMIMEType in interface Serializer<java.lang.Object>
        Parameters:
        object - If provided, allows the serializer to attach parameters to the returned MIME type containing more detailed information about the data. If null, the base MIME type is returned.
        Returns:
        The MIME type of the current data.
      • parse

        public static java.lang.Object parse​(java.lang.String json)
                                      throws SerializationException
        Converts a JSON value to a Java object.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed object.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseString

        public static java.lang.String parseString​(java.lang.String json)
                                            throws SerializationException
        Converts a JSON value to a string.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed string.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseNumber

        public static java.lang.Number parseNumber​(java.lang.String json)
                                            throws SerializationException
        Converts a JSON value to a number.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed number.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseShort

        public static java.lang.Short parseShort​(java.lang.String json)
                                          throws SerializationException
        Converts a JSON value to a short.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed short.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseInteger

        public static java.lang.Integer parseInteger​(java.lang.String json)
                                              throws SerializationException
        Converts a JSON value to a integer.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed integer.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseLong

        public static java.lang.Long parseLong​(java.lang.String json)
                                        throws SerializationException
        Converts a JSON value to a long.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed number.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseFloat

        public static java.lang.Float parseFloat​(java.lang.String json)
                                          throws SerializationException
        Converts a JSON value to a float.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed float.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseDouble

        public static java.lang.Double parseDouble​(java.lang.String json)
                                            throws SerializationException
        Converts a JSON value to a double.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed double.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseBoolean

        public static java.lang.Boolean parseBoolean​(java.lang.String json)
                                              throws SerializationException
        Converts a JSON value to a boolean.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed boolean.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseList

        public static List<?> parseList​(java.lang.String json)
                                 throws SerializationException
        Converts a JSON value to a list.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed list.
        Throws:
        SerializationException - for any formatting errors in the data.
      • parseMap

        public static Map<java.lang.String,​?> parseMap​(java.lang.String json)
                                                      throws SerializationException
        Converts a JSON value to a map.
        Parameters:
        json - The JSON value.
        Returns:
        The parsed map.
        Throws:
        SerializationException - for any formatting errors in the data.
      • toString

        public static java.lang.String toString​(java.lang.Object value)
                                         throws SerializationException
        Converts a object to a JSON string representation. The map keys will always be quote-delimited.
        Parameters:
        value - The object to convert.
        Returns:
        The resulting JSON string.
        Throws:
        SerializationException - for any formatting errors in the data.
        See Also:
        toString(Object, boolean)
      • toString

        public static java.lang.String toString​(java.lang.Object value,
                                                boolean alwaysDelimitMapKeys)
                                         throws SerializationException
        Converts a object to a JSON string representation.
        Parameters:
        value - The object to convert.
        alwaysDelimitMapKeys - A flag indicating whether or not map keys will always be quote-delimited.
        Returns:
        The resulting JSON string.
        Throws:
        SerializationException - for any formatting errors in the data.