Skip to content

Commit

Permalink
Small records fixes (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers committed Aug 20, 2021
1 parent edc5994 commit e4c42ac
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
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) {}

0 comments on commit e4c42ac

Please sign in to comment.