Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,16 @@ public void nullObjectIsNull() throws IOException {
assertThat(adapter.toJson(value)).isEqualTo("{\"i\":5}");
}

@Test
public void privateRecordSerializesCorrectly() throws IOException {
var adapter = moshi.adapter(PrivateRecord.class);
String json = "{\"id\":\"1\",\"value\":123}";
PrivateRecord privateRecord = new PrivateRecord("1", 123);
assertThat(adapter.fromJson(json)).isEqualTo(privateRecord);
assertThat(adapter.toJson(privateRecord)).isEqualTo(json);
}

private record PrivateRecord(String id, int value) {}

public record AbsentValues(String s, int i) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ internal class RecordJsonAdapter<T>(

val components = rawType.recordComponents
val bindings = LinkedHashMap<String, ComponentBinding<Any?>>()
val lookup = MethodHandles.lookup()
var lookup = MethodHandles.lookup()
try {
lookup = MethodHandles.privateLookupIn(rawType, lookup)
} catch (_: IllegalAccessException) {
// fallback to standard lookup
}
val componentRawTypes =
Array<Class<*>>(components.size) { i ->
val component = components[i]
Expand Down