com.squareup.moshi.Moshi.newBuilder()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(2.5k)|赞(0)|评价(0)|浏览(106)

本文整理了Java中com.squareup.moshi.Moshi.newBuilder()方法的一些代码示例,展示了Moshi.newBuilder()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Moshi.newBuilder()方法的具体详情如下:
包路径:com.squareup.moshi.Moshi
类名称:Moshi
方法名:newBuilder

Moshi.newBuilder介绍

[英]Returns a new builder containing all custom factories used by the current instance.
[中]返回包含当前实例使用的所有自定义工厂的新生成器。

代码示例

代码示例来源:origin: xing/xing-android-sdk

/**
 * Adds a moshi instance to build on <b>top</b>.
 * <p>
 * <b>DISCLAIMER: </b> This will extract all custom adapter factories declared in the provided instance
 * and create a builder to which internal declared factories will be added. Keep in mind that for {@linkplain
 * Moshi} the order of custom factories maters and the resulting object will favor factories from the
 * provided {@linkplain Moshi moshi}, which can brake expected behaviour. This means that by adding your own
 * {@link Moshi} you may override adapters declared internally by {@link XingApi}, but you should do that at
 * your own risk.
 * <p>
 * <b>NOTE: </b>This method can be called only once, otherwise an exception will be thrown.
 *
 * @throws java.lang.IllegalStateException If the internal builder was already initialized.
 */
public final T moshi(Moshi moshi) {
  stateNull(moshiBuilder, "Only one instance of Moshi is allowed");
  moshiBuilder = checkNotNull(moshi, "moshi == null").newBuilder();
  return self();
}

代码示例来源:origin: serj-lotutovici/moshi-lazy-adapters

@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\"]");
}

相关文章