diff --git a/moshi-records-reflect/src/main/java/dev/zacsweers/moshix/records/RecordsJsonAdapterFactory.java b/moshi-records-reflect/src/main/java/dev/zacsweers/moshix/records/RecordsJsonAdapterFactory.java index 4ed61b51..9ad14ec0 100644 --- a/moshi-records-reflect/src/main/java/dev/zacsweers/moshix/records/RecordsJsonAdapterFactory.java +++ b/moshi-records-reflect/src/main/java/dev/zacsweers/moshix/records/RecordsJsonAdapterFactory.java @@ -17,6 +17,7 @@ import java.lang.reflect.RecordComponent; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; @@ -85,7 +86,10 @@ public JsonAdapter create( } } } - var adapter = moshi.adapter(componentType, annotations); + if (qualifiers == null) { + qualifiers = Collections.emptySet(); + } + var adapter = moshi.adapter(componentType, qualifiers); var accessor = component.getAccessor(); var componentBinding = new ComponentBinding<>(name, jsonName, adapter, accessor); var replaced = bindings.put(jsonName, componentBinding); @@ -102,7 +106,8 @@ public JsonAdapter create( Constructor constructor; try { //noinspection unchecked - constructor = (Constructor) rawType.getConstructor(constructorParams); + constructor = (Constructor) rawType.getDeclaredConstructor(constructorParams); + constructor.setAccessible(true); } catch (NoSuchMethodException e) { throw new AssertionError(e); } diff --git a/moshi-sealed/sealed-interfaces-samples/java/src/test/java/dev/zacsweers/moshix/sealed/sample/java/RecordJsonAdapterFactoryTest.java b/moshi-sealed/sealed-interfaces-samples/java/src/test/java/dev/zacsweers/moshix/sealed/sample/java/RecordJsonAdapterFactoryTest.java index 51dfdbc9..6eeb6658 100644 --- a/moshi-sealed/sealed-interfaces-samples/java/src/test/java/dev/zacsweers/moshix/sealed/sample/java/RecordJsonAdapterFactoryTest.java +++ b/moshi-sealed/sealed-interfaces-samples/java/src/test/java/dev/zacsweers/moshix/sealed/sample/java/RecordJsonAdapterFactoryTest.java @@ -1,15 +1,21 @@ package dev.zacsweers.moshix.sealed.sample.java; +import com.squareup.moshi.FromJson; +import com.squareup.moshi.Json; +import com.squareup.moshi.JsonQualifier; import com.squareup.moshi.Moshi; +import com.squareup.moshi.ToJson; import com.squareup.moshi.Types; import org.junit.Test; import java.io.IOException; +import java.lang.annotation.Retention; import dev.zacsweers.moshix.records.RecordsJsonAdapterFactory; import static com.google.common.truth.Truth.assertThat; +import static java.lang.annotation.RetentionPolicy.RUNTIME; public final class RecordJsonAdapterFactoryTest { @@ -33,4 +39,39 @@ public void genericBoundedRecord() throws IOException { .isEqualTo(new GenericBoundedRecord<>(4)); } + @Test public void qualifiedValues() throws IOException { + var adapter = moshi + .newBuilder() + .add(new ColorAdapter()) + .build() + .adapter(QualifiedValues.class); + assertThat(adapter.fromJson("{\"value\":\"#ff0000\"}")).isEqualTo(new QualifiedValues(16711680)); + } + + @Test public void jsonName() throws IOException { + var adapter = moshi.adapter(JsonName.class); + assertThat(adapter.fromJson("{\"actualValue\":3}")).isEqualTo(new JsonName(3)); + } +} + +record QualifiedValues(@HexColor int value) {} + +@Retention(RUNTIME) +@JsonQualifier +@interface HexColor { } + +/** Converts strings like #ff0000 to the corresponding color ints. */ +class ColorAdapter { + @ToJson + String toJson(@HexColor int rgb) { + return String.format("#%06x", rgb); + } + + @FromJson + @HexColor int fromJson(String rgb) { + return Integer.parseInt(rgb.substring(1), 16); + } +} + +record JsonName(@Json(name = "actualValue") int value) {}