Refine search
/** See https://api.slack.com/methods/rtm.start. */ public RtmStartResponse rtmStart(String accessToken) throws IOException { HttpUrl url = baseUrl.newBuilder("rtm.start") .addQueryParameter("token", accessToken) .build(); Request request = new Request.Builder() .url(url) .build(); Call call = httpClient.newCall(request); try (Response response = call.execute()) { JsonAdapter<RtmStartResponse> jsonAdapter = moshi.adapter(RtmStartResponse.class); return jsonAdapter.fromJson(response.body().source()); } }
@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, returnType, returnTypeAnnotations) : moshi.adapter(returnType, returnTypeAnnotations); }
public void run() throws Exception { // For some reason our JSON has date and time as separate fields. We will clean that up during // parsing: Moshi will first parse the JSON directly to an EventJson and from that the // EventJsonAdapter will create the actual Event. String json = "" + "{\n" + " \"title\": \"Blackjack tournament\",\n" + " \"begin_date\": \"20151010\",\n" + " \"begin_time\": \"17:04\"\n" + "}\n"; Moshi moshi = new Moshi.Builder().add(new EventJsonAdapter()).build(); JsonAdapter<Event> jsonAdapter = moshi.adapter(Event.class); Event event = jsonAdapter.fromJson(json); System.out.println(event); System.out.println(jsonAdapter.toJson(event)); }
@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); } }
ObjectJsonAdapter(Moshi moshi) { this.moshi = moshi; this.listJsonAdapter = moshi.adapter(List.class); this.mapAdapter = moshi.adapter(Map.class); this.stringAdapter = moshi.adapter(String.class); this.doubleAdapter = moshi.adapter(Double.class); this.booleanAdapter = moshi.adapter(Boolean.class); }
public static InfluxDBException buildExceptionForErrorState(final String errorBody) { try { Moshi moshi = new Moshi.Builder().build(); JsonAdapter<ErrorMessage> adapter = moshi.adapter(ErrorMessage.class).lenient(); ErrorMessage errorMessage = adapter.fromJson(errorBody); return InfluxDBException.buildExceptionFromErrorMessage(errorMessage.error); } catch (Exception e) { return new InfluxDBException(errorBody); } }
@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 JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) { Set<? extends Annotation> nextAnnotations = Types.nextAnnotations(annotations, SerializeNulls.class); if (nextAnnotations == null) return null; return moshi.adapter(type, nextAnnotations).serializeNulls(); } };
@Override public @Nullable JsonAdapter<?> create( Type requestedType, Set<? extends Annotation> annotations, Moshi moshi) { if (type != requestedType) return null; JsonAdapter<T> delegate = moshi.nextAdapter(this, type, annotations); return new DefaultOnDataMismatchAdapter<>(delegate, defaultValue); } };
public void run() throws Exception { Moshi moshi = new Moshi.Builder() .add(new MultipleFormatsCardAdapter()) .add(new CardStringAdapter()) .build(); JsonAdapter<Card> cardAdapter = moshi.adapter(Card.class); // Decode cards from one format or the other. System.out.println(cardAdapter.fromJson("\"5D\"")); System.out.println(cardAdapter.fromJson("{\"suit\": \"SPADES\", \"rank\": 5}")); // Cards are always encoded as strings. System.out.println(cardAdapter.toJson(new Card('5', Suit.CLUBS))); }
public void run() throws Exception { Moshi moshi = new Moshi.Builder() .add(new SortedSetAdapterFactory()) .build(); JsonAdapter<SortedSet<String>> jsonAdapter = moshi.adapter( Types.newParameterizedType(SortedSet.class, String.class)); TreeSet<String> model = new TreeSet<>(); model.add("a"); model.add("b"); model.add("c"); String json = jsonAdapter.toJson(model); System.out.println(json); }
@Override protected Node readValue(String source, String label, boolean lenient) { try { JsonAdapter<Object> adapter = moshi.adapter(Object.class); if (lenient) { adapter = adapter.lenient(); } return newNode(adapter.fromJson(source)); } catch (IOException e) { throw new IllegalArgumentException("Can not parse " + label + " value.", e); } }
@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); }
@Override public @Nullable JsonAdapter<?> create( Type type, Set<? extends Annotation> annotations, Moshi moshi) { Set<? extends Annotation> delegateAnnotations = Types.nextAnnotations(annotations, Enveloped.class); if (delegateAnnotations == null) { return null; } Type envelope = Types.newParameterizedTypeWithOwner(EnvelopeJsonAdapter.class, Envelope.class, type); JsonAdapter<Envelope<?>> delegate = moshi.nextAdapter(this, envelope, delegateAnnotations); return new EnvelopeJsonAdapter(delegate); } };
/** See https://api.slack.com/methods/oauth.access. */ public OAuthSession exchangeCode(String code, HttpUrl redirectUrl) throws IOException { HttpUrl url = baseUrl.newBuilder("oauth.access") .addQueryParameter("client_id", clientId) .addQueryParameter("client_secret", clientSecret) .addQueryParameter("code", code) .addQueryParameter("redirect_uri", redirectUrl.toString()) .build(); Request request = new Request.Builder() .url(url) .build(); Call call = httpClient.newCall(request); try (Response response = call.execute()) { JsonAdapter<OAuthSession> jsonAdapter = moshi.adapter(OAuthSession.class); return jsonAdapter.fromJson(response.body().source()); } }
public void run() throws Exception { Moshi moshi = new Moshi.Builder() .add(Date.class, new Rfc3339DateJsonAdapter()) .build(); JsonAdapter<Tournament> jsonAdapter = moshi.adapter(Tournament.class); // The RFC3339 JSON adapter can read dates with a timezone offset like '-05:00'. String lastTournament = "" + "{" + " \"location\":\"Chainsaw\"," + " \"name\":\"21 for 21\"," + " \"start\":\"2015-09-01T20:00:00-05:00\"" + "}"; System.out.println("Last tournament: " + jsonAdapter.fromJson(lastTournament)); // The RFC3339 JSON adapter always writes dates with UTC, using a 'Z' suffix. Tournament nextTournament = new Tournament( "Waterloo Classic", "Bauer Kitchen", newDate(2015, 10, 1, 20, -5)); System.out.println("Next tournament JSON: " + jsonAdapter.toJson(nextTournament)); }
public void run() throws Exception { BlackjackHand blackjackHand = new BlackjackHand( new Card('6', SPADES), Arrays.asList(new Card('4', CLUBS), new Card('A', HEARTS))); Moshi moshi = new Moshi.Builder().build(); JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class); String json = jsonAdapter.toJson(blackjackHand); System.out.println(json); }
@CheckReturnValue public <T> JsonAdapter<T> adapter(Type type, Set<? extends Annotation> annotations) { return adapter(type, annotations, null); }
@Override protected Node readValue(String source, String label, boolean lenient) { try { JsonAdapter<Object> adapter = moshi.adapter(Object.class); if (lenient) { adapter = adapter.lenient(); } return newNode(adapter.fromJson(source)); } catch (IOException e) { throw new IllegalArgumentException("Can not parse " + label + " value.", e); } }