Skip to content

Commit 3366905

Browse files
authored
Merge pull request #94 from avaje/fix/93-Map-Serialisation-Error
Fix for #93 Map Serialisation Error
2 parents a2a16b2 + 6afb60f commit 3366905

File tree

23 files changed

+223
-150
lines changed

23 files changed

+223
-150
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.example.customer;
2+
3+
import io.avaje.jsonb.Json;
4+
5+
import java.util.Map;
6+
7+
@Json
8+
public class ALBResponse {
9+
10+
private final int statusCode;
11+
private final boolean isBase64Encoded;
12+
private final Map<String, String> headers;
13+
private final String body;
14+
15+
public ALBResponse(int statusCode, boolean isBase64Encoded, Map<String, String> headers, String body) {
16+
this.statusCode = statusCode;
17+
this.isBase64Encoded = isBase64Encoded;
18+
this.headers = headers;
19+
this.body = body;
20+
}
21+
22+
public int getStatusCode() {
23+
return statusCode;
24+
}
25+
26+
public boolean isBase64Encoded() {
27+
return isBase64Encoded;
28+
}
29+
30+
public Map<String, String> getHeaders() {
31+
return headers;
32+
}
33+
34+
public String getBody() {
35+
return body;
36+
}
37+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.example.customer;
2+
3+
import io.avaje.jsonb.Json;
4+
5+
import java.util.Map;
6+
7+
@Json
8+
public record ALBResponse2 (int one, Map<String, String> map1, int two, Map<String, String> map2, String three) {
9+
10+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.example.customer;
2+
3+
import io.avaje.jsonb.JsonType;
4+
import io.avaje.jsonb.Jsonb;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Map;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
class ALBResponseTest {
12+
13+
private final Jsonb jsonb = Jsonb.builder().build();
14+
15+
@Test
16+
void toJson() {
17+
ALBResponse response = new ALBResponse(200, false, Map.of("foo", "bar"), "content");
18+
19+
JsonType<ALBResponse> type = jsonb.type(ALBResponse.class);
20+
String asJson = type.toJson(response);
21+
22+
ALBResponse response1 = type.fromJson(asJson);
23+
assertThat(response1.getStatusCode()).isEqualTo(response1.getStatusCode());
24+
assertThat(response1.getBody()).isEqualTo("content");
25+
assertThat(response1.isBase64Encoded()).isFalse();
26+
assertThat(response1.getHeaders()).hasSize(1);
27+
assertThat(response1.getHeaders()).containsEntry("foo", "bar");
28+
}
29+
30+
@Test
31+
void toJsonTwoScalarMaps() {
32+
ALBResponse2 response = new ALBResponse2(10, Map.of("foo", "bar"), 20, Map.of("baz", "waz"), "content");
33+
34+
JsonType<ALBResponse2> type = jsonb.type(ALBResponse2.class);
35+
String asJson = type.toJson(response);
36+
37+
ALBResponse2 response1 = type.fromJson(asJson);
38+
assertThat(response1).isEqualTo(response);
39+
}
40+
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ public void writeToJson(Append writer) {
264264
writer.eol();
265265
writer.append(" @Override").eol();
266266
writer.append(" public void toJson(JsonWriter writer, %s %s) {", shortName, varName).eol();
267-
writer.append(" writer.beginObject();").eol();
268-
writer.append(" writer.names(names);").eol();
267+
writer.append(" writer.beginObject(names);").eol();
269268
if (hasSubTypes) {
270269
writeToJsonForSubtypes(writer, varName);
271270
} else {
@@ -418,8 +417,7 @@ String constructorParamName(String name) {
418417
private void writeFromJsonSwitch(Append writer, boolean defaultConstructor, String varName) {
419418
writer.eol();
420419
writer.append(" // read json").eol();
421-
writer.append(" reader.beginObject();").eol();
422-
writer.append(" reader.names(names);").eol();
420+
writer.append(" reader.beginObject(names);").eol();
423421
writer.append(" while (reader.hasNextField()) {").eol();
424422
if (caseInsensitiveKeys) {
425423
writer.append(" final String origFieldName = reader.nextField();").eol();

jsonb-jackson/src/main/java/io/avaje/jsonb/jackson/ArrayStack.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

jsonb-jackson/src/main/java/io/avaje/jsonb/jackson/JacksonNames.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
final class JacksonNames implements PropertyNames {
77

8+
static final JacksonNames EMPTY = new JacksonNames(new String[0]);
9+
810
private static final NameCache NAME_CACHE = new NameCache();
911

1012
private final SerializedString[] keys;

jsonb-jackson/src/main/java/io/avaje/jsonb/jackson/JacksonReader.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ final class JacksonReader implements JsonReader {
2222
this.failOnUnknown = failOnUnknown;
2323
}
2424

25-
@Override
26-
public void names(PropertyNames names) {
27-
// ignore
28-
}
29-
3025
@Override
3126
public void close() {
3227
try {
@@ -214,6 +209,11 @@ public byte[] readBinary() {
214209
}
215210
}
216211

212+
@Override
213+
public void beginObject(PropertyNames names) {
214+
beginObject();
215+
}
216+
217217
@Override
218218
public void beginObject() {
219219
if (parser.currentToken() == JsonToken.START_OBJECT) {

jsonb-jackson/src/main/java/io/avaje/jsonb/jackson/JacksonWriter.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.IOException;
99
import java.math.BigDecimal;
1010
import java.math.BigInteger;
11+
import java.util.ArrayDeque;
1112
import java.util.Collection;
1213
import java.util.List;
1314
import java.util.Map;
@@ -18,7 +19,7 @@ final class JacksonWriter implements JsonWriter {
1819
private boolean serializeEmpty;
1920
private boolean serializeNulls;
2021
private String deferredName;
21-
private final ArrayStack<JacksonNames> nameStack = new ArrayStack<>();
22+
private final ArrayDeque<JacksonNames> nameStack = new ArrayDeque<>();
2223
private JacksonNames currentNames;
2324
private boolean allNames;
2425
private int namePos = -1;
@@ -114,8 +115,7 @@ public void endArray() {
114115
}
115116
}
116117

117-
@Override
118-
public void beginObject() {
118+
private void writeBeginObject() {
119119
try {
120120
writeDeferredName();
121121
generator.writeStartObject();
@@ -124,12 +124,30 @@ public void beginObject() {
124124
}
125125
}
126126

127+
@Override
128+
public void beginObject() {
129+
writeBeginObject();
130+
if (currentNames != null && !allNames) {
131+
nameStack.addFirst(currentNames);
132+
currentNames = JacksonNames.EMPTY;
133+
}
134+
}
135+
136+
@Override
137+
public void beginObject(PropertyNames names) {
138+
writeBeginObject();
139+
if (currentNames != null) {
140+
nameStack.addFirst(currentNames);
141+
}
142+
currentNames = (JacksonNames) names;
143+
}
144+
127145
@Override
128146
public void endObject() {
129147
try {
130148
generator.writeEndObject();
131149
if (!allNames) {
132-
currentNames = nameStack.pop();
150+
currentNames = nameStack.pollFirst();
133151
}
134152
} catch (IOException e) {
135153
throw new JsonIoException(e);
@@ -147,14 +165,6 @@ public void allNames(PropertyNames names) {
147165
currentNames = (JacksonNames) names;
148166
}
149167

150-
@Override
151-
public void names(PropertyNames names) {
152-
if (currentNames != null) {
153-
nameStack.push(currentNames);
154-
}
155-
currentNames = (JacksonNames) names;
156-
}
157-
158168
@Override
159169
public void name(int position) {
160170
this.namePos = position;

jsonb-jackson/src/test/java/org/example/AddressJsonAdapter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public void build(ViewBuilder builder, String name, MethodHandle mh) {
4141

4242
@Override
4343
public void toJson(JsonWriter writer, Address address) {
44-
writer.beginObject();
45-
writer.names(names);
44+
writer.beginObject(names);
4645
writer.name(0);
4746
stringAdapter.toJson(writer, address.street());
4847
writer.name(1);

jsonb-jackson/src/test/java/org/example/CustomerJsonAdapter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public void build(ViewBuilder builder, String name, MethodHandle handle) {
5050

5151
@Override
5252
public void toJson(JsonWriter writer, Customer customer) {
53-
writer.beginObject();
54-
writer.names(names);
53+
writer.beginObject(names);
5554
writer.name( 0);
5655
intAdapter.toJson(writer, customer.id());
5756
writer.name( 1);

0 commit comments

Comments
 (0)