Class PropertiesSerializer

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

public final class PropertiesSerializer extends Object
A data serializer for the Java properties file format. The mapping to the properties format 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, Integer, Boolean, Date, Array and Dict values are supported.
  • Other value types are converted to strings.
  • Property key names should consist of printable ISO-8859-1 characters, without spaces or dots.
  • Property key names should not be numeric, as that will convert to an array index.
  • Array null values will be omitted, renumbering the remaining indices.
The structure and order of dictionary objects will be kept, using the file order in the properties file and dot-notation for property names.
Version:
1.0
  • Method Details

    • serialize

      public static void serialize(Object obj, OutputStream os) throws IOException
      Serializes an object into a properties representation.
      Parameters:
      obj - the object to serialize, 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) throws IOException
      Serializes an object into a properties representation.
      Parameters:
      obj - the object to serialize, or null
      Returns:
      a properties file representation
      Throws:
      IOException - if the data couldn't be serialized
    • unserialize

      public static Object unserialize(InputStream is) throws IOException
      Unserializes an object from a properties representation, typically a properties file. The property names are preserved as dictionary keys, except for structural interpretation. Dot ('.') characters in names are interpreted as sub-object separators, and numbers are interpreted as an array indices. Values are stored as booleans, integers or strings.
      Parameters:
      is - the input stream to load
      Returns:
      the object read
      Throws:
      IOException - if an error occurred while reading
    • write

      public static void write(File file, Object obj) throws IOException
      Serializes an object into a properties file.
      Parameters:
      file - the file to write
      obj - the object value, or null
      Throws:
      IOException - if the data couldn't be serialized