/** * This method deserializes the specified Json into an object of the specified class. It is not * suitable to use if the specified class is a generic type since it will not have the generic * type information because of the Type Erasure feature of Java. Therefore, this method should not * be used if the desired type is a generic type. Note that this method works fine if the any of * the fields of the specified object are generics, just the object itself should not be a * generic type. For the cases when the object is of generic type, invoke * {@link #fromJson(String, Type)}. If you have the Json in a {@link Reader} instead of * a String, use {@link #fromJson(Reader, Class)} instead. * * @param <T> the type of the desired object * @param json the string from which the object is to be deserialized * @param classOfT the class of T * @return an object of type T from the string. Returns {@code null} if {@code json} is {@code null}. * @throws JsonSyntaxException if json is not a valid representation for an object of type * classOfT */ public <T> T fromJson(String json, Class<T> classOfT)throws JsonSyntaxException { Objectobject= fromJson(json, (Type) classOfT); return Primitives.wrap(classOfT).cast(object); }
/** * This method deserializes the specified Json into an object of the specified type. This method * is useful if the specified object is a generic type. For non-generic objects, use * {@link #fromJson(String, Class)} instead. If you have the Json in a {@link Reader} instead of * a String, use {@link #fromJson(Reader, Type)} instead. * * @param <T> the type of the desired object * @param json the string from which the object is to be deserialized * @param typeOfT The specific genericized type of src. You can obtain this type by using the * {@link com.google.gson.reflect.TypeToken} class. For example, to get the type for * {@code Collection<Foo>}, you should use: * <pre> * Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType(); * </pre> * @return an object of type T from the string. Returns {@code null} if {@code json} is {@code null}. * @throws JsonParseException if json is not a valid representation for an object of type typeOfT * @throws JsonSyntaxException if json is not a valid representation for an object of type */ @SuppressWarnings("unchecked") public <T> T fromJson(String json, Type typeOfT)throws JsonSyntaxException { if (json == null) { returnnull; } StringReaderreader=newStringReader(json); Ttarget= (T) fromJson(reader, typeOfT); return target; }
观察fromJson(String json, Class<T> classOfT)的注释:
It is not suitable to use if the specified class is a generic type since it will not have the generic type information because of the Type Erasure feature of Java
也就是说,由于Java泛型的擦除机制,这个方法不适用于传入泛型的类,比如Map<String,Long>,List<String>等,这个时候可以用T fromJson(String json, Type typeOfT)替代。
下面还有一段话:
Note that this method works fine if the any of the fields of the specified object are generics, just the object itself should not be a generic type
** 注意:** 如果对象不是泛型的,只是字段是泛型的话这个方法是可以使用的
刚开始不太理解这句话,后来想通了,也就是类定义上不能带有泛型比如 public interface Map<K,V> 这样的就不行,但是如果是下面这样的只有域上带有的泛型是可以:
/** * Method to deserialize JSON content from given JSON content String. * * @throws IOException if a low-level I/O problem (unexpected end-of-input, * network error) occurs (passed through as-is without additional wrapping -- note * that this is one case where {@link DeserializationFeature#WRAP_EXCEPTIONS} * does NOT result in wrapping of exception even if enabled) * @throws JsonParseException if underlying input contains invalid content * of type {@link JsonParser} supports (JSON for default case) * @throws JsonMappingException if the input JSON structure does not match structure * expected for result type (or has other mismatch issues) */ @SuppressWarnings("unchecked") public <T> T readValue(String content, Class<T> valueType) throws IOException, JsonParseException, JsonMappingException { return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueType)); }
/** * Method to deserialize JSON content from given JSON content String. * * @throws IOException if a low-level I/O problem (unexpected end-of-input, * network error) occurs (passed through as-is without additional wrapping -- note * that this is one case where {@link DeserializationFeature#WRAP_EXCEPTIONS} * does NOT result in wrapping of exception even if enabled) * @throws JsonParseException if underlying input contains invalid content * of type {@link JsonParser} supports (JSON for default case) * @throws JsonMappingException if the input JSON structure does not match structure * expected for result type (or has other mismatch issues) */ @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> T readValue(String content, TypeReference valueTypeRef) throws IOException, JsonParseException, JsonMappingException { return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueTypeRef)); }