public <T> Builder add(final Type type, final JsonAdapter<T> jsonAdapter) { if (type == null) throw new IllegalArgumentException("type == null"); if (jsonAdapter == null) throw new IllegalArgumentException("jsonAdapter == null"); return add(new JsonAdapter.Factory() { @Override public @Nullable JsonAdapter<?> create( Type targetType, Set<? extends Annotation> annotations, Moshi moshi) { return annotations.isEmpty() && Util.typesMatch(type, targetType) ? jsonAdapter : null; } }); }
public <T> Builder add(final Type type, final Class<? extends Annotation> annotation, final JsonAdapter<T> jsonAdapter) { if (type == null) throw new IllegalArgumentException("type == null"); if (annotation == null) throw new IllegalArgumentException("annotation == null"); if (jsonAdapter == null) throw new IllegalArgumentException("jsonAdapter == null"); if (!annotation.isAnnotationPresent(JsonQualifier.class)) { throw new IllegalArgumentException(annotation + " does not have @JsonQualifier"); } if (annotation.getDeclaredMethods().length > 0) { throw new IllegalArgumentException("Use JsonAdapter.Factory for annotations with elements"); } return add(new JsonAdapter.Factory() { @Override public @Nullable JsonAdapter<?> create( Type targetType, Set<? extends Annotation> annotations, Moshi moshi) { if (Util.typesMatch(type, targetType) && annotations.size() == 1 && Util.isAnnotationPresent(annotations, annotation)) { return jsonAdapter; } return null; } }); }
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; }
private Moshi buildMoshi(JsonAdapter.Factory factory) { return new Moshi.Builder() .add(factory) .build(); }
static OAuth2Service createOAuthService(String baseUrl) { final Moshi moshi = new Moshi.Builder().add(new OAuthScopesAdapter()).build(); return new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(MoshiConverterFactory.create(moshi)) .build() .create(OAuth2Service.class); } }
@Test public void fromJson_shouldHandleAbsent_andNull_andNonNullValues() throws IOException { Moshi moshi = new Moshi.Builder().add(new BigDecimalAdapter()).build(); JsonAdapter<BigDecimalModel> adapter = moshi.adapter(BigDecimalModel.class); BigDecimalModel model = adapter.fromJson("{\"nullDecimal\":null,\"presentDecimal\":1.23}"); assertThat(model.absentDecimal).isNull(); assertThat(model.nullDecimal).isNull(); assertThat(model.presentDecimal).isEqualTo(new BigDecimal("1.23")); }
@Provides @Singleton static Moshi provideMoshi() { return new Moshi.Builder() .add(new KotlinJsonAdapterFactory()) .build(); }
private static <T> JsonAdapter<T> makeAdapter(final Class<T> clazz) { Moshi moshi = new Moshi.Builder() .add(Date.class, new Rfc3339DateJsonAdapter()) .build(); return moshi.adapter(clazz); }
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 { // 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)); }
public void run() throws Exception { String json = "" + "{\n" + " \"hidden_card\": \"6S\",\n" + " \"visible_cards\": [\n" + " \"4C\",\n" + " \"AH\"\n" + " ]\n" + "}\n"; Moshi moshi = new Moshi.Builder() .add(new CardAdapter()) .build(); JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class); BlackjackHand blackjackHand = jsonAdapter.fromJson(json); System.out.println(blackjackHand); }
public void run() throws Exception { String json = "" + "{\n" + " \"color\": \"#ff0000\",\n" + " \"height\": 768,\n" + " \"width\": 1024\n" + "}\n"; Moshi moshi = new Moshi.Builder() .add(new ColorAdapter()) .build(); JsonAdapter<Rectangle> jsonAdapter = moshi.adapter(Rectangle.class); Rectangle rectangle = jsonAdapter.fromJson(json); System.out.println(rectangle); }
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); }
public Builder add(Object adapter) { if (adapter == null) throw new IllegalArgumentException("adapter == null"); return add(AdapterMethodsFactory.get(adapter)); }
public static void main(String[] args) throws Exception { String json = "" + "{\"data\":" + " {\n" + " \"rank\": \"4\",\n" + " \"suit\": \"CLUBS\"\n" + " }" + "}"; Moshi moshi = new Moshi.Builder().add(EnvelopeJsonAdapter.FACTORY).build(); JsonAdapter<Card> adapter = moshi.adapter(Card.class, Enveloped.class); Card out = adapter.fromJson(json); System.out.println(out); }
public void run() throws Exception { // We want to match any Stage that starts with 'in-progress' as Stage.IN_PROGRESS // and leave the rest of the enum values as to match as normal. Moshi moshi = new Moshi.Builder().add(new StageAdapter()).build(); JsonAdapter<Stage> jsonAdapter = moshi.adapter(Stage.class); System.out.println(jsonAdapter.fromJson("\"not-started\"")); System.out.println(jsonAdapter.fromJson("\"in-progress\"")); System.out.println(jsonAdapter.fromJson("\"in-progress-step1\"")); }
public static void main(String[] args) throws Exception { Moshi moshi = new Moshi.Builder() .add(FallbackEnumJsonAdapter.FACTORY) .build(); JsonAdapter<Example> adapter = moshi.adapter(Example.class); System.out.println(adapter.fromJson("{\"transportation\":\"CARS\"}")); }
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 { String json = "\"TW9zaGksIE9saXZlLCBXaGl0ZSBDaGluPw\""; Moshi moshi = new Moshi.Builder() .add(ByteString.class, new Base64ByteStringAdapter()) .build(); JsonAdapter<ByteString> jsonAdapter = moshi.adapter(ByteString.class); ByteString byteString = jsonAdapter.fromJson(json); System.out.println(byteString); }
public void run() throws Exception { String json = "[\"DIAMONDS\", \"STARS\", \"HEARTS\"]"; Moshi moshi = new Moshi.Builder() .add(DefaultOnDataMismatchAdapter.newFactory(Suit.class, Suit.CLUBS)) .build(); JsonAdapter<List<Suit>> jsonAdapter = moshi.adapter( Types.newParameterizedType(List.class, Suit.class)); List<Suit> suits = jsonAdapter.fromJson(json); System.out.println(suits); }