Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small records fixes #143

Merged
merged 3 commits into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -102,7 +106,8 @@ public JsonAdapter<?> create(
Constructor<Object> constructor;
try {
//noinspection unchecked
constructor = (Constructor<Object>) rawType.getConstructor(constructorParams);
constructor = (Constructor<Object>) rawType.getDeclaredConstructor(constructorParams);
constructor.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
Expand Down
@@ -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 {

Expand All @@ -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) {}