diff --git a/jsonb/src/main/java/io/avaje/jsonb/JsonWriter.java b/jsonb/src/main/java/io/avaje/jsonb/JsonWriter.java index f4a177b7..32943c13 100644 --- a/jsonb/src/main/java/io/avaje/jsonb/JsonWriter.java +++ b/jsonb/src/main/java/io/avaje/jsonb/JsonWriter.java @@ -20,6 +20,7 @@ import java.io.Closeable; import java.io.Flushable; import java.math.BigDecimal; +import java.math.BigInteger; /** * Writes json content. @@ -148,24 +149,24 @@ public interface JsonWriter extends Closeable, Flushable { */ void value(BigDecimal value); + /** + * Write a BigInteger value. + */ + void value(BigInteger value); + /** * Write a value that could be any value. */ void jsonValue(Object value); /** - * Write raw encoded json value. + * Flush the writer. */ - void rawValue(String value); + void flush(); /** * Close the writer. */ void close(); - /** - * Flush the writer. - */ - void flush(); - } diff --git a/jsonb/src/main/java/io/avaje/jsonb/core/MathAdapters.java b/jsonb/src/main/java/io/avaje/jsonb/core/MathAdapters.java index 1009331c..b057e353 100644 --- a/jsonb/src/main/java/io/avaje/jsonb/core/MathAdapters.java +++ b/jsonb/src/main/java/io/avaje/jsonb/core/MathAdapters.java @@ -5,7 +5,6 @@ import io.avaje.jsonb.JsonWriter; import io.avaje.jsonb.Jsonb; -import java.io.IOException; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; @@ -35,7 +34,7 @@ public BigDecimal fromJson(JsonReader reader) { @Override public void toJson(JsonWriter writer, BigDecimal value) { - writer.rawValue("\"" + value.toString() + "\""); + writer.value(value.toString()); } @Override @@ -86,7 +85,7 @@ public BigInteger fromJson(JsonReader reader) { @Override public void toJson(JsonWriter writer, BigInteger value) { - writer.rawValue(value.toString()); + writer.value(value); } @Override diff --git a/jsonb/src/main/java/io/avaje/jsonb/jackson/JacksonWriter.java b/jsonb/src/main/java/io/avaje/jsonb/jackson/JacksonWriter.java index 2a5829dd..14263eec 100644 --- a/jsonb/src/main/java/io/avaje/jsonb/jackson/JacksonWriter.java +++ b/jsonb/src/main/java/io/avaje/jsonb/jackson/JacksonWriter.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Collection; import java.util.List; import java.util.Map; @@ -310,13 +311,13 @@ public void value(BigDecimal value) { } @Override - public void rawValue(String value) { + public void value(BigInteger value) { if (value == null) { nullValue(); } else { try { writeDeferredName(); - generator.writeRaw(value); + generator.writeNumber(value); } catch (IOException e) { throw new JsonIoException(e); } diff --git a/jsonb/src/main/java/io/avaje/jsonb/spi/DelegateJsonWriter.java b/jsonb/src/main/java/io/avaje/jsonb/spi/DelegateJsonWriter.java index b2aa96e0..675dc338 100644 --- a/jsonb/src/main/java/io/avaje/jsonb/spi/DelegateJsonWriter.java +++ b/jsonb/src/main/java/io/avaje/jsonb/spi/DelegateJsonWriter.java @@ -3,6 +3,7 @@ import io.avaje.jsonb.JsonWriter; import java.math.BigDecimal; +import java.math.BigInteger; /** * Provides a delegating JsonWriter. @@ -85,11 +86,6 @@ public void nullValue() { delegate.nullValue(); } - @Override - public void rawValue(String value) { - delegate.rawValue(value); - } - @Override public final void value(String value) { delegate.value(value); @@ -140,6 +136,11 @@ public void value(BigDecimal value) { delegate.value(value); } + @Override + public void value(BigInteger value) { + delegate.value(value); + } + @Override public void jsonValue(Object value) { delegate.jsonValue(value); diff --git a/jsonb/src/test/java/io/avaje/jsonb/core/UUIDTest.java b/jsonb/src/test/java/io/avaje/jsonb/core/UUIDTest.java new file mode 100644 index 00000000..764ada52 --- /dev/null +++ b/jsonb/src/test/java/io/avaje/jsonb/core/UUIDTest.java @@ -0,0 +1,51 @@ +package io.avaje.jsonb.core; + +import io.avaje.jsonb.JsonType; +import io.avaje.jsonb.Jsonb; +import io.avaje.jsonb.Types; +import org.junit.jupiter.api.Test; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + +class UUIDTest { + + Jsonb jsonb = Jsonb.newBuilder().build(); + + @Test + void toJson_fromJson() { + + UUID val = UUID.randomUUID(); + JsonType type = jsonb.type(UUID.class); + String asJson = type.toJson(val); + assertThat(asJson).isEqualTo("\"" + val + "\""); + + UUID fromJson = type.list().fromJson("[\"" + val + "\"]").get(0); + assertThat(fromJson).isEqualTo(val); + } + + @Test + void asMap_toFromJson() { + + UUID v0 = UUID.randomUUID(); + UUID v1 = UUID.randomUUID(); + Map map = new LinkedHashMap<>(); + map.put("k0", v0); + map.put("k1", v1); + + Jsonb jsonb = Jsonb.newBuilder().build(); + + JsonType> mapUidType = jsonb.type(Types.mapOf(UUID.class)); + String asJson = mapUidType.toJson(map); + + Map fromJson = mapUidType.fromJson(asJson); + + assertThat(fromJson).containsKeys("k0", "k1"); + assertThat(fromJson.get("k0")).isEqualTo(v0); + assertThat(fromJson.get("k1")).isEqualTo(v1); + } + +}