Skip to content
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.
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
@@ -0,0 +1,8 @@
package org.example.customer.generics;

import java.util.List;

import io.avaje.jsonb.Json;

@Json
public record MyListWrapperRecord<T>(List<T> list) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.example.customer.generics;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.example.customer.Address;
import org.junit.jupiter.api.Test;

import io.avaje.jsonb.Jsonb;
import io.avaje.jsonb.Types;

class MyListWrapperRecordTest {

Jsonb jsonb = Jsonb.instance();

private static MyListWrapperRecord<Address> createData() {
return new MyListWrapperRecord<>(List.of(new Address(90L, "one"), new Address(91L, "two")));
}

@SuppressWarnings("unchecked")
@Test
void toJson() {
var bean = createData();

var type = jsonb.type(MyListWrapperRecord.class);

String asJson = type.toJson(bean);
assertThat(asJson)
.isEqualTo("{\"list\":[{\"id\":90,\"street\":\"one\"},{\"id\":91,\"street\":\"two\"}]}");

var jsonOfParams =
jsonb.type(Types.newParameterizedType(MyListWrapperRecord.class, Address.class));

var wrapper = (MyListWrapperRecord<Address>) jsonOfParams.fromJson(asJson);
assertThat(wrapper.list()).hasSize(2);
assertThat(wrapper.list().get(0)).isInstanceOf(Address.class);
assertThat(wrapper.list().get(1)).isInstanceOf(Address.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.avaje.jsonb.generator;

import javax.lang.model.type.TypeMirror;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import javax.lang.model.type.TypeMirror;

final class FieldProperty {

private final boolean raw;
Expand Down Expand Up @@ -159,16 +160,19 @@ private String initShortName() {
return Util.initLower(genericType.shortName()) + "JsonAdapter";
}

String typeParamToObject(String shortType) {
String typeParamToObject() {
var shortType = genericType.shortType();
for (final String typeParam : genericTypeParams) {
if (shortType.equals(typeParam)) {
return "Object";
}
if (shortType.contains("<" + typeParam + ">")) {
shortType = shortType.replace("<" + typeParam + ">", "<Object>");
}
}
return shortType;
}


boolean typeObjectBooleanWithIsPrefix() {
return nameHasIsPrefix() && isObjectBoolean();
}
Expand All @@ -182,7 +186,7 @@ private boolean isObjectBoolean() {
}

private boolean isBoolean() {
return ("boolean".equals(genericType.topType()) || "java.lang.Boolean".equals(genericType.topType()));
return "boolean".equals(genericType.topType()) || "java.lang.Boolean".equals(genericType.topType());
}

private boolean nameHasIsPrefix() {
Expand Down Expand Up @@ -279,7 +283,7 @@ void writeFromJsonVariables(Append writer, String num) {
if (unmapped) {
return;
}
final String shortType = typeParamToObject(genericType.shortType());
final String shortType = typeParamToObject();
writer.append(" %s _val$%s = %s;", pad(shortType), fieldName + num, defaultValue);
if (!constructorParam && !optional) {
writer.append(" boolean _set$%s = false;", fieldName + num);
Expand All @@ -288,7 +292,7 @@ void writeFromJsonVariables(Append writer, String num) {
}

void writeFromJsonVariablesRecord(Append writer, String num) {
final String type = genericTypeParameter ? "Object" : genericType.shortType();
final String type = typeParamToObject();
writer.append(" %s _val$%s = %s;", pad(type), fieldName + num, defaultValue).eol();
}

Expand Down