Class JsonSerializer

java.lang.Object
com.eatthepath.json.JsonSerializer

public class JsonSerializer extends Object

A JSON serializer is responsible for producing complete and legal JSON texts (objects or arrays). It can serialize a Map as a JSON object or any Collection or array as a JSON list. The keys of a Map are always serialized as strings. Values within a Map, Collection, or array are converted to JSON values as follows:

Mapping of Java types to JSON types
Object type JSON representation
null null
Map object
Collection list
byte[] list
short[] list
int[] list
long[] list
float[] list
double[] list
boolean[] list
char[] list
Object[] list
Number number (or null)
Boolean true or false (or null)
all others the result of obj.toString()
Author:
Jon Chambers
See Also:
  • Method Details

    • writeJsonText

      public static void writeJsonText(Map<?,?> map, Appendable out) throws IOException
      Writes the given Map as a JSON object to the given Appendable. Note that the keys of the given map will be represented as Strings (via their toString method) regardless of their actual type.
      Parameters:
      map - the map to write as a JSON text
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(Map<?,?> map)
      Returns the given Map as a JSON string. Note that the keys of the given map will be represented as Strings (via their toString method) regardless of their actual type.
      Parameters:
      map - the map to return as a JSON text
      Returns:
      a JSON string representing the given map
    • writeJsonText

      public static void writeJsonText(Collection<?> collection, Appendable out) throws IOException
      Writes the given Collection as a JSON list to the given Appendable.
      Parameters:
      collection - the collection to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(Collection<?> collection)
      Returns the given Collection as a JSON string.
      Parameters:
      collection - the collection to return as a JSON text
      Returns:
      a JSON string representing the given collection
    • writeJsonText

      public static void writeJsonText(byte[] array, Appendable out) throws IOException
      Writes the given array of bytes as a JSON list to the given Appendable.
      Parameters:
      array - the array to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(byte[] array)
      Returns the given array of bytes as a JSON string.
      Parameters:
      array - the array to return as a JSON text
      Returns:
      a JSON string representing the given array
    • writeJsonText

      public static void writeJsonText(short[] array, Appendable out) throws IOException
      Writes the given array of shorts as a JSON list to the given Appendable.
      Parameters:
      array - the array to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(short[] array)
      Returns the given array of shorts as a JSON string.
      Parameters:
      array - the array to return as a JSON text
      Returns:
      a JSON string representing the given array
    • writeJsonText

      public static void writeJsonText(int[] array, Appendable out) throws IOException
      Writes the given array of ints as a JSON list to the given Appendable.
      Parameters:
      array - the array to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(int[] array)
      Returns the given array of ints as a JSON string.
      Parameters:
      array - the array to return as a JSON text
      Returns:
      a JSON string representing the given array
    • writeJsonText

      public static void writeJsonText(long[] array, Appendable out) throws IOException
      Writes the given array of longs as a JSON list to the given Appendable.
      Parameters:
      array - the array to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(long[] array)
      Returns the given array of longs as a JSON string.
      Parameters:
      array - the array to return as a JSON text
      Returns:
      a JSON string representing the given array
    • writeJsonText

      public static void writeJsonText(float[] array, Appendable out) throws IOException
      Writes the given array of floats as a JSON list to the given Appendable.
      Parameters:
      array - the array to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(float[] array)
      Returns the given array of floats as a JSON string.
      Parameters:
      array - the array to return as a JSON text
      Returns:
      a JSON string representing the given array
    • writeJsonText

      public static void writeJsonText(double[] array, Appendable out) throws IOException
      Writes the given array of doubles as a JSON list to the given Appendable.
      Parameters:
      array - the array to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(double[] array)
      Returns the given array of doubles as a JSON string.
      Parameters:
      array - the array to return as a JSON text
      Returns:
      a JSON string representing the given array
    • writeJsonText

      public static void writeJsonText(boolean[] array, Appendable out) throws IOException
      Writes the given array of booleans as a JSON list to the given Appendable.
      Parameters:
      array - the array to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(boolean[] array)
      Returns the given array of booleans as a JSON string.
      Parameters:
      array - the array to return as a JSON text
      Returns:
      a JSON string representing the given array
    • writeJsonText

      public static void writeJsonText(char[] array, Appendable out) throws IOException
      Writes the given array of characters as a JSON list to the given Appendable.
      Parameters:
      array - the array to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(char[] array)
      Returns the given array of chars as a JSON string.
      Parameters:
      array - the array to return as a JSON text
      Returns:
      a JSON string representing the given array
    • writeJsonText

      public static void writeJsonText(Object[] array, Appendable out) throws IOException
      Writes the given array of Objects as a JSON list to the given Appendable.
      Parameters:
      array - the array to write as a JSON value
      out - the Appendable to which to write the JSON text
      Throws:
      IOException - in the event of an I/O error when writing to the given Appendable
    • writeJsonTextAsString

      public static String writeJsonTextAsString(Object[] array)
      Returns the given array of Objects as a JSON string.
      Parameters:
      array - the array to return as a JSON text
      Returns:
      a JSON string representing the given array