[johnzon] branch master updated: [JOHNZON-318] ensure List and List works even when not integers

Wed, 15 Jul 2020 10:05:56 -0700

This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git


The following commit(s) were added to refs/heads/master by this push:
     new 7612bc2  [JOHNZON-318] ensure List<Object> and List<JsonValue> works 
even when not integers
7612bc2 is described below

commit 7612bc2686e9bb980846a4bf5741bb24df47b650
Author: Romain Manni-Bucau <rmannibu...@gmail.com>
AuthorDate: Wed Jul 15 19:05:43 2020 +0200

    [JOHNZON-318] ensure List<Object> and List<JsonValue> works even when not 
integers
---
 .../org/apache/johnzon/jsonb/JohnzonJsonbTest.java | 24 ++++++++++++++++++++
 .../apache/johnzon/mapper/MappingParserImpl.java   | 26 ++++++++++------------
 2 files changed, 36 insertions(+), 14 deletions(-)

diff --git 
a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonJsonbTest.java 
b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonJsonbTest.java
index 757359b..759a16a 100644
--- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonJsonbTest.java
+++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonJsonbTest.java
@@ -20,7 +20,9 @@ package org.apache.johnzon.jsonb;
 
 import static org.junit.Assert.assertEquals;
 
+import javax.json.Json;
 import javax.json.JsonArray;
+import javax.json.JsonValue;
 import javax.json.bind.Jsonb;
 import javax.json.bind.JsonbBuilder;
 
@@ -28,11 +30,25 @@ import org.apache.johnzon.jsonb.test.JsonbRule;
 import org.junit.Rule;
 import org.junit.Test;
 
+import java.util.List;
+
 public class JohnzonJsonbTest {
     @Rule
     public final JsonbRule rule = new JsonbRule();
 
     @Test
+    public void listJsonValue() {
+        assertEquals(Json.createValue(1.1),
+                rule.fromJson("{\"value\":[1.1]}", 
ArrayJsonValueWrapper.class).value.get(0));
+    }
+
+    @Test
+    public void listObject() {
+        assertEquals(1.1, Number.class.cast(
+                rule.fromJson("{\"value\":[1.1]}", 
ArrayObjectWrapper.class).value.get(0)).doubleValue(), 0);
+    }
+
+    @Test
     public void jsonArray() throws Exception {
         try (final Jsonb jsonb = JsonbBuilder.create()) {
             final String json = "[{\"foo\":\"bar\"}]";
@@ -63,4 +79,12 @@ public class JohnzonJsonbTest {
             this.value = value;
         }
     }
+
+    public static class ArrayObjectWrapper {
+        public List<Object> value;
+    }
+
+    public static class ArrayJsonValueWrapper {
+        public List<JsonValue> value;
+    }
 }
diff --git 
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
index 25a5134..7acdd67 100644
--- 
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
+++ 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
@@ -478,16 +478,13 @@ public class MappingParserImpl implements MappingParser {
             final long longValue = jsonNumber.longValue();
             if (intValue == longValue) {
                 return intValue;
-            } else {
-                return longValue;
-            }
-        } else {
-            if (config.isUseBigDecimalForFloats()) {
-                return jsonNumber.bigDecimalValue();
-            } else {
-                return jsonNumber.doubleValue();
             }
+            return longValue;
         }
+        if (config.isUseBigDecimalForFloats()) {
+            return jsonNumber.bigDecimalValue();
+        }
+        return jsonNumber.doubleValue();
     }
 
     private Object convertTo(final Adapter converter, final JsonValue 
jsonValue, final JsonPointerTracker jsonPointer,
@@ -644,7 +641,7 @@ public class MappingParserImpl implements MappingParser {
         }
 
         if (JsonObject.class.isInstance(jsonValue)) {
-            if (JsonObject.class == type || JsonStructure.class == type) {
+            if (JsonObject.class == type || JsonStructure.class == type || 
JsonValue.class == type) {
                 return jsonValue;
             }
             final boolean typedAdapter = 
!ConverterAdapter.class.isInstance(itemConverter) && 
TypeAwareAdapter.class.isInstance(itemConverter);
@@ -655,12 +652,12 @@ public class MappingParserImpl implements MappingParser {
                     jsonPointer);
             return typedAdapter ? itemConverter.to(object) : object;
         } else if (JsonArray.class.isInstance(jsonValue)) {
-            if (JsonArray.class == type || JsonStructure.class == type) {
+            if (JsonArray.class == type || JsonStructure.class == type || 
JsonValue.class == type) {
                 return jsonValue;
             }
             return buildArray(type, JsonArray.class.cast(jsonValue), 
itemConverter, null, jsonPointer, rootType);
         } else if (JsonNumber.class.isInstance(jsonValue)) {
-            if (JsonNumber.class == type) {
+            if (JsonNumber.class == type || JsonValue.class == type) {
                 return jsonValue;
             }
 
@@ -686,12 +683,12 @@ public class MappingParserImpl implements MappingParser {
                 return number.bigDecimalValue();
             }
 
-            int intValue = number.intValueExact();
             if (type == Integer.class || type == int.class) {
-                return intValue;
+                return number.intValueExact();
             }
 
             if (type == Short.class || type == short.class) {
+                final int intValue = number.intValue();
                 short shortVal = (short) intValue;
                 if (intValue != shortVal) {
                     throw new java.lang.ArithmeticException("Overflow");
@@ -700,12 +697,13 @@ public class MappingParserImpl implements MappingParser {
             }
 
             if (type == Byte.class || type == byte.class) {
+                final int intValue = number.intValueExact();
                 Validator.validateByte(intValue);
                 return (byte) intValue;
             }
 
         } else if (JsonString.class.isInstance(jsonValue)) {
-            if (JsonString.class == type) {
+            if (JsonString.class == type || JsonValue.class == type) {
                 return jsonValue;
             }
 

Reply via email to