Java Code Examples for com.squareup.moshi.JsonReader#of()
The following examples show how to use
com.squareup.moshi.JsonReader#of() .
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: PolymorphicJsonAdapterFactoryTest.java From moshi with Apache License 2.0 | 6 votes |
@Test public void unregisteredLabelValue() 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("{\"type\":\"data\",\"value\":\"Okay!\"}")); try { adapter.fromJson(reader); fail(); } catch (JsonDataException expected) { assertThat(expected).hasMessage("Expected one of [success, error] for key 'type' but found" + " 'data'. Register a subtype for this label."); } assertThat(reader.peek()).isEqualTo(JsonReader.Token.BEGIN_OBJECT); }
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: ModelListAdapterTest.java From android with Apache License 2.0 | 5 votes |
@Test public void testModelListAdapter() throws IOException { Buffer buffer = new Buffer(); JsonWriter jsonWriter = JsonWriter.of(buffer); JsonAdapter<List<Nation>> adapter = moshi.adapter(Types.newParameterizedType(List.class, Nation.class)); Nation nation = Nation.create(1, 1, "foo", "bar", "baz"); List<Nation> nations = Collections.singletonList(nation); adapter.toJson(jsonWriter, nations); String json = buffer.readUtf8(); buffer = new Buffer().writeUtf8(json); JsonReader jsonReader = JsonReader.of(buffer); assertThat(adapter.fromJson(jsonReader)).isEqualTo(nations); }
Example 4
Source File: PolymorphicJsonAdapterFactoryTest.java From moshi with Apache License 2.0 | 5 votes |
@Test public void nullSafe() 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("null")); assertThat(adapter.fromJson(reader)).isNull(); assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT); }
Example 5
Source File: EnumJsonAdapterTest.java From moshi with Apache License 2.0 | 5 votes |
@Test public void withoutFallbackValue() throws Exception { EnumJsonAdapter<Roshambo> adapter = EnumJsonAdapter.create(Roshambo.class); JsonReader reader = JsonReader.of(new Buffer().writeUtf8("\"SPOCK\"")); try { adapter.fromJson(reader); fail(); } catch (JsonDataException expected) { assertThat(expected).hasMessage( "Expected one of [ROCK, PAPER, scr] but was SPOCK at path $"); } assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT); }
Example 6
Source File: EnumJsonAdapterTest.java From moshi with Apache License 2.0 | 5 votes |
@Test public void withFallbackValue() throws Exception { EnumJsonAdapter<Roshambo> adapter = EnumJsonAdapter.create(Roshambo.class) .withUnknownFallback(Roshambo.ROCK); JsonReader reader = JsonReader.of(new Buffer().writeUtf8("\"SPOCK\"")); assertThat(adapter.fromJson(reader)).isEqualTo(Roshambo.ROCK); assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT); }
Example 7
Source File: EnumJsonAdapterTest.java From moshi with Apache License 2.0 | 5 votes |
@Test public void withNullFallbackValue() throws Exception { EnumJsonAdapter<Roshambo> adapter = EnumJsonAdapter.create(Roshambo.class) .withUnknownFallback(null); JsonReader reader = JsonReader.of(new Buffer().writeUtf8("\"SPOCK\"")); assertThat(adapter.fromJson(reader)).isNull(); assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT); }