Class JsonSerializer

java.lang.Object
org.rapidcontext.core.data.JsonSerializer

public final class JsonSerializer extends Object
A data serializer and unserializer for the JSON format. The object mapping to JSON is not exact, and may omit serialization of data in some cases. The following basic requirements must be met in order to serialize an object:
  • No circular references are permitted.
  • String, Number, Boolean, Date, Array and Dict values are supported.
Version:
1.0
  • Method Details

    • serialize

      public static void serialize(Object obj, OutputStream os) throws IOException
      Serializes an object into a JSON representation.
      Parameters:
      obj - the object to convert, or null
      os - the output stream to write to
      Throws:
      IOException - if the data couldn't be serialized
    • serialize

      public static String serialize(Object obj, boolean indent)
      Serializes an object into a JSON representation. If the indentation flag is set, the JSON data will be indented and formatted. Otherwise a minimal string will be returned.
      Parameters:
      obj - the object to convert, or null
      indent - the indentation flag
      Returns:
      a JSON data representation
    • unserialize

      public static Object unserialize(InputStream is) throws IOException
      Unserializes JSON data into a Java object. Returns the corresponding String, Number, Boolean, Dict or Array value.
      Parameters:
      is - the input stream to load
      Returns:
      the Java data representation
      Throws:
      IOException - if the unserialization failed
    • unserialize

      public static Object unserialize(String json) throws IOException
      Unserializes JSON data into a Java object. Returns the corresponding String, Number, Boolean, Dict or Array value.
      Parameters:
      json - the JSON data to convert
      Returns:
      the Java data representation
      Throws:
      IOException - if the unserialization failed