Class Fastjson2Library

java.lang.Object
com.github.fmjsjx.libcommon.json.Fastjson2Library
All Implemented Interfaces:
com.github.fmjsjx.libcommon.json.JsonDecoder<com.alibaba.fastjson2.JSONObject>, com.github.fmjsjx.libcommon.json.JsonEncoder, com.github.fmjsjx.libcommon.json.JsonLibrary<com.alibaba.fastjson2.JSONObject>

public class Fastjson2Library extends Object implements com.github.fmjsjx.libcommon.json.JsonLibrary<com.alibaba.fastjson2.JSONObject>
The implementation of JsonLibrary using Fastjson2.
Since:
3.4
  • Constructor Details

    • Fastjson2Library

      public Fastjson2Library(com.alibaba.fastjson2.JSONReader.Feature[] readerFeatures, com.alibaba.fastjson2.JSONWriter.Feature[] writerFeatures)
      Creates a new Fastjson2Library with the specified features given.
      Parameters:
      readerFeatures - the reader features
      writerFeatures - the writer features
    • Fastjson2Library

      public Fastjson2Library()
      Creates a new Fastjson2Library with the default features.

      Default read features:

      none
      Default write features:
      - WriteNonStringKeyAsString
  • Method Details

    • getInstance

      public static final Fastjson2Library getInstance()
      Returns the singleton (default) Fastjson2Library instance.
      Returns:
      the singleton (default) Fastjson2Library instance.
    • defaultInstance

      public static final Fastjson2Library defaultInstance()
      Returns the default (singleton) Fastjson2Library instance.

      This method is equivalent to getInstance().

      Returns:
      the default (singleton) Fastjson2Library instance.
    • readerFeatures

      public com.alibaba.fastjson2.JSONReader.Feature[] readerFeatures()
      Returns the reader features.
      Returns:
      an array of JSONReader.Feature
    • writerFeatures

      public com.alibaba.fastjson2.JSONWriter.Feature[] writerFeatures()
      Returns the writer features.
      Returns:
      an array of JSONWriter.Feature
    • loads

      public <T extends com.alibaba.fastjson2.JSONObject> T loads(byte[] src) throws Fastjson2Library.Fastjson2Exception
      Specified by:
      loads in interface com.github.fmjsjx.libcommon.json.JsonDecoder<com.alibaba.fastjson2.JSONObject>
      Returns:
      a JSONObject
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loads

      public <T extends com.alibaba.fastjson2.JSONObject> T loads(String src) throws Fastjson2Library.Fastjson2Exception
      Specified by:
      loads in interface com.github.fmjsjx.libcommon.json.JsonDecoder<com.alibaba.fastjson2.JSONObject>
      Returns:
      a JSONObject
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loads

      public com.alibaba.fastjson2.JSONObject loads(InputStream src) throws Fastjson2Library.Fastjson2Exception
      Decodes data from input stream.
      Parameters:
      src - the source input stream
      Returns:
      a JSONArray
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loads

      public <T> T loads(byte[] src, Class<T> type) throws Fastjson2Library.Fastjson2Exception
      Specified by:
      loads in interface com.github.fmjsjx.libcommon.json.JsonDecoder<com.alibaba.fastjson2.JSONObject>
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loads

      public <T> T loads(byte[] src, Type type) throws Fastjson2Library.Fastjson2Exception
      Specified by:
      loads in interface com.github.fmjsjx.libcommon.json.JsonDecoder<com.alibaba.fastjson2.JSONObject>
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loads

      public <T> T loads(byte[] src, com.alibaba.fastjson2.TypeReference<T> type) throws Fastjson2Library.Fastjson2Exception
      Decodes data from byte array.
      Type Parameters:
      T - the type of the data
      Parameters:
      src - the source byte array
      type - the type of the data
      Returns:
      a data object as given type
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loads

      public <T> T loads(String src, Class<T> type) throws Fastjson2Library.Fastjson2Exception
      Specified by:
      loads in interface com.github.fmjsjx.libcommon.json.JsonDecoder<com.alibaba.fastjson2.JSONObject>
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loads

      public <T> T loads(String src, Type type) throws Fastjson2Library.Fastjson2Exception
      Specified by:
      loads in interface com.github.fmjsjx.libcommon.json.JsonDecoder<com.alibaba.fastjson2.JSONObject>
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loads

      public <T> T loads(String src, com.alibaba.fastjson2.TypeReference<T> type) throws Fastjson2Library.Fastjson2Exception
      Decodes data from string.
      Type Parameters:
      T - the type of the data
      Parameters:
      src - the source string
      type - the type of the data
      Returns:
      a data object as given type
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loads

      public <T> T loads(InputStream src, Type type) throws Fastjson2Library.Fastjson2Exception
      Decodes data from input stream.
      Type Parameters:
      T - the type of the data
      Parameters:
      src - the source input stream
      type - the type of the data
      Returns:
      a data object as given type
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loadsArray

      public com.alibaba.fastjson2.JSONArray loadsArray(byte[] src) throws Fastjson2Library.Fastjson2Exception
      Decodes JSONArray from byte array.
      Parameters:
      src - the source byte array
      Returns:
      a JSONArray
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loadsArray

      public com.alibaba.fastjson2.JSONArray loadsArray(String src) throws Fastjson2Library.Fastjson2Exception
      Decodes JSONArray from string.
      Parameters:
      src - the source string
      Returns:
      a JSONArray
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loadsArray

      public com.alibaba.fastjson2.JSONArray loadsArray(InputStream src) throws Fastjson2Library.Fastjson2Exception
      Decodes JSONArray from input stream.
      Parameters:
      src - the source input stream
      Returns:
      a JSONArray
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loadsList

      public <T> List<T> loadsList(byte[] src, Type type) throws Fastjson2Library.Fastjson2Exception
      Decodes List from byte array.
      Type Parameters:
      T - the type of the element
      Parameters:
      src - the source byte array
      type - the type of the element
      Returns:
      a List<T>
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loadsList

      public <T> List<T> loadsList(byte[] src, Class<T> type) throws Fastjson2Library.Fastjson2Exception
      Decodes List from byte array.
      Type Parameters:
      T - the type of the element
      Parameters:
      src - the source byte array
      type - the type of the element
      Returns:
      a List<T>
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loadsList

      public <T> List<T> loadsList(String src, Type type) throws Fastjson2Library.Fastjson2Exception
      Decodes List from string.
      Type Parameters:
      T - the type of the element
      Parameters:
      src - the source string
      type - the type of the element
      Returns:
      a List<T>
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • loadsList

      public <T> List<T> loadsList(String src, Class<T> type) throws Fastjson2Library.Fastjson2Exception
      Decodes List from string.
      Type Parameters:
      T - the type of the element
      Parameters:
      src - the source string
      type - the type of the element
      Returns:
      a List<T>
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • dumpsToBytes

      public byte[] dumpsToBytes(Object obj) throws Fastjson2Library.Fastjson2Exception
      Specified by:
      dumpsToBytes in interface com.github.fmjsjx.libcommon.json.JsonEncoder
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • dumpsToString

      public String dumpsToString(Object obj) throws Fastjson2Library.Fastjson2Exception
      Specified by:
      dumpsToString in interface com.github.fmjsjx.libcommon.json.JsonEncoder
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • dumps

      public void dumps(Object obj, OutputStream out) throws Fastjson2Library.Fastjson2Exception
      Specified by:
      dumps in interface com.github.fmjsjx.libcommon.json.JsonEncoder
      Throws:
      Fastjson2Library.Fastjson2Exception - if any JSON decode error occurs
    • listTypeReference

      public <T> com.alibaba.fastjson2.TypeReference<List<T>> listTypeReference(Type elementsType)
      Create TypeReference for Lists.
      Type Parameters:
      T - the type of the elements
      Parameters:
      elementsType - the elements type of the list
      Returns:
      a TypeReference<List<T>>
    • collectionTypeReference

      public <T, C extends Collection<T>> com.alibaba.fastjson2.TypeReference<C> collectionTypeReference(Type elementsType, Class<? extends Collection<?>> collectionType)
      Create TypeReference for Collections.
      Type Parameters:
      T - the type of the elements
      C - the type of the collection
      Parameters:
      elementsType - the elements type of the collection
      collectionType - the type of the collection
      Returns:
      a TypeReference<C>
    • mapTypeReference

      public <K, V> com.alibaba.fastjson2.TypeReference<Map<K,V>> mapTypeReference(Type keyType, Type valueType)
      Create TypeReference for Maps.
      Type Parameters:
      K - the type of the key
      V - the type of the value
      Parameters:
      keyType - the type of the key
      valueType - the type of the value
      Returns:
      a TypeReference<Map<K, V>>
    • mapTypeReference

      public <K, V, M extends Map<K, V>> com.alibaba.fastjson2.TypeReference<M> mapTypeReference(Type keyType, Type valueType, Class<? extends Map<?,?>> mapType)
      Create TypeReference for Maps.
      Type Parameters:
      K - the type of the key
      V - the type of the value
      M - the type of the map
      Parameters:
      keyType - the type of the key
      valueType - the type of the value
      mapType - the type of the map
      Returns:
      a TypeReference<M>
    • genericTypeReference

      public <T> com.alibaba.fastjson2.TypeReference<T> genericTypeReference(Class<?> rawType, Type... typeArguments)
      Create TypeReference.
      Type Parameters:
      T - the type
      Parameters:
      rawType - the raw type
      typeArguments - the type arguments
      Returns:
      a TypeReference<T>