Class JacksonUtil


  • public class JacksonUtil
    extends Object
    Utility class providing static method to help serializing and de-serializing complex object hierarchies with Jackson. To get the current sub-tree of the JSON as JsonNode in a JsonDeserializer, you can simply use the following code:
     jp.getCodec().readTree(jp)
     
    Since:
    3.0.0
    • Constructor Detail

      • JacksonUtil

        public JacksonUtil()
    • Method Detail

      • writeValue

        public static void writeValue​(com.fasterxml.jackson.core.JsonGenerator jgen,
                                      Object value)
                               throws IOException
        Parameters:
        jgen - the JsonGenerator.
        value - the value to write depending on its type.
        Throws:
        IOException - in case of an I/O error.
      • writeNumber

        private static void writeNumber​(com.fasterxml.jackson.core.JsonGenerator jgen,
                                        Number value)
                                 throws IOException
        Throws:
        IOException
      • readValue

        public static <V> V readValue​(com.fasterxml.jackson.databind.JsonNode node,
                                      String fieldName,
                                      Class<V> type,
                                      boolean required)
        Type Parameters:
        V - the generic type of the value to read.
        Parameters:
        node - the JsonNode to parse.
        fieldName - the name of the JSON property to read from the JsonNode.
        type - the Class reflecting the type of the requested value.
        required - - true if the value is required and an exception shall be thrown if missing, false otherwise (make it optional and return null if not present).
        Returns:
        the parsed value. May be null if required is false.
        Throws:
        RuntimeException - in case of an error.
      • readValue

        public static <V> V readValue​(com.fasterxml.jackson.databind.JsonNode node,
                                      Class<V> type,
                                      boolean required)
        Type Parameters:
        V - the generic type of the value to read.
        Parameters:
        node - the JsonNode to parse.
        type - the Class reflecting the type of the requested value.
        required - - true if the value is required and an exception shall be thrown if missing, false otherwise (make it optional and return null if not present).
        Returns:
        the parsed value. May be null if required is false.
        Throws:
        RuntimeException - in case of an error.
      • readValue

        public static <V> V readValue​(com.fasterxml.jackson.databind.JsonNode node,
                                      Class<V> type)
        Type Parameters:
        V - the generic type of the value to read.
        Parameters:
        node - the JsonNode to parse.
        type - the Class reflecting the type of the requested value.
        Returns:
        the parsed value. May be null if required is false.
        Throws:
        RuntimeException - in case of an error.
      • readAtomicValue

        private static <V> V readAtomicValue​(com.fasterxml.jackson.databind.JsonNode node,
                                             Class<V> type)