mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 16:39:22 +08:00
Add tests for duplicate JSON keys
This commit is contained in:
@@ -995,6 +995,26 @@ public final class MoshiTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void duplicateKeyDisallowedInObjectType() throws Exception {
|
||||||
|
Moshi moshi = new Moshi.Builder().build();
|
||||||
|
JsonAdapter<Object> adapter = moshi.adapter(Object.class);
|
||||||
|
String json = "{\"diameter\":5,\"diameter\":5,\"extraCheese\":true}";
|
||||||
|
try {
|
||||||
|
adapter.fromJson(json);
|
||||||
|
fail();
|
||||||
|
} catch (JsonDataException expected) {
|
||||||
|
assertThat(expected).hasMessage(
|
||||||
|
"Map key 'diameter' has multiple values at path $.diameter: 5.0 and 5.0");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void duplicateKeysAllowedInCustomType() throws Exception {
|
||||||
|
Moshi moshi = new Moshi.Builder().build();
|
||||||
|
JsonAdapter<Pizza> adapter = moshi.adapter(Pizza.class);
|
||||||
|
String json = "{\"diameter\":5,\"diameter\":5,\"extraCheese\":true}";
|
||||||
|
assertThat(adapter.fromJson(json)).isEqualTo(new Pizza(5, true));
|
||||||
|
}
|
||||||
|
|
||||||
static class Pizza {
|
static class Pizza {
|
||||||
final int diameter;
|
final int diameter;
|
||||||
final boolean extraCheese;
|
final boolean extraCheese;
|
||||||
|
Reference in New Issue
Block a user