Skip to content

Commit

Permalink
fix for BigInteger and UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Nov 26, 2021
1 parent d99775e commit bec4194
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
15 changes: 8 additions & 7 deletions jsonb/src/main/java/io/avaje/jsonb/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.Closeable;
import java.io.Flushable;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
* Writes json content.
Expand Down Expand Up @@ -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();

}
5 changes: 2 additions & 3 deletions jsonb/src/main/java/io/avaje/jsonb/core/MathAdapters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions jsonb/src/main/java/io/avaje/jsonb/jackson/JacksonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 6 additions & 5 deletions jsonb/src/main/java/io/avaje/jsonb/spi/DelegateJsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.avaje.jsonb.JsonWriter;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
* Provides a delegating JsonWriter.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions jsonb/src/test/java/io/avaje/jsonb/core/UUIDTest.java
Original file line number Diff line number Diff line change
@@ -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<UUID> 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<String, UUID> map = new LinkedHashMap<>();
map.put("k0", v0);
map.put("k1", v1);

Jsonb jsonb = Jsonb.newBuilder().build();

JsonType<Map<String,UUID>> mapUidType = jsonb.type(Types.mapOf(UUID.class));
String asJson = mapUidType.toJson(map);

Map<String, UUID> fromJson = mapUidType.fromJson(asJson);

assertThat(fromJson).containsKeys("k0", "k1");
assertThat(fromJson.get("k0")).isEqualTo(v0);
assertThat(fromJson.get("k1")).isEqualTo(v1);
}

}

0 comments on commit bec4194

Please sign in to comment.