Class FastJsonProvider

  • All Implemented Interfaces:
    javax.ws.rs.ext.MessageBodyReader<Object>, javax.ws.rs.ext.MessageBodyWriter<Object>

    @Provider
    @Consumes("*/*")
    @Produces("*/*")
    public class FastJsonProvider
    extends Object
    implements javax.ws.rs.ext.MessageBodyReader<Object>, javax.ws.rs.ext.MessageBodyWriter<Object>
    Fastjson for JAX-RS Provider.
    Since:
    1.2.9
    Author:
    smallnest, VictorZeng
    See Also:
    MessageBodyReader, MessageBodyWriter
    • Field Detail

      • DEFAULT_UNREADABLES

        public static final Class<?>[] DEFAULT_UNREADABLES
        These are classes that we never use for reading (never try to deserialize instances of these types).
      • DEFAULT_UNWRITABLES

        public static final Class<?>[] DEFAULT_UNWRITABLES
        These are classes that we never use for writing (never try to serialize instances of these types).
      • providers

        @Context
        protected javax.ws.rs.ext.Providers providers
        Injectable context object used to locate configured instance of FastJsonConfig to use for actual serialization.
    • Constructor Detail

      • FastJsonProvider

        public FastJsonProvider()
        Can serialize/deserialize all types.
      • FastJsonProvider

        public FastJsonProvider​(Class<?>[] clazzes)
        Only serialize/deserialize all types in clazzes.
    • Method Detail

      • getFastJsonConfig

        public FastJsonConfig getFastJsonConfig()
        Returns:
        the fastJsonConfig.
        Since:
        1.2.11
      • setFastJsonConfig

        public void setFastJsonConfig​(FastJsonConfig fastJsonConfig)
        Parameters:
        fastJsonConfig - the fastJsonConfig to set.
        Since:
        1.2.11
      • setPretty

        public FastJsonProvider setPretty​(boolean p)
        Set pretty format
      • isAssignableFrom

        protected boolean isAssignableFrom​(Class<?> type,
                                           Class<?>[] classes)
        Check some are interface/abstract classes to exclude.
        Parameters:
        type - the type
        classes - the classes
        Returns:
        the boolean
      • isValidType

        protected boolean isValidType​(Class<?> type,
                                      Annotation[] classAnnotations)
        Check whether a class can be serialized or deserialized. It can check based on packages, annotations on entities or explicit classes.
        Parameters:
        type - class need to check
        Returns:
        true if valid
      • hasMatchingMediaType

        protected boolean hasMatchingMediaType​(javax.ws.rs.core.MediaType mediaType)
        Check media type like "application/json".
        Parameters:
        mediaType - media type
        Returns:
        true if the media type is valid
      • isWriteable

        public boolean isWriteable​(Class<?> type,
                                   Type genericType,
                                   Annotation[] annotations,
                                   javax.ws.rs.core.MediaType mediaType)
        Method that JAX-RS container calls to try to check whether given value (of specified type) can be serialized by this provider.
        Specified by:
        isWriteable in interface javax.ws.rs.ext.MessageBodyWriter<Object>
      • getSize

        public long getSize​(Object t,
                            Class<?> type,
                            Type genericType,
                            Annotation[] annotations,
                            javax.ws.rs.core.MediaType mediaType)
        Method that JAX-RS container calls to try to figure out serialized length of given value. always return -1 to denote "not known".
        Specified by:
        getSize in interface javax.ws.rs.ext.MessageBodyWriter<Object>
      • writeTo

        public void writeTo​(Object obj,
                            Class<?> type,
                            Type genericType,
                            Annotation[] annotations,
                            javax.ws.rs.core.MediaType mediaType,
                            javax.ws.rs.core.MultivaluedMap<String,​Object> httpHeaders,
                            OutputStream entityStream)
                     throws IOException,
                            javax.ws.rs.WebApplicationException
        Method that JAX-RS container calls to serialize given value.
        Specified by:
        writeTo in interface javax.ws.rs.ext.MessageBodyWriter<Object>
        Throws:
        IOException
        javax.ws.rs.WebApplicationException
      • isReadable

        public boolean isReadable​(Class<?> type,
                                  Type genericType,
                                  Annotation[] annotations,
                                  javax.ws.rs.core.MediaType mediaType)
        Method that JAX-RS container calls to try to check whether values of given type (and media type) can be deserialized by this provider.
        Specified by:
        isReadable in interface javax.ws.rs.ext.MessageBodyReader<Object>
      • readFrom

        public Object readFrom​(Class<Object> type,
                               Type genericType,
                               Annotation[] annotations,
                               javax.ws.rs.core.MediaType mediaType,
                               javax.ws.rs.core.MultivaluedMap<String,​String> httpHeaders,
                               InputStream entityStream)
                        throws IOException,
                               javax.ws.rs.WebApplicationException
        Method that JAX-RS container calls to deserialize given value.
        Specified by:
        readFrom in interface javax.ws.rs.ext.MessageBodyReader<Object>
        Throws:
        IOException
        javax.ws.rs.WebApplicationException
      • locateConfigProvider

        protected FastJsonConfig locateConfigProvider​(Class<?> type,
                                                      javax.ws.rs.core.MediaType mediaType)
        Helper method that is called if no config has been explicitly configured.