Class TypeReference<T>
java.lang.Object
com.alibaba.fastjson2.TypeReference<T>
- Type Parameters:
T- the type refered to
Represents a generic type
T.
Java doesn't yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at runtime.
This syntax cannot be used to create type literals that have wildcard
parameters, such as Class<T> or List<? extends CharSequence>.
For example, to create a type literal for List<String>, you can
create an empty anonymous inner class:
TypeReference<List<String>> typeReference = new TypeReference<List<String>>(){};
For example, use it quickly:
String text = "{\"id\":1,\"name\":\"kraity\"}";
User user = new TypeReference<User>(){}.parseObject(text);
- Since:
- 2.0.2
- Author:
- wenshao[szujobs@hotmail.com]
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new type literal.TypeReference(Type... actualTypeArguments) Constructs a new type literal with the specified actual type arguments. -
Method Summary
Modifier and TypeMethodDescriptionstatic TypeCreates an array type with the specified element type.static TypecollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass) Creates a collection type with the specified collection class and element class.static TypeReference<?> Gets a type reference for the specified type.Gets the raw type.final TypegetType()Gets the type literal.static TypeCreates a map type with the specified key class and value type.static TypeCreates a map type with the specified map class, key class and value class.static TypeCreates a multi-type from the specified types.static TypeparametricType(Class<?> parametrized, Class<?>... parameterClasses) Creates a parameterized type with the specified parametrized class and parameter classes.static TypeparametricType(Class<?> parametrized, Type... parameterTypes) Creates a parameterized type with the specified parametrized class and parameter types.parseArray(byte[] utf8Bytes, JSONReader.Feature... features) SeeJSON.parseArray(byte[], Type, JSONReader.Feature...)for detailsparseArray(String text, JSONReader.Feature... features) SeeJSON.parseArray(String, JSONReader.Feature...)for detailsparseObject(byte[] utf8Bytes) SeeJSON.parseObject(byte[], Type)for detailsparseObject(String text) SeeJSON.parseObject(String, Type)for detailsSeeJSONArray.to(Type)for detailsto(JSONObject object, JSONReader.Feature... features) SeeJSONObject.to(Type, JSONReader.Feature...)for detailstoJavaObject(JSONArray array) Deprecated.toJavaObject(JSONObject object, JSONReader.Feature... features) Deprecated.since 2.0.4, please useto(JSONObject, JSONReader.Feature...)
-
Field Details
-
type
-
rawType
-
-
Constructor Details
-
TypeReference
public TypeReference()Constructs a new type literal. Derives represented class from type parameter.Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy, so we can reconstitute it at runtime despite erasure.
-
TypeReference
Constructs a new type literal with the specified actual type arguments.For example:
Class<T> klass = ...; TypeReference<Response<T>> ref = new TypeReference<Response<T>>(new Type[]{klass}){};- Parameters:
actualTypeArguments- an array of Type objects representing the actual type arguments to this type- Throws:
NullPointerException- If theactualTypeArgumentsis null or empty- Since:
- 2.0.2
-
-
Method Details
-
getType
-
getRawType
-
parseObject
SeeJSON.parseObject(String, Type)for detailsString text = "{\"id\":1,\"name\":\"kraity\"}"; User user = new TypeReference<User>(){}.parseObject(text);- Parameters:
text- the JSONStringto be parsed- Since:
- 2.0.2
-
parseObject
SeeJSON.parseObject(byte[], Type)for detailsString utf8Bytes = "{\"id\":1,\"name\":\"kraity\"}".getBytes(StandardCharsets.UTF_8); User user = new TypeReference<User>(){}.parseObject(utf8Bytes);- Parameters:
utf8Bytes- UTF8 encoded JSON byte array to parse- Since:
- 2.0.3
-
parseArray
SeeJSON.parseArray(String, JSONReader.Feature...)for detailsString text = "[{\"id\":1,\"name\":\"kraity\"}]"; List<User> users = new TypeReference<User>(){}.parseArray(text);- Parameters:
text- the JSONStringto be parsedfeatures- features to be enabled in parsing- Since:
- 2.0.2
-
parseArray
SeeJSON.parseArray(byte[], Type, JSONReader.Feature...)for detailsString utf8Bytes = "[{\"id\":1,\"name\":\"kraity\"}]".getBytes(StandardCharsets.UTF_8); List<User> users = new TypeReference<User>(){}.parseArray(utf8Bytes);- Parameters:
utf8Bytes- UTF8 encoded JSON byte array to parsefeatures- features to be enabled in parsing- Since:
- 2.0.3
-
to
SeeJSONArray.to(Type)for detailsJSONArray array = ... List<User> users = new TypeReference<ArrayList<User>>(){}.to(array);- Parameters:
array- specify theJSONArrayto convert- Since:
- 2.0.4
-
to
SeeJSONObject.to(Type, JSONReader.Feature...)for detailsJSONObject object = ... Map<String, User> users = new TypeReference<HashMap<String, User>>(){}.to(object);- Parameters:
object- specify theJSONObjectto convertfeatures- features to be enabled in parsing- Since:
- 2.0.4
-
toJavaObject
Deprecated.since 2.0.4, please useto(JSONArray)SeeJSONArray.toJavaObject(Type)for details- Parameters:
array- specify theJSONArrayto convert
-
toJavaObject
Deprecated.since 2.0.4, please useto(JSONObject, JSONReader.Feature...)SeeJSONObject.to(Type, JSONReader.Feature...)for details- Parameters:
object- specify theJSONObjectto convertfeatures- features to be enabled in parsing
-
get
Gets a type reference for the specified type.- Parameters:
type- specify theTypeto be converted- Returns:
- the type reference
-
of
-
collectionType
public static Type collectionType(Class<? extends Collection> collectionClass, Class<?> elementClass) Creates a collection type with the specified collection class and element class.- Parameters:
collectionClass- the collection classelementClass- the element class- Returns:
- the parameterized type
-
arrayType
-
mapType
-
mapType
-
parametricType
-
parametricType
-
to(JSONArray)