Class JsonObjectParser

  • All Implemented Interfaces:
    ObjectParser

    public class JsonObjectParser
    extends java.lang.Object
    implements ObjectParser
    Parses JSON data into an data class of key/value pairs.

    Implementation is thread-safe.

    Sample usage:

    
      static void setParser(HttpRequest request) {
        request.setParser(new JsonObjectParser(new JacksonFactory()));
      }
     
     
    Since:
    1.10
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private JsonFactory jsonFactory
      JSON factory.
      private java.util.Set<java.lang.String> wrapperKeys
      Wrapper keys for the JSON content or empty for none.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      JsonFactory getJsonFactory()
      Returns the JSON factory.
      java.util.Set<java.lang.String> getWrapperKeys()
      Returns the unmodifiable set of wrapper keys for the JSON content.
      private void initializeParser​(JsonParser parser)
      Initialize the parser to skip to wrapped keys (if any).
      <T> T parseAndClose​(java.io.InputStream in, java.nio.charset.Charset charset, java.lang.Class<T> dataClass)
      Parses the given input stream into a new instance of the the given data class of key/value pairs and closes the input stream.
      java.lang.Object parseAndClose​(java.io.InputStream in, java.nio.charset.Charset charset, java.lang.reflect.Type dataType)
      Parses the given input stream into a new instance of the the given data type of key/value pairs and closes the input stream.
      <T> T parseAndClose​(java.io.Reader reader, java.lang.Class<T> dataClass)
      Parses the given reader into a new instance of the the given data class of key/value pairs and closes the reader.
      java.lang.Object parseAndClose​(java.io.Reader reader, java.lang.reflect.Type dataType)
      Parses the given reader into a new instance of the the given data type of key/value pairs and closes the reader.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • jsonFactory

        private final JsonFactory jsonFactory
        JSON factory.
      • wrapperKeys

        private final java.util.Set<java.lang.String> wrapperKeys
        Wrapper keys for the JSON content or empty for none.
    • Constructor Detail

      • JsonObjectParser

        public JsonObjectParser​(JsonFactory jsonFactory)
        Parameters:
        jsonFactory - JSON factory
      • JsonObjectParser

        protected JsonObjectParser​(JsonObjectParser.Builder builder)
        Parameters:
        builder - builder
        Since:
        1.14
    • Method Detail

      • parseAndClose

        public <T> T parseAndClose​(java.io.InputStream in,
                                   java.nio.charset.Charset charset,
                                   java.lang.Class<T> dataClass)
                            throws java.io.IOException
        Description copied from interface: ObjectParser
        Parses the given input stream into a new instance of the the given data class of key/value pairs and closes the input stream.
        Specified by:
        parseAndClose in interface ObjectParser
        Parameters:
        in - input stream which contains the data to parse
        charset - charset which should be used to decode the input stream or null if unknown
        dataClass - class into which the data is parsed
        Throws:
        java.io.IOException
      • parseAndClose

        public java.lang.Object parseAndClose​(java.io.InputStream in,
                                              java.nio.charset.Charset charset,
                                              java.lang.reflect.Type dataType)
                                       throws java.io.IOException
        Description copied from interface: ObjectParser
        Parses the given input stream into a new instance of the the given data type of key/value pairs and closes the input stream.
        Specified by:
        parseAndClose in interface ObjectParser
        Parameters:
        in - input stream which contains the data to parse
        charset - charset which should be used to decode the input stream or null if unknown
        dataType - type into which the data is parsed
        Throws:
        java.io.IOException
      • parseAndClose

        public <T> T parseAndClose​(java.io.Reader reader,
                                   java.lang.Class<T> dataClass)
                            throws java.io.IOException
        Description copied from interface: ObjectParser
        Parses the given reader into a new instance of the the given data class of key/value pairs and closes the reader.
        Specified by:
        parseAndClose in interface ObjectParser
        Parameters:
        reader - reader which contains the text data to parse
        dataClass - class into which the data is parsed
        Throws:
        java.io.IOException
      • parseAndClose

        public java.lang.Object parseAndClose​(java.io.Reader reader,
                                              java.lang.reflect.Type dataType)
                                       throws java.io.IOException
        Description copied from interface: ObjectParser
        Parses the given reader into a new instance of the the given data type of key/value pairs and closes the reader.
        Specified by:
        parseAndClose in interface ObjectParser
        Parameters:
        reader - reader which contains the text data to parse
        dataType - type into which the data is parsed
        Throws:
        java.io.IOException
      • getJsonFactory

        public final JsonFactory getJsonFactory()
        Returns the JSON factory.
      • getWrapperKeys

        public java.util.Set<java.lang.String> getWrapperKeys()
        Returns the unmodifiable set of wrapper keys for the JSON content.
        Since:
        1.14
      • initializeParser

        private void initializeParser​(JsonParser parser)
                               throws java.io.IOException
        Initialize the parser to skip to wrapped keys (if any).
        Parameters:
        parser - JSON parser
        Throws:
        java.io.IOException