Package com.github.fmjsjx.libcommon.json
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA JSON exception threw by theFastjson2Library. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newFastjson2Librarywith the default features.Fastjson2Library(com.alibaba.fastjson2.JSONReader.Feature[] readerFeatures, com.alibaba.fastjson2.JSONWriter.Feature[] writerFeatures) Creates a newFastjson2Librarywith the specified features given. -
Method Summary
Modifier and TypeMethodDescription<T,C extends Collection<T>>
com.alibaba.fastjson2.TypeReference<C>collectionTypeReference(Type elementsType, Class<? extends Collection<?>> collectionType) CreateTypeReferenceforCollections.static final Fastjson2LibraryReturns the default (singleton)Fastjson2Libraryinstance.voiddumps(Object obj, OutputStream out) byte[]dumpsToBytes(Object obj) dumpsToString(Object obj) <T> com.alibaba.fastjson2.TypeReference<T>genericTypeReference(Class<?> rawType, Type... typeArguments) CreateTypeReference.static final Fastjson2LibraryReturns the singleton (default)Fastjson2Libraryinstance.<T> com.alibaba.fastjson2.TypeReference<List<T>>listTypeReference(Type elementsType) CreateTypeReferenceforLists.<T extends com.alibaba.fastjson2.JSONObject>
Tloads(byte[] src) <T> Tloads(byte[] src, com.alibaba.fastjson2.TypeReference<T> type) Decodes data from byte array.<T> T<T> Tcom.alibaba.fastjson2.JSONObjectloads(InputStream src) Decodes data from input stream.<T> Tloads(InputStream src, Type type) Decodes data from input stream.<T extends com.alibaba.fastjson2.JSONObject>
T<T> TDecodes data from string.<T> T<T> Tcom.alibaba.fastjson2.JSONArrayloadsArray(byte[] src) DecodesJSONArrayfrom byte array.com.alibaba.fastjson2.JSONArrayloadsArray(InputStream src) DecodesJSONArrayfrom input stream.com.alibaba.fastjson2.JSONArrayloadsArray(String src) DecodesJSONArrayfrom string.<T> List<T>DecodesListfrom byte array.<T> List<T>DecodesListfrom byte array.<T> List<T>DecodesListfrom string.<T> List<T>DecodesListfrom string.<K,V> com.alibaba.fastjson2.TypeReference<Map<K, V>> mapTypeReference(Type keyType, Type valueType) CreateTypeReferenceforMaps.<K,V, M extends Map<K, V>>
com.alibaba.fastjson2.TypeReference<M>mapTypeReference(Type keyType, Type valueType, Class<? extends Map<?, ?>> mapType) CreateTypeReferenceforMaps.com.alibaba.fastjson2.JSONReader.Feature[]Returns the reader features.com.alibaba.fastjson2.JSONWriter.Feature[]Returns the writer features.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.github.fmjsjx.libcommon.json.JsonEncoder
dumps
-
Constructor Details
-
Fastjson2Library
public Fastjson2Library(com.alibaba.fastjson2.JSONReader.Feature[] readerFeatures, com.alibaba.fastjson2.JSONWriter.Feature[] writerFeatures) Creates a newFastjson2Librarywith the specified features given.- Parameters:
readerFeatures- the reader featureswriterFeatures- the writer features
-
Fastjson2Library
public Fastjson2Library()Creates a newFastjson2Librarywith the default features.Default read features:
none
Default write features:-
WriteNonStringKeyAsString
-
-
Method Details
-
getInstance
Returns the singleton (default)Fastjson2Libraryinstance.- Returns:
- the singleton (default)
Fastjson2Libraryinstance.
-
defaultInstance
Returns the default (singleton)Fastjson2Libraryinstance.This method is equivalent to
getInstance().- Returns:
- the default (singleton)
Fastjson2Libraryinstance.
-
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:
loadsin interfacecom.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:
loadsin interfacecom.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
- Specified by:
loadsin interfacecom.github.fmjsjx.libcommon.json.JsonDecoder<com.alibaba.fastjson2.JSONObject>- Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
loads
- Specified by:
loadsin interfacecom.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 arraytype- the type of the data- Returns:
- a data object as given type
- Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
loads
- Specified by:
loadsin interfacecom.github.fmjsjx.libcommon.json.JsonDecoder<com.alibaba.fastjson2.JSONObject>- Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
loads
- Specified by:
loadsin interfacecom.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 stringtype- the type of the data- Returns:
- a data object as given type
- Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
loads
Decodes data from input stream.- Type Parameters:
T- the type of the data- Parameters:
src- the source input streamtype- 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 DecodesJSONArrayfrom 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 DecodesJSONArrayfrom 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 DecodesJSONArrayfrom input stream.- Parameters:
src- the source input stream- Returns:
- a
JSONArray - Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
loadsList
DecodesListfrom byte array.- Type Parameters:
T- the type of the element- Parameters:
src- the source byte arraytype- the type of the element- Returns:
- a
List<T> - Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
loadsList
DecodesListfrom byte array.- Type Parameters:
T- the type of the element- Parameters:
src- the source byte arraytype- the type of the element- Returns:
- a
List<T> - Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
loadsList
DecodesListfrom string.- Type Parameters:
T- the type of the element- Parameters:
src- the source stringtype- the type of the element- Returns:
- a
List<T> - Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
loadsList
DecodesListfrom string.- Type Parameters:
T- the type of the element- Parameters:
src- the source stringtype- the type of the element- Returns:
- a
List<T> - Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
dumpsToBytes
- Specified by:
dumpsToBytesin interfacecom.github.fmjsjx.libcommon.json.JsonEncoder- Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
dumpsToString
- Specified by:
dumpsToStringin interfacecom.github.fmjsjx.libcommon.json.JsonEncoder- Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
dumps
- Specified by:
dumpsin interfacecom.github.fmjsjx.libcommon.json.JsonEncoder- Throws:
Fastjson2Library.Fastjson2Exception- if any JSON decode error occurs
-
listTypeReference
CreateTypeReferenceforLists.- 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) CreateTypeReferenceforCollections.- Type Parameters:
T- the type of the elementsC- the type of the collection- Parameters:
elementsType- the elements type of the collectioncollectionType- 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) CreateTypeReferenceforMaps.- Type Parameters:
K- the type of the keyV- the type of the value- Parameters:
keyType- the type of the keyvalueType- the type of the value- Returns:
- a
TypeReference<Map<K, V>>
-
mapTypeReference
public <K,V, com.alibaba.fastjson2.TypeReference<M> mapTypeReferenceM extends Map<K, V>> (Type keyType, Type valueType, Class<? extends Map<?, ?>> mapType) CreateTypeReferenceforMaps.- Type Parameters:
K- the type of the keyV- the type of the valueM- the type of the map- Parameters:
keyType- the type of the keyvalueType- the type of the valuemapType- the type of the map- Returns:
- a
TypeReference<M>
-
genericTypeReference
public <T> com.alibaba.fastjson2.TypeReference<T> genericTypeReference(Class<?> rawType, Type... typeArguments) CreateTypeReference.- Type Parameters:
T- the type- Parameters:
rawType- the raw typetypeArguments- the type arguments- Returns:
- a
TypeReference<T>
-