Class JodaBeanJsonWriter


  • public class JodaBeanJsonWriter
    extends Object
    Provides the ability for a Joda-Bean to be written to JSON.

    This class contains mutable state and cannot be used from multiple threads. A new instance must be created for each message.

    The JSON format is kept relatively natural, however some meta-data is added. This has the unfortunate effect of adding an additional object structure to hold the type in a few places.

    Beans are output using JSON objects where the key is the property name. The type of the bean will be sent using the '@type' property name if necessary.

    Most simple types, defined by Joda-Convert, are output as JSON strings. If the simple type requires additional type information, the value is replaced by a JSON object containing the keys '@type' and 'value'.

    Null values are generally omitted, but where included are sent as 'null'. Boolean values are sent as 'true' and 'false'. Integer and Double values are sent as JSON numbers. Other numeric types are also sent as numbers but may have additional type information.

    Collections are output using JSON objects or arrays. Multisets are output as a map of value to count.

    If a collection contains a collection then addition meta-type information is written to aid with deserialization. At this level, the data read back may not be identical to that written. If the collection type requires additional type information, the value is replaced by a JSON object containing the keys '@meta' and 'value'.

    Type names are shortened by the package of the root type if possible. Certain basic types are also handled, such as String, Integer, File and URI.

    • Constructor Detail

      • JodaBeanJsonWriter

        public JodaBeanJsonWriter​(JodaBeanSer settings)
        Creates an instance.
        Parameters:
        settings - the settings to use, not null
    • Method Detail

      • write

        public String write​(Bean bean)
        Writes the bean to a string.

        The type of the bean will be set in the message.

        Parameters:
        bean - the bean to output, not null
        Returns:
        the JSON, not null
      • write

        public String write​(Bean bean,
                            boolean rootType)
        Writes the bean to a string specifying whether to include the type at the root.
        Parameters:
        bean - the bean to output, not null
        rootType - true to output the root type
        Returns:
        the JSON, not null
      • write

        public void write​(Bean bean,
                          Appendable output)
                   throws IOException
        Writes the bean to the Appendable.

        The type of the bean will be set in the message.

        Parameters:
        bean - the bean to output, not null
        output - the output appendable, not null
        Throws:
        IOException - if an error occurs
      • write

        public void write​(Bean bean,
                          boolean rootType,
                          Appendable output)
                   throws IOException
        Writes the bean to the Appendable specifying whether to include the type at the root.
        Parameters:
        bean - the bean to output, not null
        rootType - true to output the root type
        output - the output appendable, not null
        Throws:
        IOException - if an error occurs