From 98b61ba886723624e99612e9af3280a2fb387510 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 4 Aug 2023 10:05:50 +0100 Subject: [PATCH 1/2] add number tests --- .../datatype/jsonorg/SimpleReadTest.java | 31 ++++++++++++++++++- .../jsr353/JsonValueDeserializationTest.java | 29 +++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/json-org/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java b/json-org/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java index 012f60c..9977015 100644 --- a/json-org/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java +++ b/json-org/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java @@ -4,7 +4,7 @@ import org.json.*; -import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule; +import java.math.BigDecimal; public class SimpleReadTest extends ModuleTestBase { @@ -46,4 +46,33 @@ public void testReadArray() throws Exception JSONArray array2 = array.getJSONArray(6); assertEquals(0, array2.length()); } + + public void testDouble() throws Exception + { + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JsonOrgModule()); + + JSONObject val = mapper.readValue("{\"val\":0.5}", JSONObject.class); + assertEquals(0.5d, val.getDouble("val")); + assertEquals(new BigDecimal(0.5d), val.get("val")); + } + + public void testBigInteger() throws Exception + { + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JsonOrgModule()); + + JSONObject val = mapper.readValue("{\"val\":2e308}", JSONObject.class); + assertEquals(new BigDecimal("2e308").toBigInteger(), val.getBigInteger("val")); + } + + public void testBigIntegerArray() throws Exception + { + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JsonOrgModule()); + + JSONArray array = mapper.readValue("[2e308]", JSONArray.class); + assertEquals(1, array.length()); + assertEquals(new BigDecimal("2e308").toBigInteger(), array.getBigInteger(0)); + } } diff --git a/jsr-353/src/test/java/com/fasterxml/jackson/datatype/jsr353/JsonValueDeserializationTest.java b/jsr-353/src/test/java/com/fasterxml/jackson/datatype/jsr353/JsonValueDeserializationTest.java index 3d7d250..db00856 100644 --- a/jsr-353/src/test/java/com/fasterxml/jackson/datatype/jsr353/JsonValueDeserializationTest.java +++ b/jsr-353/src/test/java/com/fasterxml/jackson/datatype/jsr353/JsonValueDeserializationTest.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import java.beans.ConstructorProperties; +import java.math.BigDecimal; public class JsonValueDeserializationTest extends TestBase { @@ -135,4 +136,32 @@ public void testConstructorProperties() throws Exception assertNull(ob2.obj1); assertSame(JsonValue.NULL, ob2.obj2); } + + public void testBigInteger() throws Exception + { + final String JSON = "[2e308]"; + JsonValue v = MAPPER.readValue(JSON, JsonValue.class); + assertTrue(v instanceof JsonArray); + JsonArray a = (JsonArray) v; + assertEquals(1, a.size()); + assertTrue(a.get(0) instanceof JsonNumber); + assertEquals(new BigDecimal("2e308").toBigInteger(), ((JsonNumber) a.get(0)).bigIntegerValue()); + + + // also, should work with explicit type + JsonArray array = MAPPER.readValue(JSON, JsonArray.class); + assertEquals(1, array.size()); + + // and round-tripping ought to be ok: + assertEquals("[2E+308]", serializeAsString(v)); + } + + public void testDouble() throws Exception + { + JsonValue val = MAPPER.readValue("{\"val\":0.5}", JsonValue.class); + JsonObject jsonObject = val.asJsonObject(); + JsonNumber jsonNumber = jsonObject.getJsonNumber("val"); + assertEquals(0.5d, jsonNumber.doubleValue()); + assertEquals(new BigDecimal(0.5d), jsonNumber.numberValue()); + } } From f7eeba682a3b8dcc8b2346cf14f300bb379b5364 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 26 Sep 2023 10:01:09 +0100 Subject: [PATCH 2/2] get tests working with 2.14 code --- .../datatype/jsonorg/SimpleReadTest.java | 22 ------------------- .../jsr353/JsonValueDeserializationTest.java | 19 ---------------- 2 files changed, 41 deletions(-) diff --git a/json-org/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java b/json-org/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java index 9977015..621e6b5 100644 --- a/json-org/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java +++ b/json-org/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java @@ -4,8 +4,6 @@ import org.json.*; -import java.math.BigDecimal; - public class SimpleReadTest extends ModuleTestBase { public void testReadObject() throws Exception @@ -54,25 +52,5 @@ public void testDouble() throws Exception JSONObject val = mapper.readValue("{\"val\":0.5}", JSONObject.class); assertEquals(0.5d, val.getDouble("val")); - assertEquals(new BigDecimal(0.5d), val.get("val")); - } - - public void testBigInteger() throws Exception - { - ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(new JsonOrgModule()); - - JSONObject val = mapper.readValue("{\"val\":2e308}", JSONObject.class); - assertEquals(new BigDecimal("2e308").toBigInteger(), val.getBigInteger("val")); - } - - public void testBigIntegerArray() throws Exception - { - ObjectMapper mapper = new ObjectMapper(); - mapper.registerModule(new JsonOrgModule()); - - JSONArray array = mapper.readValue("[2e308]", JSONArray.class); - assertEquals(1, array.length()); - assertEquals(new BigDecimal("2e308").toBigInteger(), array.getBigInteger(0)); } } diff --git a/jsr-353/src/test/java/com/fasterxml/jackson/datatype/jsr353/JsonValueDeserializationTest.java b/jsr-353/src/test/java/com/fasterxml/jackson/datatype/jsr353/JsonValueDeserializationTest.java index db00856..3f4d605 100644 --- a/jsr-353/src/test/java/com/fasterxml/jackson/datatype/jsr353/JsonValueDeserializationTest.java +++ b/jsr-353/src/test/java/com/fasterxml/jackson/datatype/jsr353/JsonValueDeserializationTest.java @@ -137,25 +137,6 @@ public void testConstructorProperties() throws Exception assertSame(JsonValue.NULL, ob2.obj2); } - public void testBigInteger() throws Exception - { - final String JSON = "[2e308]"; - JsonValue v = MAPPER.readValue(JSON, JsonValue.class); - assertTrue(v instanceof JsonArray); - JsonArray a = (JsonArray) v; - assertEquals(1, a.size()); - assertTrue(a.get(0) instanceof JsonNumber); - assertEquals(new BigDecimal("2e308").toBigInteger(), ((JsonNumber) a.get(0)).bigIntegerValue()); - - - // also, should work with explicit type - JsonArray array = MAPPER.readValue(JSON, JsonArray.class); - assertEquals(1, array.size()); - - // and round-tripping ought to be ok: - assertEquals("[2E+308]", serializeAsString(v)); - } - public void testDouble() throws Exception { JsonValue val = MAPPER.readValue("{\"val\":0.5}", JsonValue.class);