本文整理匯總了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"));
}
示例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);
}
}
示例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();
}
}
示例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\"]}");
}
示例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}}}");
}
示例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\"]}");
}
示例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);
}
示例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);
}
示例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.
}
示例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();
}
示例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();
}
示例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();
}
}
示例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\"]}");
}
示例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\"]");
}
示例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\"]");
}