Skip to content

Commit c8066f5

Browse files
authored
Merge pull request #406 from avaje/feature/collection-types-specific
For collection types List, Set, Map use equals() rather than isAssign…
2 parents caf622f + c69703a commit c8066f5

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.example.customer.stream;
2+
3+
import io.avaje.jsonb.Json;
4+
5+
import java.util.ArrayList;
6+
7+
@Json
8+
public record MyArrayList(int id, ArrayList<String> names) {
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.example.customer.stream;
2+
3+
import io.avaje.jsonb.Json;
4+
5+
import java.util.LinkedHashSet;
6+
7+
@Json
8+
public record MyLinked(int id, LinkedHashSet<String> names) {
9+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.example.customer.stream;
2+
3+
import io.avaje.jsonb.Jsonb;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.ArrayList;
7+
import java.util.LinkedHashSet;
8+
import java.util.List;
9+
import java.util.Set;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
15+
class MyArrayListTest {
16+
17+
Jsonb jsonb = Jsonb.builder().build();
18+
19+
@Test
20+
void toJson_fromJson() {
21+
MyArrayList myLinked = new MyArrayList(1, new ArrayList<>(List.of("a", "b", "c")));
22+
23+
String json = jsonb.toJson(myLinked);
24+
assertNotNull(json);
25+
assertThat(json).isEqualTo("{\"id\":1,\"names\":[\"a\",\"b\",\"c\"]}");
26+
27+
MyArrayList fromJson = jsonb.type(MyArrayList.class).fromJson(json);
28+
assertEquals(myLinked, fromJson);
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.example.customer.stream;
2+
3+
import io.avaje.jsonb.Jsonb;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.LinkedHashSet;
7+
import java.util.List;
8+
import java.util.Set;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
class MyLinkedHashSetTest {
14+
15+
Jsonb jsonb = Jsonb.builder().build();
16+
17+
@Test
18+
void toJson_fromJson() {
19+
MyLinked myLinked = new MyLinked(1, new LinkedHashSet<>(List.of("a", "b", "c")));
20+
21+
String json = jsonb.toJson(myLinked);
22+
assertNotNull(json);
23+
assertThat(json).isEqualTo("{\"id\":1,\"names\":[\"a\",\"b\",\"c\"]}");
24+
25+
MyLinked fromJson = jsonb.type(MyLinked.class).fromJson(json);
26+
assertEquals(myLinked, fromJson);
27+
}
28+
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/GenericType.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,22 @@ private String asTypeBasic() {
167167
if (adapterType != null) {
168168
return adapterType;
169169
}
170-
return Util.shortName(topType)+".class";
170+
return Util.shortName(topType) + ".class";
171171
}
172172

173173
private String asTypeContainer() {
174174
GenericType param = params.get(0);
175175
String containerType = topType();
176-
if (isAssignable(containerType, "java.util.List")) {
176+
if ("java.util.List".equals(containerType) || "java.util.ArrayList".equals(containerType)) {
177177
return "Types.listOf(" + Util.shortName(param.topType()) + ".class)";
178178
}
179-
if (isAssignable(containerType, "java.util.Set")) {
179+
if ("java.util.Set".equals(containerType) || "java.util.LinkedHashSet".equals(containerType)) {
180180
return "Types.setOf(" + Util.shortName(param.topType()) + ".class)";
181181
}
182-
if (isAssignable(containerType, "java.util.stream.Stream")) {
182+
if ("java.util.stream.Stream".equals(containerType)) {
183183
return "Types.streamOf(" + Util.shortName(param.topType()) + ".class)";
184184
}
185-
if (isAssignable(containerType, "java.util.Optional")) {
185+
if ("java.util.Optional".equals(containerType)) {
186186
return "Types.optionalOf(" + Util.shortName(param.topType()) + ".class)";
187187
}
188188
return null;

0 commit comments

Comments
 (0)