⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gson.java

📁 地图生成的一个文件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * {@code getClass()} loses the generic type information because of the Type Erasure feature   * of Java. Note that this method works fine if the any of the object fields are of generic type,   * just the object itself should not be of a generic type. If the object is of generic type, use   * {@link #toJson(Object, Type, Appendable)} instead.   *   * @param src the object for which Json representation is to be created setting for Gson   * @param writer Writer to which the Json representation needs to be written   * @since 1.2   */  public void toJson(Object src, Appendable writer) {    try {      if (src != null) {        toJson(src, src.getClass(), writer);      } else if (serializeNulls) {        writeOutNullString(writer);      }    } catch (IOException ioe) {      throw new RuntimeException(ioe);    }  }  /**   * This method serializes the specified object, including those of generic types, into its   * equivalent Json representation. This method must be used if the specified object is a generic   * type. For non-generic objects, use {@link #toJson(Object, Appendable)} instead.   *   * @param src the object for which JSON representation is to be created   * @param typeOfSrc 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 typeOfSrc = new TypeToken&lt;Collection&lt;Foo&gt;&gt;(){}.getType();   * </pre>   * @param writer Writer to which the Json representation of src needs to be written.   * @since 1.2   */  public void toJson(Object src, Type typeOfSrc, Appendable writer) {    try {      if (src != null) {        JsonSerializationContext context = new JsonSerializationContextDefault(            createDefaultObjectNavigatorFactory(), serializeNulls, serializers);        JsonElement jsonElement = context.serialize(src, typeOfSrc);        if (generateNonExecutableJson) {          writer.append(JSON_NON_EXECUTABLE_PREFIX);        }        //TODO(Joel): instead of navigating the "JsonElement" inside the formatter, do it here.        formatter.format(jsonElement, writer, serializeNulls);      } else {        if (serializeNulls) {          writeOutNullString(writer);        }      }    } catch (IOException ioe) {      throw new RuntimeException(ioe);    }  }  /**   * 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   * @throws JsonParseException if json is not a valid representation for an object of type   * classOfT   */  @SuppressWarnings("unchecked")  public <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException {    T target = (T) fromJson(json, (Type) classOfT);    return target;  }  /**   * 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&lt;Collection&lt;Foo&gt;&gt;(){}.getType();   * </pre>   * @return an object of type T from the string   * @throws JsonParseException if json is not a valid representation for an object of type typeOfT   */  @SuppressWarnings("unchecked")  public <T> T fromJson(String json, Type typeOfT) throws JsonParseException {    StringReader reader = new StringReader(json);    T target = (T) fromJson(reader, typeOfT);    return target;  }  /**   * This method deserializes the Json read from the specified reader 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(Reader, Type)}. If you have the Json in a String form instead of a   * {@link Reader}, use {@link #fromJson(String, Class)} instead.   *   * @param <T> the type of the desired object   * @param json the reader producing the Json from which the object is to be deserialized.   * @param classOfT the class of T   * @return an object of type T from the string   * @throws JsonParseException if json is not a valid representation for an object of type   * classOfT   * @since 1.2   */  public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonParseException {    T target = classOfT.cast(fromJson(json, (Type) classOfT));    return target;  }  /**   * This method deserializes the Json read from the specified reader 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(Reader, Class)} instead. If you have the Json in a   * String form instead of a {@link Reader}, use {@link #fromJson(String, Type)} instead.   *   * @param <T> the type of the desired object   * @param json the reader producing Json 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&lt;Collection&lt;Foo&gt;&gt;(){}.getType();   * </pre>   * @return an object of type T from the json   * @throws JsonParseException if json is not a valid representation for an object of type typeOfT   * @since 1.2   */  @SuppressWarnings("unchecked")  public <T> T fromJson(Reader json, Type typeOfT) throws JsonParseException {    JsonElement root = new JsonParser().parse(json);    JsonDeserializationContext context = new JsonDeserializationContextDefault(        createDefaultObjectNavigatorFactory(), deserializers, objectConstructor);    T target = (T) context.deserialize(root, typeOfT);    return target;  }  /**   * This method deserializes the Json read from the specified parse tree into an object of the   * specified type. 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(JsonElement, Type)}.    * @param <T> the type of the desired object   * @param json the root of the parse tree of {@link JsonElement}s from which the object is to    * be deserialized   * @param classOfT The class of T   * @return an object of type T from the json   * @throws JsonParseException if json is not a valid representation for an object of type typeOfT   * @since 1.3   */  public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonParseException {    T target = classOfT.cast(fromJson(json, (Type) classOfT));    return target;  }  /**   * This method deserializes the Json read from the specified parse tree 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(JsonElement, Class)} instead.    *   * @param <T> the type of the desired object   * @param json the root of the parse tree of {@link JsonElement}s 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&lt;Collection&lt;Foo&gt;&gt;(){}.getType();   * </pre>   * @return an object of type T from the json   * @throws JsonParseException if json is not a valid representation for an object of type typeOfT   * @since 1.3   */  @SuppressWarnings("unchecked")  public <T> T fromJson(JsonElement json, Type typeOfT) throws JsonParseException {    JsonDeserializationContext context = new JsonDeserializationContextDefault(        createDefaultObjectNavigatorFactory(), deserializers, objectConstructor);    T target = (T) context.deserialize(json, typeOfT);    return target;  }  /**   * Appends the {@link #NULL_STRING} to the {@code writer} object.   *   * @param writer the object to append the null value to   */  private void writeOutNullString(Appendable writer) throws IOException {    writer.append(NULL_STRING);  }    @Override   public String toString() {  	StringBuilder sb = new StringBuilder("{")  	    .append("serializeNulls:").append(serializeNulls)  	    .append(",serializers:").append(serializers)  	    .append(",deserializers:").append(deserializers)  	      	// using the name instanceCreator instead of ObjectConstructor since the users of Gson are       	// more familiar with the concept of Instance Creators. Moreover, the objectConstructor is      	// just a utility class around instance creators, and its toString() only displays them.        .append(",instanceCreators:").append(objectConstructor)        .append("}");  	return sb.toString();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -