org.springframework.http.converter.json

## Class AbstractJsonHttpMessageConverter

- java.lang.Object
- - org.springframework.http.converter.AbstractHttpMessageConverter<T>
  - - org.springframework.http.converter.AbstractGenericHttpMessageConverter<Object>
    - - org.springframework.http.converter.json.AbstractJsonHttpMessageConverter

- All Implemented Interfaces:
    GenericHttpMessageConverter<Object>, HttpMessageConverter<Object>
  Direct Known Subclasses:
    GsonHttpMessageConverter, JsonbHttpMessageConverter

      public abstract class AbstractJsonHttpMessageConverter
      extends AbstractGenericHttpMessageConverter<Object>

  Common base class for plain JSON converters, e.g. Gson and JSON-B.

  Note that the Jackson converters have a dedicated class hierarchy due to their multi-format support.

  Since:
    5.0
  Author:
    Juergen Hoeller
  See Also:
    `GsonHttpMessageConverter`, `JsonbHttpMessageConverter`, `readInternal(Type, Reader)`, `writeInternal(Object, Type, Writer)`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static Charset` | `DEFAULT_CHARSET` The default charset used by the converter.

    - ### Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter

      `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractJsonHttpMessageConverter()`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Object` | `read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)` Read an object of the given type form the given input message, and returns it.
    `protected Object` | `readInternal(Class<?> clazz, HttpInputMessage inputMessage)` Abstract template method that reads the actual object.
    `protected abstract Object` | `readInternal(Type resolvedType, Reader reader)` Template method that reads the JSON-bound object from the given `Reader`.
    `void` | `setJsonPrefix(String jsonPrefix)` Specify a custom prefix to use for JSON output.
    `void` | `setPrefixJson(boolean prefixJson)` Indicate whether the JSON output by this view should be prefixed with ")]}', ".
    `protected void` | `writeInternal(Object object, Type type, HttpOutputMessage outputMessage)` Abstract template method that writes the actual body.
    `protected abstract void` | `writeInternal(Object object, Type type, Writer writer)` Template method that writes the JSON-bound object to the given `Writer`.

    - ### Methods inherited from class org.springframework.http.converter.AbstractGenericHttpMessageConverter

      `canRead, canWrite, supports, write, writeInternal`

    - ### Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter

      `addDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.springframework.http.converter.HttpMessageConverter

      `canRead, canWrite, getSupportedMediaTypes, getSupportedMediaTypes, read, write`

- - ### Field Detail

    - #### DEFAULT_CHARSET

          public static final Charset DEFAULT_CHARSET

      The default charset used by the converter.

  - ### Constructor Detail

    - #### AbstractJsonHttpMessageConverter

          public AbstractJsonHttpMessageConverter()

  - ### Method Detail

    - #### setJsonPrefix

          public void setJsonPrefix(String jsonPrefix)

      Specify a custom prefix to use for JSON output. Default is none.
      See Also:
        `setPrefixJson(boolean)`

    - #### setPrefixJson

          public void setPrefixJson(boolean prefixJson)

      Indicate whether the JSON output by this view should be prefixed with ")]}', ". Default is `false`.

      Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. This prefix should be stripped before parsing the string as JSON.

      See Also:
        `setJsonPrefix(java.lang.String)`

    - #### read

          public final Object read(Type type,
                                   @Nullable
                                   Class<?> contextClass,
                                   HttpInputMessage inputMessage)
                            throws IOException,
                                   HttpMessageNotReadableException

      Description copied from interface: `GenericHttpMessageConverter`
      Read an object of the given type form the given input message, and returns it.
      Parameters:
        `type` - the (potentially generic) type of object to return. This type must have previously been passed to the `canRead` method of this interface, which must have returned `true`.
        `contextClass` - a context class for the target type, for example a class in which the target type appears in a method signature (can be `null`)
        `inputMessage` - the HTTP input message to read from
      Returns:
        the converted object
      Throws:
        `IOException` - in case of I/O errors
        `HttpMessageNotReadableException` - in case of conversion errors

    - #### readInternal

          protected final Object readInternal(Class<?> clazz,
                                              HttpInputMessage inputMessage)
                                       throws IOException,
                                              HttpMessageNotReadableException

      Description copied from class: `AbstractHttpMessageConverter`
      Abstract template method that reads the actual object. Invoked from `AbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)`.
      Specified by:
        `readInternal` in class `AbstractHttpMessageConverter<Object>`
      Parameters:
        `clazz` - the type of object to return
        `inputMessage` - the HTTP input message to read from
      Returns:
        the converted object
      Throws:
        `IOException` - in case of I/O errors
        `HttpMessageNotReadableException` - in case of conversion errors

    - #### writeInternal

          protected final void writeInternal(Object object,
                                             @Nullable
                                             Type type,
                                             HttpOutputMessage outputMessage)
                                      throws IOException,
                                             HttpMessageNotWritableException

      Description copied from class: `AbstractGenericHttpMessageConverter`
      Abstract template method that writes the actual body. Invoked from `AbstractGenericHttpMessageConverter.write(T, java.lang.reflect.Type, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)`.
      Specified by:
        `writeInternal` in class `AbstractGenericHttpMessageConverter<Object>`
      Parameters:
        `object` - the object to write to the output message
        `type` - the type of object to write (may be `null`)
        `outputMessage` - the HTTP output message to write to
      Throws:
        `IOException` - in case of I/O errors
        `HttpMessageNotWritableException` - in case of conversion errors

    - #### readInternal

          protected abstract Object readInternal(Type resolvedType,
                                                 Reader reader)
                                          throws Exception

      Template method that reads the JSON-bound object from the given `Reader`.
      Parameters:
        `resolvedType` - the resolved generic type
        `reader` - the Reader to use
      Returns:
        the JSON-bound object
      Throws:
        `Exception` - in case of read/parse failures

    - #### writeInternal

          protected abstract void writeInternal(Object object,
                                                @Nullable
                                                Type type,
                                                Writer writer)
                                         throws Exception

      Template method that writes the JSON-bound object to the given `Writer`.
      Parameters:
        `object` - the object to write to the output message
        `type` - the type of object to write (may be `null`)
        `writer` - the Writer to use
      Throws:
        `Exception` - in case of write failures