com.google.gson.Gson.newJsonReader()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(13.4k)|赞(0)|评价(0)|浏览(464)

本文整理了Java中com.google.gson.Gson.newJsonReader()方法的一些代码示例,展示了Gson.newJsonReader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Gson.newJsonReader()方法的具体详情如下:
包路径:com.google.gson.Gson
类名称:Gson
方法名:newJsonReader

Gson.newJsonReader介绍

[英]Returns a new JSON reader configured for the settings on this Gson instance.
[中]返回为此Gson实例上的设置配置的新JSON读取器。

代码示例

代码示例来源:origin: spring-projects/spring-batch

@Override
public void open(Resource resource) throws Exception {
  Assert.notNull(resource, "The resource must not be null");
  this.inputStream = resource.getInputStream();
  this.jsonReader = this.mapper.newJsonReader(new InputStreamReader(this.inputStream));
  Assert.state(this.jsonReader.peek() == JsonToken.BEGIN_ARRAY,
      "The Json input stream must start with an array of Json objects");
  this.jsonReader.beginArray();
}

代码示例来源:origin: square/retrofit

@Override public T convert(ResponseBody value) throws IOException {
  JsonReader jsonReader = gson.newJsonReader(value.charStream());
  try {
   T result = adapter.read(jsonReader);
   if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
    throw new JsonIOException("JSON document was not fully consumed.");
   }
   return result;
  } finally {
   value.close();
  }
 }
}

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

@Override
  public T convert(ResponseBody value) throws IOException {
    JsonReader jsonReader = gson.newJsonReader(value.charStream());
    try {
      return adapter.read(jsonReader);
    } finally {
      value.close();
    }
  }
}

代码示例来源:origin: com.squareup.retrofit2/converter-gson

@Override public T convert(ResponseBody value) throws IOException {
  JsonReader jsonReader = gson.newJsonReader(value.charStream());
  try {
   T result = adapter.read(jsonReader);
   if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
    throw new JsonIOException("JSON document was not fully consumed.");
   }
   return result;
  } finally {
   value.close();
  }
 }
}

代码示例来源:origin: nickbutcher/plaid

@Override
public Converter<ResponseBody, ?> responseBodyConverter(
    Type type, Annotation[] annotations, Retrofit retrofit) {
  // This converter requires an annotation providing the name of the payload in the envelope;
  // if one is not supplied then return null to continue down the converter chain.
  final String payloadName = getPayloadName(annotations);
  if (payloadName == null) return null;
  final TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
  return (Converter<ResponseBody, Object>) body -> {
    try (JsonReader jsonReader = gson.newJsonReader(body.charStream())) {
      jsonReader.beginObject();
      while (jsonReader.hasNext()) {
        if (payloadName.equals(jsonReader.nextName())) {
          return adapter.read(jsonReader);
        } else {
          jsonReader.skipValue();
        }
      }
      return null;
    } finally {
      body.close();
    }
  };
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * 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. Returns {@code null} if {@code json} is at EOF.
 * @throws JsonIOException if there was a problem reading from the Reader
 * @throws JsonSyntaxException if json is not a valid representation for an object of type
 * @since 1.2
 */
@SuppressWarnings("unchecked")
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
 JsonReader jsonReader = newJsonReader(json);
 T object = (T) fromJson(jsonReader, typeOfT);
 assertFullConsumption(object, jsonReader);
 return object;
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * 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. Returns {@code null} if {@code json} is at EOF.
 * @throws JsonIOException if there was a problem reading from the Reader
 * @throws JsonSyntaxException if json is not a valid representation for an object of type
 * @since 1.2
 */
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
 JsonReader jsonReader = newJsonReader(json);
 Object object = fromJson(jsonReader, classOfT);
 assertFullConsumption(object, jsonReader);
 return Primitives.wrap(classOfT).cast(object);
}

代码示例来源:origin: gradle.plugin.com.github.mazzeb/auto-version

public Version readFromFile() {
  try (JsonReader jsonReader = GSON.newJsonReader(new FileReader(fileName))) {
    return GSON.fromJson(jsonReader, Version.class);
  } catch (IOException e) {
    throw new GradleException(e.getMessage());
  }
}

代码示例来源:origin: apache/servicemix-bundles

@Override
public void open(Resource resource) throws Exception {
  Assert.notNull(resource, "The resource must not be null");
  this.inputStream = resource.getInputStream();
  this.jsonReader = this.mapper.newJsonReader(new InputStreamReader(this.inputStream));
  Assert.state(this.jsonReader.peek() == JsonToken.BEGIN_ARRAY,
      "The Json input stream must start with an array of Json objects");
  this.jsonReader.beginArray();
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

/**
 * 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. Returns {@code null} if {@code json} is at EOF.
 * @throws JsonIOException if there was a problem reading from the Reader
 * @throws JsonSyntaxException if json is not a valid representation for an object of type
 * @since 1.2
 */
@SuppressWarnings("unchecked")
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
 JsonReader jsonReader = newJsonReader(json);
 T object = (T) fromJson(jsonReader, typeOfT);
 assertFullConsumption(object, jsonReader);
 return object;
}

代码示例来源:origin: vaibhav-sinha/kong-java-client

@Override
  public T convert(ResponseBody value) throws IOException {

    String response = value.string();

    if(response == null || response.isEmpty()) {
      //It may response empty body...
      log.debug("Response empty body...");
      return null;
    }

    MediaType contentType = value.contentType();
    Charset charset = contentType != null ? contentType.charset(UTF_8) : UTF_8;
    InputStream inputStream = new ByteArrayInputStream(response.getBytes());
    Reader reader = new InputStreamReader(inputStream, charset);
    JsonReader jsonReader = gson.newJsonReader(reader);

    try {
      return adapter.read(jsonReader);
    } finally {
      value.close();
    }

  }
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

/**
 * 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. Returns {@code null} if {@code json} is at EOF.
 * @throws JsonIOException if there was a problem reading from the Reader
 * @throws JsonSyntaxException if json is not a valid representation for an object of type
 * @since 1.2
 */
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
 JsonReader jsonReader = newJsonReader(json);
 Object object = fromJson(jsonReader, classOfT);
 assertFullConsumption(object, jsonReader);
 return Primitives.wrap(classOfT).cast(object);
}

代码示例来源:origin: sososeen09/MultiTypeJsonParser

@Override
  public ListInfoWithType convert(@NonNull ResponseBody value) throws IOException {
    JsonReader jsonReader = gson.newJsonReader(value.charStream());
    try {
      return adapter.read(jsonReader);
    } finally {
      value.close();
    }
  }
}

代码示例来源:origin: Lauzy/TicktockMusic

@Override
  public T convert(@NonNull ResponseBody value) throws IOException {
    String response = value.string();
    try {
      StatusEntity status = mGson.fromJson(response, StatusEntity.class);
      if (status.isInvalidCode()) {//这里可具体判断多种状态
        value.close();
        throw new ErrorMsgException(status.error_code, status.error_msg);
      }

      MediaType contentType = value.contentType();
      Charset charset = contentType != null ? contentType.charset(UTF_8) : UTF_8;
      InputStream inputStream = new ByteArrayInputStream(response.getBytes());
      if (charset == null)
        throw new NullPointerException("charset == null");
      Reader reader = new InputStreamReader(inputStream, charset);
      JsonReader jsonReader = mGson.newJsonReader(reader);
      return mTypeAdapter.read(jsonReader);
    } finally {
      value.close();
    }
  }
}

代码示例来源:origin: lionoggo/Akit-Reader

@Override
  public T convert(ResponseBody value) throws IOException {
    String response = value.string();
    GanResult re = mGson.fromJson(response, GanResult.class);
    if (re.isError()) {
      value.close();
      throw new ApiException(ApiErrorCode.ERROR_OTHER, "未知错误");
    }

    MediaType mediaType = value.contentType();
    Charset charset = mediaType != null ? mediaType.charset(UTF_8) : UTF_8;
    ByteArrayInputStream bis = new ByteArrayInputStream(response.getBytes());
    InputStreamReader reader = new InputStreamReader(bis, charset);
    JsonReader jsonReader = mGson.newJsonReader(reader);
    try {
      return adapter.read(jsonReader);
    } finally {
      value.close();
    }
  }
}

代码示例来源:origin: lionoggo/FastApp

@Override
  public T convert(ResponseBody value) throws IOException {
    String response = value.string();
    GanResult re = mGson.fromJson(response, GanResult.class);
    if (re.isError()) {
      value.close();
      throw new ApiException(ApiErrorCode.ERROR_OTHER, "未知错误");
    }

    MediaType mediaType = value.contentType();
    Charset charset = mediaType != null ? mediaType.charset(UTF_8) : UTF_8;
    ByteArrayInputStream bis = new ByteArrayInputStream(response.getBytes());
    InputStreamReader reader = new InputStreamReader(bis, charset);
    JsonReader jsonReader = mGson.newJsonReader(reader);
    try {
      return adapter.read(jsonReader);
    } finally {
      value.close();
    }
  }
}

代码示例来源:origin: WallaceXiao/StockChart-MPAndroidChart

@Override
public <T> T load(InputStream source, Type type) {
  T value = null;
  try {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    JsonReader jsonReader = gson.newJsonReader(new InputStreamReader(source));
    value = (T) adapter.read(jsonReader);
    //value = gson.fromJson(new InputStreamReader(source), type);
  } catch (JsonIOException | IOException| ConcurrentModificationException | JsonSyntaxException e) {
    HttpLog.e(e.getMessage());
  } catch (Exception e){
    HttpLog.e(e.getMessage());
  }finally {
    Utils.close(source);
  }
  return value;
}

代码示例来源:origin: flipkart-incubator/proteus

@Override
 public T convert(ResponseBody value) throws IOException {
  TypeAdapter<T> adapter = getAdapter();
  JsonReader jsonReader = gson.newJsonReader(value.charStream());
  jsonReader.setLenient(true);
  try {
   return adapter.read(jsonReader);
  } finally {
   value.close();
  }
 }
}

代码示例来源:origin: lionoggo/Akit-Reader

@Override
  public T convert(ResponseBody value) throws IOException {
    String response = value.string();
    WXResult re = mGson.fromJson(response, WXResult.class);
    if (!re.isOk()) {
      value.close();
      if (re.getNewslist() != null) {
        String errorExtendMsg = mGson.toJson(re.getNewslist());
        throw new ApiException(re.getCode(), re.getMsg(), errorExtendMsg);
      } else {
        throw new ApiException(re.getCode(), re.getMsg());
      }
    }

    MediaType mediaType = value.contentType();
    Charset charset = mediaType != null ? mediaType.charset(UTF_8) : UTF_8;
    ByteArrayInputStream bis = new ByteArrayInputStream(response.getBytes());
    InputStreamReader reader = new InputStreamReader(bis, charset);
    JsonReader jsonReader = mGson.newJsonReader(reader);
    try {
      return adapter.read(jsonReader);
    } finally {
      value.close();
    }
  }
}

代码示例来源:origin: lionoggo/FastApp

@Override
  public T convert(ResponseBody value) throws IOException {
    String response = value.string();
    WXResult re = mGson.fromJson(response, WXResult.class);
    if (!re.isOk()) {
      value.close();
      if (re.getNewslist() != null) {
        String errorExtendMsg = mGson.toJson(re.getNewslist());
        throw new ApiException(re.getCode(), re.getMsg(), errorExtendMsg);
      } else {
        throw new ApiException(re.getCode(), re.getMsg());
      }
    }

    MediaType mediaType = value.contentType();
    Charset charset = mediaType != null ? mediaType.charset(UTF_8) : UTF_8;
    ByteArrayInputStream bis = new ByteArrayInputStream(response.getBytes());
    InputStreamReader reader = new InputStreamReader(bis, charset);
    JsonReader jsonReader = mGson.newJsonReader(reader);
    try {
      return adapter.read(jsonReader);
    } finally {
      value.close();
    }
  }
}

相关文章