Java Code Examples for com.squareup.moshi.JsonAdapter#fromJson()
The following examples show how to use
com.squareup.moshi.JsonAdapter#fromJson() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SerializeOnlyNonEmptyJsonAdapterTest.java From moshi-lazy-adapters with Apache License 2.0 | 6 votes |
@Test public void serializesOnlyNonEmptyCustomArray() throws Exception { JsonAdapter<Data1> adapter = moshi.adapter(Data1.class); Data1 fromJson = adapter.fromJson("{\n" + "\"customArray\": [{" + "\"data\":\"blub\"" + "}]\n" + "}"); assertThat(fromJson.customArray).isNotNull().hasSize(1); assertThat(fromJson.customArray[0].data).isEqualTo("blub"); fromJson.customArray = new CustomType[0]; assertThat(adapter.toJson(fromJson)).isEqualTo("{}"); fromJson.customArray = new CustomType[] { new CustomType("blub") }; assertThat(adapter.toJson(fromJson)).isEqualTo("{\"customArray\":[{\"data\":\"blub\"}]}"); }
Example 2
Source File: PolymorphicJsonAdapterFactoryTest.java From moshi with Apache License 2.0 | 6 votes |
@Test public void nonObjectDoesNotConsume() throws IOException { Moshi moshi = new Moshi.Builder() .add(PolymorphicJsonAdapterFactory.of(Message.class, "type") .withSubtype(Success.class, "success") .withSubtype(Error.class, "error")) .build(); JsonAdapter<Message> adapter = moshi.adapter(Message.class); JsonReader reader = JsonReader.of(new Buffer().writeUtf8("\"Failure\"")); try { adapter.fromJson(reader); fail(); } catch (JsonDataException expected) { assertThat(expected).hasMessage("Expected BEGIN_OBJECT but was STRING at path $"); } assertThat(reader.nextString()).isEqualTo("Failure"); assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT); }
Example 3
Source File: WrappedJsonAdapterTest.java From moshi-lazy-adapters with Apache License 2.0 | 6 votes |
@Test public void failOnNotFound() throws Exception { JsonAdapter<Data2> adapter = moshi.adapter(Data2.class); try { adapter.fromJson("{\n" + " \"data\": {\n" + " \"1\": {\n" + " \"2\": null\n" + " }\n" + " }\n" + "}"); fail(); } catch (JsonDataException ex) { assertThat(ex).hasMessage( "Wrapped Json expected at path: [1, 2]. Found null at $.data.1.2"); } }
Example 4
Source File: BoundParameterQueryTest.java From influxdb-java with MIT License | 6 votes |
@Test public void testGetParameterJsonWithUrlEncoded() throws IOException { BoundParameterQuery query = QueryBuilder.newQuery("SELECT * FROM abc WHERE integer > $i" + "AND double = $d AND bool = $bool AND string = $string AND other = $object") .forDatabase("foobar") .bind("i", 0) .bind("d", 1.0) .bind("bool", true) .bind("string", "test") .bind("object", new Object()) .create(); Moshi moshi = new Moshi.Builder().build(); JsonAdapter<Point> adapter = moshi.adapter(Point.class); Point point = adapter.fromJson(decode(query.getParameterJsonWithUrlEncoded())); Assert.assertEquals(0, point.i); Assert.assertEquals(1.0, point.d, 0.0); Assert.assertEquals(true, point.bool); Assert.assertEquals("test", point.string); Assert.assertTrue(point.object.matches("java.lang.Object@[a-z0-9]+")); }
Example 5
Source File: LazyAdaptersAutoValueTest.java From moshi-lazy-adapters with Apache License 2.0 | 6 votes |
@Test public void unwrap() throws Exception { JsonAdapter<Data> adapter = moshi.adapter(Data.class); Data fromJson = adapter.fromJson("{\n" + " \"name\": \"data_name\",\n" + " \"meta\": {\n" + " \"1\": {\n" + " \"value1\": \"value1\",\n" + " \"value2\": 2\n" + " }\n" + " }\n" + "}"); assertThat(fromJson.name()).isEqualTo("data_name"); assertThat(fromJson.meta().value1).isEqualTo("value1"); assertThat(fromJson.meta().value2).isEqualTo(2); }
Example 6
Source File: MoshiSpeaker.java From Jolyglot with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override public <T> T fromJson(File file, Type typeOfT) throws RuntimeException { BufferedSource bufferedSource = null; try { bufferedSource = Okio.buffer(Okio.source(file)); JsonAdapter<T> jsonAdapter = moshi.adapter(typeOfT); return jsonAdapter.fromJson(JsonReader.of(bufferedSource)); } catch (IOException e) { throw new RuntimeException(e); } finally { if(bufferedSource != null){ try { bufferedSource.close(); } catch (IOException ignored) { } } } }
Example 7
Source File: CustomTypeAdapter.java From moshi with Apache License 2.0 | 6 votes |
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); }
Example 8
Source File: SerializeOnlyJsonAdapterTest.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
@Test public void serializeOnly() throws Exception { JsonAdapter<Data1> adapter = moshi.adapter(Data1.class); Data1 fromJson = adapter.fromJson("{\"data\": \"test\"}"); assertThat(fromJson.data).isNull(); fromJson.data = "1234"; assertThat(adapter.toJson(fromJson)).isEqualTo("{\"data\":\"1234\"}"); }
Example 9
Source File: MoshiMessageBodyReader.java From jax-rs-moshi with Apache License 2.0 | 5 votes |
@Override public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { JsonAdapter<Object> adapter = moshi.adapter(genericType); BufferedSource source = Okio.buffer(Okio.source(entityStream)); if (!source.request(1)) { throw new NoContentException("Stream is empty"); } return adapter.fromJson(source); // Note: we do not close the InputStream per the interface documentation. }
Example 10
Source File: MoshiHelper.java From moshi-jsonapi with MIT License | 5 votes |
public static <T> T nextNullableObject(JsonReader reader, JsonAdapter<T> adapter) throws IOException { if (reader.peek() == JsonReader.Token.NULL) { reader.skipValue(); return null; } else { return adapter.fromJson(reader); } }
Example 11
Source File: JsonBuffer.java From moshi-jsonapi with MIT License | 5 votes |
public <R extends T> R get(JsonAdapter<R> adapter) { try { Buffer buffer = new Buffer(); buffer.write(this.buffer); return adapter.fromJson(buffer); } catch (IOException e) { throw new RuntimeException("JsonBuffer failed to deserialize value with [" + adapter.getClass() + "]", e); } }
Example 12
Source File: WrappedJsonAdapterTest.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
@Test public void fromJsonDoesNotSwallowIOExceptions() throws Exception { JsonAdapter<Data4> adapter = moshi.adapter(Data4.class); try { adapter.fromJson("{\n" + " \"th\": {\n" + " \"1\": \"this_will_throw\"\n" + " }\n" + "}"); fail(); } catch (IOException e) { assertThat(e).hasMessage("ThrowingAdapter.fromJson"); } }
Example 13
Source File: SerializeOnlyNonEmptyJsonAdapterTest.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
@Test public void serializesOnlyNonEmptyLongArray() throws Exception { JsonAdapter<Data1> adapter = moshi.adapter(Data1.class); Data1 fromJson = adapter.fromJson("{\n" + "\"longArray\": [1]\n" + "}"); assertThat(fromJson.longArray).containsExactly(1L); fromJson.longArray = new long[0]; assertThat(adapter.toJson(fromJson)).isEqualTo("{}"); fromJson.longArray = new long[] { 5L }; assertThat(adapter.toJson(fromJson)).isEqualTo("{\"longArray\":[5]}"); }
Example 14
Source File: PolymorphicJsonAdapterFactoryTest.java From moshi with Apache License 2.0 | 5 votes |
@Test public void specifiedNullFallbackSubtype() throws IOException { Moshi moshi = new Moshi.Builder() .add(PolymorphicJsonAdapterFactory.of(Message.class, "type") .withSubtype(Success.class, "success") .withSubtype(Error.class, "error") .withDefaultValue(null)) .build(); JsonAdapter<Message> adapter = moshi.adapter(Message.class); Message message = adapter.fromJson("{\"type\":\"data\",\"value\":\"Okay!\"}"); assertThat(message).isNull(); }
Example 15
Source File: WrappedJsonAdapterTest.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
@Test public void factoryMaintainsOtherAnnotations() throws Exception { JsonAdapter<Data3> adapter = moshi.adapter(Data3.class); Data3 fromJson = adapter.fromJson("{\n" + " \"str\": {\n" + " \"1\": \"test\"\n" + " }\n" + "}"); assertThat(fromJson.str).isEqualTo("testCustom"); String toJson = adapter.toJson(fromJson); assertThat(toJson).isEqualTo("{\"str\":{\"1\":\"test\"}}"); }
Example 16
Source File: MoshiSpeaker.java From Jolyglot with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public <T> T fromJson(String json, Class<T> classOfT) throws RuntimeException { try { JsonAdapter<T> jsonAdapter = moshi.adapter(classOfT); return jsonAdapter.fromJson(json); } catch (IOException e) { throw new RuntimeException(e); } }
Example 17
Source File: SerializeOnlyNonEmptyJsonAdapterTest.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
@Test public void serializesOnlyNonEmptyShortArray() throws Exception { JsonAdapter<Data1> adapter = moshi.adapter(Data1.class); Data1 fromJson = adapter.fromJson("{\n" + "\"shortArray\": [1]\n" + "}"); assertThat(fromJson.shortArray).containsExactly((short) 1); fromJson.shortArray = new short[0]; assertThat(adapter.toJson(fromJson)).isEqualTo("{}"); fromJson.shortArray = new short[] { 5 }; assertThat(adapter.toJson(fromJson)).isEqualTo("{\"shortArray\":[5]}"); }
Example 18
Source File: ElementAtJsonAdapterTest.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
@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\"]}"); }
Example 19
Source File: LazyAdaptersRxJavaTest.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
private static Callable<String> failingCallable() { Set<Annotation> annotations = Collections.<Annotation>singleton(Wrapped.Factory.create(true, "one", "two", "three")); final JsonAdapter<String> adapter = MOSHI.adapter(String.class, annotations); return new Callable<String>() { @Override public String call() throws Exception { return adapter.fromJson("{\n" + " \"one\": {\n" + " \"two\": null\n" + " }\n" + "}"); } }; }
Example 20
Source File: FallbackOnNullJsonAdapterTest.java From moshi-lazy-adapters with Apache License 2.0 | 5 votes |
@Test public void fallbackOnNullIsDelegated() throws Exception { JsonAdapter<AndAnotherInt> adapter = moshi.adapter(AndAnotherInt.class); AndAnotherInt fromJson = adapter.fromJson("{\n" + " \"willFallback\": null\n" + "}"); assertThat(fromJson.willFallback).isEqualTo(2); }