Refine search
private PendingCommandSerializer() { Moshi moshi = new Moshi.Builder().build(); HashMap<String, JsonAdapter<? extends PendingCommand>> adapters = new HashMap<>(); adapters.put(MessagingControllerCommands.COMMAND_MOVE_OR_COPY, moshi.adapter(PendingMoveOrCopy.class)); adapters.put(MessagingControllerCommands.COMMAND_APPEND, moshi.adapter(PendingAppend.class)); adapters.put(MessagingControllerCommands.COMMAND_EMPTY_TRASH, moshi.adapter(PendingEmptyTrash.class)); adapters.put(MessagingControllerCommands.COMMAND_EXPUNGE, moshi.adapter(PendingExpunge.class)); adapters.put(MessagingControllerCommands.COMMAND_MARK_ALL_AS_READ, moshi.adapter(PendingMarkAllAsRead.class)); adapters.put(MessagingControllerCommands.COMMAND_SET_FLAG, moshi.adapter(PendingSetFlag.class)); this.adapters = Collections.unmodifiableMap(adapters); }
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; }
@Test public void factorySupportsType() throws Exception { Type parameterized = Types.newParameterizedType(List.class, String.class); List<String> fallback = Collections.singletonList("test"); // Build a moshi instance using the adapter under test and one that will throw on each read. Moshi moshi = buildMoshi(DefaultOnDataMismatchAdapter.newFactory(parameterized, fallback)) .newBuilder() .add(new JsonAdapter.Factory() { @Override public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) { final JsonAdapter<Object> next = moshi.nextAdapter(this, type, annotations); return new JsonAdapter<Object>() { @Override public Object fromJson(JsonReader reader) throws IOException { throw new JsonDataException("Fail for all types"); } @Override public void toJson(JsonWriter writer, Object value) throws IOException { next.toJson(writer, value); } }; } }) .build(); JsonAdapter<List<String>> adapter = moshi.adapter(parameterized); List<String> fromJson = adapter.fromJson("[]"); assertThat(fromJson) .isEqualTo(fallback) .containsExactly("test"); String toJson = adapter.toJson(fromJson); assertThat(toJson).isEqualTo("[\"test\"]"); }
/** Create an instance using a default {@link Moshi} instance for conversion. */ public static MoshiConverterFactory create() { return create(new Moshi.Builder().build()); }
/** Returns a new builder containing all custom factories used by the current instance. */ @CheckReturnValue public Moshi.Builder newBuilder() { int fullSize = factories.size(); int tailSize = BUILT_IN_FACTORIES.size(); List<JsonAdapter.Factory> customFactories = factories.subList(0, fullSize - tailSize); return new Builder().addAll(customFactories); }
server.enqueue(new MockResponse().setBody("{\"name\": \"Gson\"}")); com.squareup.moshi.Moshi moshi = new com.squareup.moshi.Moshi.Builder().build(); com.google.gson.Gson gson = new GsonBuilder().create(); MoshiConverterFactory moshiConverterFactory = MoshiConverterFactory.create(moshi);
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 Builder add(Object adapter) { if (adapter == null) throw new IllegalArgumentException("adapter == null"); return add(AdapterMethodsFactory.get(adapter)); }
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; } }); }
Retrofit createRetrofit(OkHttpClient client, Session session) { Moshi moshi = new Moshi.Builder().add(new BigDecimalAdapter()).build(); return new Retrofit.Builder() .addConverterFactory(MoshiConverterFactory.create(moshi)) .baseUrl(session.getAuthenticator().getSessionConfiguration().getEndpointHost()) .client(client) .build(); } }
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; } }); }
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); } }
@Before public void setUp() throws Exception { Moshi moshi = new Moshi.Builder().add(new BigDecimalAdapter()).build(); service = new Retrofit.Builder() .addConverterFactory(MoshiConverterFactory.create(moshi)) .client(new OkHttpClient.Builder() .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build()) .baseUrl("http://localhost:" + wireMockRule.port()) .build() .create(RidesService.class); }
@Provides @Singleton static Moshi provideMoshi() { return new Moshi.Builder() .add(new KotlinJsonAdapterFactory()) .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); } }
private Moshi buildMoshi(JsonAdapter.Factory factory) { return new Moshi.Builder() .add(factory) .build(); }
/** Returns a new builder containing all custom factories used by the current instance. */ @CheckReturnValue public Moshi.Builder newBuilder() { int fullSize = factories.size(); int tailSize = BUILT_IN_FACTORIES.size(); List<JsonAdapter.Factory> customFactories = factories.subList(0, fullSize - tailSize); return new Builder().addAll(customFactories); }
Retrofit createRetrofit(OkHttpClient client, Session session) { Moshi moshi = new Moshi.Builder().add(new BigDecimalAdapter()).build(); return new Retrofit.Builder() .addConverterFactory(MoshiConverterFactory.create(moshi)) .baseUrl(session.getAuthenticator().getSessionConfiguration().getEndpointHost()) .client(client) .build(); } }
@Before public void setUp() throws Exception { moshi = new Moshi.Builder().build(); converter = new Converter(moshi); }
/** Create an instance using a default {@link Moshi} instance for conversion. */ public static MoshiConverterFactory create() { return create(new Moshi.Builder().build()); }