Class TypeReference<T>

java.lang.Object
com.alibaba.fastjson2.TypeReference<T>
Type Parameters:
T - the type refered to

public abstract class TypeReference<T> extends Object
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 Details

    • type

      protected final Type type
    • rawType

      protected final Class<? super T> 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

      public TypeReference(Type... actualTypeArguments)
      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 the actualTypeArguments is null or empty
      Since:
      2.0.2
  • Method Details

    • getType

      public final Type getType()
      Gets the type literal.
      Returns:
      the Type
    • getRawType

      public final Class<? super T> getRawType()
      Gets the raw type.
      Returns:
      the raw Class
    • parseObject

      public T parseObject(String text)
      See JSON.parseObject(String, Type) for details
      
       String text = "{\"id\":1,\"name\":\"kraity\"}";
       User user = new TypeReference<User>(){}.parseObject(text);
       
      Parameters:
      text - the JSON String to be parsed
      Since:
      2.0.2
    • parseObject

      public T parseObject(byte[] utf8Bytes)
      See JSON.parseObject(byte[], Type) for details
      
       String 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

      public List<T> parseArray(String text, JSONReader.Feature... features)
      See JSON.parseArray(String, JSONReader.Feature...) for details
      
       String text = "[{\"id\":1,\"name\":\"kraity\"}]";
       List<User> users = new TypeReference<User>(){}.parseArray(text);
       
      Parameters:
      text - the JSON String to be parsed
      features - features to be enabled in parsing
      Since:
      2.0.2
    • parseArray

      public List<T> parseArray(byte[] utf8Bytes, JSONReader.Feature... features)
      See JSON.parseArray(byte[], Type, JSONReader.Feature...) for details
      
       String 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 parse
      features - features to be enabled in parsing
      Since:
      2.0.3
    • to

      public T to(JSONArray array)
      See JSONArray.to(Type) for details
      
       JSONArray array = ...
       List<User> users = new TypeReference<ArrayList<User>>(){}.to(array);
       
      Parameters:
      array - specify the JSONArray to convert
      Since:
      2.0.4
    • to

      public T to(JSONObject object, JSONReader.Feature... features)
      See JSONObject.to(Type, JSONReader.Feature...) for details
      
       JSONObject object = ...
       Map<String, User> users = new TypeReference<HashMap<String, User>>(){}.to(object);
       
      Parameters:
      object - specify the JSONObject to convert
      features - features to be enabled in parsing
      Since:
      2.0.4
    • toJavaObject

      @Deprecated public T toJavaObject(JSONArray array)
      Deprecated.
      since 2.0.4, please use to(JSONArray)
      Parameters:
      array - specify the JSONArray to convert
    • toJavaObject

      @Deprecated public T toJavaObject(JSONObject object, JSONReader.Feature... features)
      Deprecated.
      Parameters:
      object - specify the JSONObject to convert
      features - features to be enabled in parsing
    • get

      public static TypeReference<?> get(Type type)
      Gets a type reference for the specified type.
      Parameters:
      type - specify the Type to be converted
      Returns:
      the type reference
    • of

      public static Type of(Type... types)
      Creates a multi-type from the specified types.
      Parameters:
      types - the types
      Returns:
      the multi-type
    • 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 class
      elementClass - the element class
      Returns:
      the parameterized type
    • arrayType

      public static Type arrayType(Class<?> elementType)
      Creates an array type with the specified element type.
      Parameters:
      elementType - the element type
      Returns:
      the generic array type
    • mapType

      public static Type mapType(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass)
      Creates a map type with the specified map class, key class and value class.
      Parameters:
      mapClass - the map class
      keyClass - the key class
      valueClass - the value class
      Returns:
      the parameterized type
    • mapType

      public static Type mapType(Class<?> keyClass, Type valueType)
      Creates a map type with the specified key class and value type.
      Parameters:
      keyClass - the key class
      valueType - the value type
      Returns:
      the parameterized type
    • parametricType

      public static Type parametricType(Class<?> parametrized, Class<?>... parameterClasses)
      Creates a parameterized type with the specified parametrized class and parameter classes.
      Parameters:
      parametrized - the parametrized class
      parameterClasses - the parameter classes
      Returns:
      the parameterized type
    • parametricType

      public static Type parametricType(Class<?> parametrized, Type... parameterTypes)
      Creates a parameterized type with the specified parametrized class and parameter types.
      Parameters:
      parametrized - the parametrized class
      parameterTypes - the parameter types
      Returns:
      the parameterized type