Skip to content

Commit

Permalink
[json-node] Fix NULL value bug with JsonObject
Browse files Browse the repository at this point in the history
Also fix DecimalAdapter
  • Loading branch information
rbygrave committed Dec 16, 2024
1 parent ab3cc87 commit 295c877
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
import io.avaje.json.JsonWriter;
import io.avaje.json.node.JsonLong;
import io.avaje.json.node.JsonDecimal;

final class DecimalAdapter implements JsonAdapter<JsonLong> {
final class DecimalAdapter implements JsonAdapter<JsonDecimal> {

@Override
public void toJson(JsonWriter writer, JsonLong value) {
writer.value(value.longValue());
public void toJson(JsonWriter writer, JsonDecimal value) {
writer.value(value.decimalValue());
}

@Override
public JsonLong fromJson(JsonReader reader) {
return JsonLong.of(reader.readLong());
public JsonDecimal fromJson(JsonReader reader) {
return JsonDecimal.of(reader.readDecimal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ObjectAdapter objectAdapter() {
public JsonNode fromJson(JsonReader reader) {
switch (reader.currentToken()) {
case NULL:
reader.isNullValue();
return null;
case BEGIN_ARRAY:
return arrayAdapter.fromJson(reader);
Expand All @@ -55,8 +56,7 @@ public void toJson(JsonWriter writer, JsonNode value) {
writer.nullValue();
return;
}
JsonNode.Type type = value.type();
switch (type) {
switch (value.type()) {
case NULL:
writer.nullValue();
break;
Expand Down
11 changes: 10 additions & 1 deletion json-node/src/test/java/io/avaje/json/node/JsonObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JsonObjectTest {

@Test
void of() {
Map<String,JsonNode> map = new LinkedHashMap<>();
Map<String, JsonNode> map = new LinkedHashMap<>();
map.put("name", JsonString.of("foo"));
map.put("other", JsonInteger.of(42));

Expand Down Expand Up @@ -190,4 +190,13 @@ void toPlain() {
assertThat(plainMap.get("name")).isEqualTo("foo");
assertThat(plainMap.get("other")).isEqualTo(Map.of("b", 42));
}

@Test
void nullValuesInMap() {
String s = "{\"a\":1,\"b\":null,\"c\":null,\"d\":4}";
JsonNodeMapper mapper = JsonNodeMapper.builder().build();
JsonObject jsonObject = mapper.fromJsonObject(s);

assertThat(jsonObject.elements().keySet()).containsExactly("a", "b", "c", "d");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
import io.avaje.json.node.*;
import io.avaje.json.simple.SimpleMapper;
import io.avaje.json.stream.JsonStream;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

Expand Down Expand Up @@ -67,9 +69,15 @@ void create_JsonDouble_expect_sameInstance() {

@Test
void create_JsonDecimal_expect_sameInstance() {
JsonAdapter<?> jsonAdapter = mapper.adapter(JsonDecimal.class);
JsonAdapter<JsonDecimal> jsonAdapter = mapper.adapter(JsonDecimal.class);
JsonAdapter<JsonDecimal> adapter = mapper.adapter(JsonDecimal.class);
assertThat(jsonAdapter).isSameAs(adapter);

SimpleMapper.Type<JsonDecimal> type = mapper.type(jsonAdapter);

String asJson1 = type.toJson(JsonDecimal.of(new BigDecimal("23.45")));
assertThat(asJson1).isEqualTo("23.45");
assertThat(type.fromJson(asJson1)).isEqualTo(JsonDecimal.of(new BigDecimal("23.45")));
}

@Test
Expand Down

0 comments on commit 295c877

Please sign in to comment.