### Question:
ClassJsonAdapter extends JsonAdapter<T> { @Override public T fromJson(JsonReader reader) throws IOException { T result; try { result = classFactory.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw Util.rethrowCause(e); } catch (IllegalAccessException e) { throw new AssertionError(); } try { reader.beginObject(); while (reader.hasNext()) { int index = reader.selectName(options); if (index == -1) { reader.skipName(); reader.skipValue(); continue; } fieldsArray[index].read(reader, result); } reader.endObject(); return result; } catch (IllegalAccessException e) { throw new AssertionError(); } } ClassJsonAdapter(ClassFactory<T> classFactory, Map<String, FieldBinding<?>> fieldsMap); @Override T fromJson(JsonReader reader); @Override void toJson(JsonWriter writer, T value); @Override String toString(); static final JsonAdapter.Factory FACTORY; }### Answer:
@Test public void noArgConstructor() throws Exception { NoArgConstructor fromJson = fromJson(NoArgConstructor.class, "{\"b\":8}"); assertThat(fromJson.a).isEqualTo(5); assertThat(fromJson.b).isEqualTo(8); }
@Test public void noArgConstructorThrowsCheckedException() throws Exception { try { fromJson(NoArgConstructorThrowsCheckedException.class, "{}"); fail(); } catch (RuntimeException expected) { assertThat(expected.getCause()).hasMessageThat().isEqualTo("foo"); } }
@Test public void noArgConstructorThrowsUncheckedException() throws Exception { try { fromJson(NoArgConstructorThrowsUncheckedException.class, "{}"); fail(); } catch (UnsupportedOperationException expected) { assertThat(expected).hasMessageThat().isEqualTo("foo"); } }
@Test public void noArgConstructorFieldDefaultsHonored() throws Exception { NoArgConstructorWithDefaultField fromJson = fromJson(NoArgConstructorWithDefaultField.class, "{\"b\":8}"); assertThat(fromJson.a).isEqualTo(5); assertThat(fromJson.b).isEqualTo(8); }
@Test public void magicConstructor() throws Exception { MagicConstructor fromJson = fromJson(MagicConstructor.class, "{\"a\":8}"); assertThat(fromJson.a).isEqualTo(8); }
@Test public void magicConstructorFieldDefaultsNotHonored() throws Exception { MagicConstructorWithDefaultField fromJson = fromJson(MagicConstructorWithDefaultField.class, "{\"b\":3}"); assertThat(fromJson.a).isEqualTo(0); assertThat(fromJson.b).isEqualTo(3); }