static <T> JsonAdapter<Set<T>> newLinkedHashSetAdapter(Type type, Moshi moshi) { Type elementType = Types.collectionElementType(type, Collection.class); JsonAdapter<T> elementAdapter = moshi.adapter(elementType); return new CollectionJsonAdapter<Set<T>, T>(elementAdapter) { @Override Set<T> newCollection() { return new LinkedHashSet<>(); } }; }
@Override public void toJson(JsonWriter writer, SortedSet<T> set) throws IOException { writer.beginArray(); for (T element : set) { elementAdapter.toJson(writer, element); } writer.endArray(); } }
@Override public void toJson(JsonWriter writer, Object value) throws IOException { Class<?> valueClass = value.getClass(); if (valueClass == Object.class) { // Don't recurse infinitely when the runtime type is also Object.class. writer.beginObject(); writer.endObject(); } else { moshi.adapter(toJsonType(valueClass), Util.NO_ANNOTATIONS).toJson(writer, value); } }
public SlackApi(String clientId, String clientSecret, int port) { this.httpClient = new OkHttpClient.Builder() .build(); this.moshi = new Moshi.Builder() .add(new SlackJsonAdapters()) .build(); this.clientId = clientId; this.clientSecret = clientSecret; this.port = port; }
/** Create an instance using a default {@link Moshi} instance for conversion. */ public static MoshiConverterFactory create() { return create(new Moshi.Builder().build()); }
@Override public @Nullable T fromJson(JsonReader reader) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { throw new JsonDataException("Unexpected null at " + reader.getPath()); } else { return delegate.fromJson(reader); } } @Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException {
@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 Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { JsonAdapter<?> adapter = moshi.adapter(type, jsonAnnotations(parameterAnnotations)); if (lenient) { adapter = adapter.lenient(); } if (failOnUnknown) { adapter = adapter.failOnUnknown(); } if (serializeNulls) { adapter = adapter.serializeNulls(); } return new MoshiRequestBodyConverter<>(adapter); }
@Override public void bind(Moshi moshi, JsonAdapter.Factory factory) { super.bind(moshi, factory); delegate = Types.equals(parameterTypes[0], returnType) && qualifierAnnotations.equals(returnTypeAnnotations) ? moshi.nextAdapter(factory, parameterTypes[0], qualifierAnnotations) : moshi.adapter(parameterTypes[0], qualifierAnnotations); }
@Override public T fromJson(JsonReader reader) throws IOException { // Read the value first so that the reader will be in a known state even if there's an // exception. Otherwise it may be awkward to recover: it might be between calls to // beginObject() and endObject() for example. Object jsonValue = reader.readJsonValue(); // Use the delegate to convert the JSON value to the target type. try { return delegate.fromJsonValue(jsonValue); } catch (JsonDataException e) { return defaultValue; } }
@ToJson String urlToJson(HttpUrl httpUrl) { return httpUrl.toString(); }
@FromJson HttpUrl urlFromJson(String urlString) { if (urlString.startsWith("wss:")) urlString = "https:" + urlString.substring(4); if (urlString.startsWith("ws:")) urlString = "http:" + urlString.substring(3); return HttpUrl.get(urlString); } }
/** * Throws a new IO exception with the given message and a context snippet * with this reader's content. */ final JsonEncodingException syntaxError(String message) throws JsonEncodingException { throw new JsonEncodingException(message + " at path " + getPath()); }
@Override public void endArray() throws IOException { JsonIterator peeked = require(JsonIterator.class, Token.END_ARRAY); if (peeked.endToken != Token.END_ARRAY || peeked.hasNext()) { throw typeMismatch(peeked, Token.END_ARRAY); } remove(); }
@Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { JsonAdapter<?> adapter = moshi.adapter(type, jsonAnnotations(annotations)); if (lenient) { adapter = adapter.lenient(); } if (failOnUnknown) { adapter = adapter.failOnUnknown(); } if (serializeNulls) { adapter = adapter.serializeNulls(); } return new MoshiResponseBodyConverter<>(adapter); }
static <T> JsonAdapter<Collection<T>> newArrayListAdapter(Type type, Moshi moshi) { Type elementType = Types.collectionElementType(type, Collection.class); JsonAdapter<T> elementAdapter = moshi.adapter(elementType); return new CollectionJsonAdapter<Collection<T>, T>(elementAdapter) { @Override Collection<T> newCollection() { return new ArrayList<>(); } }; }
@Override public void toJson(JsonWriter writer, C value) throws IOException { writer.beginArray(); for (T element : value) { elementAdapter.toJson(writer, element); } writer.endArray(); }
@Override public void endObject() throws IOException { JsonIterator peeked = require(JsonIterator.class, Token.END_OBJECT); if (peeked.endToken != Token.END_OBJECT || peeked.hasNext()) { throw typeMismatch(peeked, Token.END_OBJECT); } pathNames[stackSize - 1] = null; remove(); }