Class JSONCodec


  • public class JSONCodec
    extends Object
    An encoder of Java bean to a JSON representation and a decoder of JSON stream into the corresponding Java bean.

    In order to perform the marshalling and the unmarchalling, the fields of the bean must be annotated with the JAXB annotations. All null fields are by default ignored.

    Author:
    mmoquillon
    • Method Detail

      • encode

        public static <T> String encode​(T bean)
        Encodes the specified bean into a JSON representation.
        Type Parameters:
        T - the type of the bean.
        Parameters:
        bean - the bean to encode.
        Returns:
        the JSON representation of the bean in a String.
        Throws:
        EncodingException - if an error occurs while encoding a bean in JSON.
      • encodeObject

        public static String encodeObject​(UnaryOperator<JSONCodec.JSONObject> beanBuilder)
        Encodes the bean dynamically built by the specified builder. This method is just a convenient one to dynamically build a JSON representation of a simple bean. We recommend to represent the bean to serialize as a Java object and then to use the method org.silverpeas.core.util.JSONCodec#encode(T bean).
        Parameters:
        beanBuilder - a function that accepts as argument a JSONObject instance and that returns the JSONObject instance enriched with the attributes set by the function.
        Returns:
        the JSON representation of the bean in a String.
        Throws:
        EncodingException - if an error occurs while encoding a bean in JSON.
      • encodeArray

        public static String encodeArray​(UnaryOperator<JSONCodec.JSONArray> arrayBuilder)
        Encodes an array of beans that are dynamically built thank to the specified builder. This method is just a convenient one to dynamically build a JSON representation of an array of simple beans. We recommend to represent the bean to serialize as a Java object and then to use the method org.silverpeas.core.util.JSONCodec#encode(T bean).
        Parameters:
        arrayBuilder - a function that accepts as argument a JSONObject instance and that returns the JSONObject instance enriched with the attributes set by the function.
        Returns:
        the JSON representation of the bean in a String.
        Throws:
        EncodingException - if an error occurs while encoding a bean in JSON.
      • decode

        public static <T> T decode​(String json,
                                   Class<T> beanType)
        Decodes the specified JSON representation into its corresponding bean.
        Type Parameters:
        T - the type of the bean.
        Parameters:
        json - the JSON representation of a bean to decode.
        beanType - the class of the bean
        Returns:
        the bean decoded from JSON.
        Throws:
        EncodingException - if an error occurs while decoding a JSON String into a bean.
      • decode

        public static <T> T decode​(InputStream jsonStream,
                                   Class<T> beanType)
        Decodes the specified JSON representation into its corresponding bean.
        Type Parameters:
        T - the type of the bean.
        Parameters:
        jsonStream - a stream to a JSON representation of a bean to decode.
        beanType - the class of the bean
        Returns:
        the bean decoded from JSON.
        Throws:
        EncodingException - if an error occurs while decoding a JSON stream into a bean.