@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(); }
@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(); } } }
@Override public T convert(ResponseBody value) throws IOException { JsonReader jsonReader = gson.newJsonReader(value.charStream()); try { return adapter.read(jsonReader); } finally { value.close(); } } }
@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(); } } }
@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(); } }; }
/** * 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<Collection<Foo>>(){}.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; }
/** * 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); }
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()); } }
@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(); }
/** * 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<Collection<Foo>>(){}.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; }
@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(); } } }
/** * 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); }
@Override public ListInfoWithType convert(@NonNull ResponseBody value) throws IOException { JsonReader jsonReader = gson.newJsonReader(value.charStream()); try { return adapter.read(jsonReader); } finally { value.close(); } } }
@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(); } } }
@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(); } } }
@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(); } } }
@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; }
@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(); } } }
@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(); } } }
@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(); } } }