@CheckReturnValue public final @Nullable T fromJson(BufferedSource source) throws IOException { return fromJson(JsonReader.of(source)); }
@CheckReturnValue public final @Nullable T fromJson(String string) throws IOException { JsonReader reader = JsonReader.of(new Buffer().writeUtf8(string)); T result = fromJson(reader); if (!isLenient() && reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; }
@Override public JsonWriter value(BufferedSource source) throws IOException { if (promoteValueToName) { throw new IllegalStateException( "BufferedSource cannot be used as a map key in JSON at path " + getPath()); } Object value = JsonReader.of(source).readJsonValue(); boolean serializeNulls = this.serializeNulls; this.serializeNulls = true; try { add(value); } finally { this.serializeNulls = serializeNulls; } pathIndices[stackSize - 1]++; return this; }
@Override public T convert(ResponseBody value) throws IOException { BufferedSource source = value.source(); try { // Moshi has no document-level API so the responsibility of BOM skipping falls to whatever // is delegating to it. Since it's a UTF-8-only library as well we only honor the UTF-8 BOM. if (source.rangeEquals(0, UTF8_BOM)) { source.skip(UTF8_BOM.size()); } JsonReader reader = JsonReader.of(source); T result = adapter.fromJson(reader); if (reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; } finally { value.close(); } } }
@Override public List<String> convert(BufferedSource b) throws IOException { return collectValuesNamed(JsonReader.of(b), "key"); } };
@CheckReturnValue public final @Nullable T fromJson(BufferedSource source) throws IOException { return fromJson(JsonReader.of(source)); }
@Override public Response intercept(Chain chain) throws IOException { Request input = chain.request(); Request requestWithCredentials = appendBasicAuthHeaderParameters(input); Response response = chain.proceed(requestWithCredentials); if (response.code() == 403) { try (ResponseBody body = response.body()) { JsonReader message = enterPath(JsonReader.of(body.source()), "message"); if (message != null) throw new IllegalStateException(message.nextString()); } throw new IllegalStateException(response.toString()); } return response; }
@Override public Response intercept(Chain chain) throws IOException { Request input = chain.request(); Request requestWithCredentials = appendBasicAuthHeaderParameters(input); Response response = chain.proceed(requestWithCredentials); if (response.code() == 403) { try (ResponseBody body = response.body()) { JsonReader message = enterPath(JsonReader.of(body.source()), "message"); if (message != null) throw new IllegalStateException(message.nextString()); } throw new IllegalStateException(response.toString()); } return response; }
@Override String format(String source) { Buffer buffer = new Buffer().writeUtf8(source); JsonReader reader = JsonReader.of(buffer); try { return ADAPTER.toJson(reader.readJsonValue()); } catch (IOException e) { return ""; } }
@CheckReturnValue public final @Nullable T fromJson(String string) throws IOException { JsonReader reader = JsonReader.of(new Buffer().writeUtf8(string)); T result = fromJson(reader); if (!isLenient() && reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; }
@Override public JsonWriter value(BufferedSource source) throws IOException { if (promoteValueToName) { throw new IllegalStateException( "BufferedSource cannot be used as a map key in JSON at path " + getPath()); } Object value = JsonReader.of(source).readJsonValue(); boolean serializeNulls = this.serializeNulls; this.serializeNulls = true; try { add(value); } finally { this.serializeNulls = serializeNulls; } pathIndices[stackSize - 1]++; return this; }
/** Converts the contents of the {@link ResponseBody} to the expected java {@code type}. */ @SuppressWarnings("unchecked") // Type matching is the caller's responsibility. public <R> R convertFromBody(Type type, ResponseBody body) throws IOException { if (body == null) return null; try { // Don't parse the response body, if the caller doesn't expect a json. if (type == Void.class) return null; if (type == String.class) return (R) body.string(); if (type == ResponseBody.class) return (R) buffer(body); JsonAdapter<R> adapter = findAdapter(type); JsonReader reader = JsonReader.of(body.source()); return adapter.fromJson(reader); } finally { closeQuietly(body); } }
@Benchmark @Override public Object moshi() throws Exception { try (com.squareup.moshi.JsonReader jr = com.squareup.moshi.JsonReader.of(JSON_SOURCE().nextOkioBufferedSource())) { return JSON_SOURCE().streamDeserializer().moshi(jr); } }
@Override public T convert(ResponseBody value) throws IOException { BufferedSource source = value.source(); try { // Moshi has no document-level API so the responsibility of BOM skipping falls to whatever // is delegating to it. Since it's a UTF-8-only library as well we only honor the UTF-8 BOM. if (source.rangeEquals(0, UTF8_BOM)) { source.skip(UTF8_BOM.size()); } JsonReader reader = JsonReader.of(source); T result = adapter.fromJson(reader); if (reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } return result; } finally { value.close(); } } }