當前位置: 首頁>>代碼示例>>Java>>正文


Java JsonAdapter.toJson方法代碼示例

本文整理匯總了Java中com.squareup.moshi.JsonAdapter.toJson方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonAdapter.toJson方法的具體用法?Java JsonAdapter.toJson怎麽用?Java JsonAdapter.toJson使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.squareup.moshi.JsonAdapter的用法示例。


在下文中一共展示了JsonAdapter.toJson方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: enumValuesAreSerializedCorrectly

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void enumValuesAreSerializedCorrectly() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/typeWithEnumProperty.json", "com.example",
            config("annotationStyle", "moshi1",
                    "propertyWordDelimiters", "_"));

    Class generatedType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty");
    Class enumType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty$EnumProperty");
    Object instance = generatedType.newInstance();

    Method setter = generatedType.getMethod("setEnumProperty", enumType);
    setter.invoke(instance, enumType.getEnumConstants()[3]);

    JsonAdapter jsonAdapter = moshi.adapter(generatedType);
    String json = jsonAdapter.toJson(instance);

    Map<String, String> jsonAsMap = new Gson().fromJson(json, Map.class);
    assertThat(jsonAsMap.get("enum_Property"), is("4 ! 1"));
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:22,代碼來源:Moshi1IT.java

示例2: writeTranslationList

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
void writeTranslationList(TranslationList list) {
  File cacheFile = getCachedFile();
  try {
    File directory = cacheFile.getParentFile();
    boolean directoryExists = directory.mkdirs() || directory.isDirectory();
    if (directoryExists) {
      if (cacheFile.exists()) {
        cacheFile.delete();
      }
      Moshi moshi = new Moshi.Builder().build();
      JsonAdapter<TranslationList> jsonAdapter = moshi.adapter(TranslationList.class);
      BufferedSink sink = Okio.buffer(Okio.sink(cacheFile));
      jsonAdapter.toJson(sink, list);
      sink.close();
      quranSettings.setLastUpdatedTranslationDate(System.currentTimeMillis());
    }
  } catch (Exception e) {
    cacheFile.delete();
    Crashlytics.logException(e);
  }
}
 
開發者ID:Elias33,項目名稱:Quran,代碼行數:22,代碼來源:TranslationManagerPresenter.java

示例3: toJson

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
/**
 * Recursively writes the respective roots forming a json object that resembles the {@code path}
 * wrapping the type of the {@code adapter}.
 */
private static <T> void toJson(JsonAdapter<T> adapter, JsonWriter writer, T value,
    String[] path, int index) throws IOException {
  if (value != null || writer.getSerializeNulls()) {
    if (index == path.length) {
      adapter.toJson(writer, value);
    } else {
      writer.beginObject();
      writer.name(path[index]);
      toJson(adapter, writer, value, path, ++index);
      writer.endObject();
    }
  } else {
    // If we don't propagate the null value the writer will throw.
    writer.nullValue();
  }
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:21,代碼來源:WrappedJsonAdapter.java

示例4: first

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void first() throws Exception {
  JsonAdapter<Data> adapter = moshi.adapter(Data.class);

  Data fromJson = adapter.fromJson("{\n"
      + "  \"obj\": [\n"
      + "    \"one\",\n"
      + "    \"two\"\n"
      + "  ]\n"
      + "}");
  assertThat(fromJson.str).isEqualTo("one");

  String toJson = adapter.toJson(fromJson);
  // The excluded data is lost during parsing
  // Adapter under test assumes that the consumer doesn't need that data
  assertThat(toJson).isEqualTo("{\"obj\":[\"one\"]}");
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:17,代碼來源:FirstElementJsonAdapterTest.java

示例5: notNullSafe

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void notNullSafe() throws Exception {
  JsonAdapter<Data2> adapter = moshi.adapter(Data2.class);

  try {
    adapter.fromJson("{\n"
        + "  \"data\": null\n"
        + "}");
    fail();
  } catch (JsonDataException expected) {
  }

  Data2 data2 = new Data2();
  String toJson = adapter.toJson(data2);
  assertThat(toJson).isEqualTo("{}");

  toJson = adapter.serializeNulls().toJson(data2);
  assertThat(toJson).isEqualTo("{\"data\":{\"1\":{\"2\":null}}}");
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:19,代碼來源:WrappedJsonAdapterTest.java

示例6: last

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void last() throws Exception {
  JsonAdapter<LastElementJsonAdapterTest.Data>
      adapter = moshi.adapter(LastElementJsonAdapterTest.Data.class);

  LastElementJsonAdapterTest.Data fromJson = adapter.fromJson("{\n"
      + "  \"obj\": [\n"
      + "    \"one\",\n"
      + "    \"two\"\n"
      + "  ]\n"
      + "}");
  assertThat(fromJson.str).isEqualTo("two");

  String toJson = adapter.toJson(fromJson);
  // The excluded data is lost during parsing
  // Adapter under test assumes that the consumer doesn't need that data
  assertThat(toJson).isEqualTo("{\"obj\":[\"two\"]}");
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:18,代碼來源:LastElementJsonAdapterTest.java

示例7: serialize

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
public <T extends PendingCommand> String serialize(T command) {
    // noinspection unchecked, we know the map has correctly matching adapters
    JsonAdapter<T> adapter = (JsonAdapter<T>) adapters.get(command.getCommandName());
    if (adapter == null) {
        throw new IllegalArgumentException("Unsupported pending command type!");
    }
    return adapter.toJson(command);
}
 
開發者ID:philipwhiuk,項目名稱:q-mail,代碼行數:9,代碼來源:PendingCommandSerializer.java

示例8: assertJsonRoundTrip

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@SuppressWarnings({"unchecked", "rawtypes"})
private void assertJsonRoundTrip(ClassLoader resultsClassLoader, String className, String jsonResource) throws ClassNotFoundException, IOException {
    Class generatedType = resultsClassLoader.loadClass(className);

    String expectedJson = IOUtils.toString(getClass().getResource(jsonResource));
    JsonAdapter<Object> jsonAdapter = moshi.adapter(generatedType);
    Object javaInstance = jsonAdapter.fromJson(expectedJson);
    String actualJson = jsonAdapter.toJson(javaInstance);

    assertEqualsJson(expectedJson, actualJson);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:12,代碼來源:Moshi1IT.java

示例9: writeTo

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Override public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations,
    MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
    throws IOException, WebApplicationException {
  JsonAdapter<Object> adapter = moshi.adapter(genericType);
  BufferedSink sink = Okio.buffer(Okio.sink(entityStream));
  adapter.toJson(sink, o);
  sink.emit();
  // Note: we do not close the OutputStream per the interface documentation.
}
 
開發者ID:JakeWharton,項目名稱:jax-rs-moshi,代碼行數:10,代碼來源:MoshiMessageBodyWriter.java

示例10: serialize

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Override
public <T> void serialize(Type type, T value, OutputStream outputStream) throws IOException {
  JsonAdapter<Object> adapter = moshi.adapter(type); // no need to cache moshi does this for us.
  BufferedSink buffer = Okio.buffer(Okio.sink(outputStream));
  adapter.toJson(buffer, value);
  buffer.close();
}
 
開發者ID:rogues-dev,項目名稱:hoard,代碼行數:8,代碼來源:MoshiSerializer.java

示例11: setFavorite

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
public void setFavorite(Movie movie)
{
    SharedPreferences.Editor editor = pref.edit();
    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<Movie> jsonAdapter = moshi.adapter(Movie.class);
    String movieJson = jsonAdapter.toJson(movie);
    editor.putString(movie.getId(), movieJson);
    editor.apply();
}
 
開發者ID:rohanoid5,項目名稱:CineBuff,代碼行數:10,代碼來源:FavoritesStore.java

示例12: writeNullableValue

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
public static <T> void writeNullableValue(JsonWriter writer, JsonAdapter<T> adapter, T value) throws IOException {
    if (value != null) {
        adapter.toJson(writer, value);
    } else {
        writer.nullValue();
    }
}
 
開發者ID:kamikat,項目名稱:moshi-jsonapi,代碼行數:8,代碼來源:MoshiHelper.java

示例13: factoryMaintainsOtherAnnotations

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test @Ignore public void factoryMaintainsOtherAnnotations() throws Exception {
  JsonAdapter<Data2> adapter = moshi.adapter(Data2.class);

  Data2 fromJson = adapter.fromJson("{\n"
      + "  \"str\": [\n"
      + "    \"test\"\n"
      + "  ]\n"
      + "}");
  assertThat(fromJson.str).isEqualTo("testCustom");

  String toJson = adapter.toJson(fromJson);
  assertThat(toJson).isEqualTo("{\"str\":[\"test\"]}");
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:14,代碼來源:FirstElementJsonAdapterTest.java

示例14: noNullValues

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void noNullValues() throws Exception {
  JsonAdapter<List<String>> adapter = moshi.adapter(Types.newParameterizedType(List.class,
      String.class), FilterNulls.class);

  List<String> fromJson = adapter.fromJson("[\"apple\",\"banana\"]");
  assertThat(fromJson).containsExactly("apple", "banana");

  String toJson = adapter.toJson(fromJson);
  assertThat(toJson).isEqualTo("[\"apple\",\"banana\"]");
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:11,代碼來源:FilterNullsJsonAdapterTest.java

示例15: nullValues

import com.squareup.moshi.JsonAdapter; //導入方法依賴的package包/類
@Test public void nullValues() throws Exception {
  JsonAdapter<List<String>> adapter = moshi.adapter(Types.newParameterizedType(List.class,
      String.class), FilterNulls.class);

  List<String> fromJson = adapter.fromJson("[\"apple\",\"banana\",null]");
  assertThat(fromJson).containsExactly("apple", "banana");

  String toJson = adapter.toJson(new ArrayList<>(asList("apple", "banana", null)));
  assertThat(toJson).isEqualTo("[\"apple\",\"banana\"]");
}
 
開發者ID:serj-lotutovici,項目名稱:moshi-lazy-adapters,代碼行數:11,代碼來源:FilterNullsJsonAdapterTest.java


注:本文中的com.squareup.moshi.JsonAdapter.toJson方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。