You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2019/08/10 16:17:07 UTC

[johnzon] branch master updated: JOHNZON-241 float can't be serialized as double

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 1f34d56  JOHNZON-241 float can't be serialized as double
1f34d56 is described below

commit 1f34d56f76af29cf99fd74e6d9aec5ee6eecf37f
Author: Romain Manni-Bucau <rm...@apache.org>
AuthorDate: Sat Aug 10 18:17:01 2019 +0200

    JOHNZON-241 float can't be serialized as double
---
 .../java/org/apache/johnzon/jsonb/FloatTest.java   | 51 ++++++++++++++++++++++
 .../johnzon/mapper/MappingGeneratorImpl.java       | 24 +++++++---
 2 files changed, 69 insertions(+), 6 deletions(-)

diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/FloatTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/FloatTest.java
new file mode 100644
index 0000000..6b2c94f
--- /dev/null
+++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/FloatTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.jsonb;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.johnzon.jsonb.model.Holder;
+import org.apache.johnzon.jsonb.test.JsonbRule;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class FloatTest {
+    @Rule
+    public final JsonbRule jsonb = new JsonbRule();
+
+    @Test
+    public void floatMin() {
+        final String json = jsonb.toJson(new FloatHolder() {{ setInstance(Float.MIN_VALUE); }});
+        assertEquals("{\"instance\":" + Float.toString(Float.MIN_VALUE) + "}", json);
+    }
+
+    public static class FloatHolder implements Holder<Float> {
+        private Float instance;
+
+        @Override
+        public Float getInstance() {
+            return instance;
+        }
+
+        @Override
+        public void setInstance(final Float instance) {
+            this.instance = instance;
+        }
+    }
+}
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
index e80629a..1d6119d 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
@@ -219,9 +219,15 @@ public class MappingGeneratorImpl implements MappingGenerator {
             generator.write(Number.class.cast(value).intValue());
             handled = true;
         } else if (isFloat(type)) {
-            final double doubleValue = Number.class.cast(value).doubleValue();
-            if (!Double.isNaN(doubleValue)) {
-                generator.write(doubleValue);
+            if (type == Float.class || type == float.class) {
+                if (!Float.isNaN(Float.class.cast(value))) {
+                    generator.write(new BigDecimal(value.toString()));
+                }
+            } else {
+                final double doubleValue = Number.class.cast(value).doubleValue();
+                if (!Double.isNaN(doubleValue)) {
+                    generator.write(doubleValue);
+                }
             }
             handled = true;
         } else if (type == boolean.class || type == Boolean.class) {
@@ -256,9 +262,15 @@ public class MappingGeneratorImpl implements MappingGenerator {
             generator.write(key, Number.class.cast(value).intValue());
             handled = true;
         } else if (isFloat(type)) {
-            final double doubleValue = Number.class.cast(value).doubleValue();
-            if (!Double.isNaN(doubleValue)) {
-                generator.write(key, doubleValue);
+            if (type == Float.class || type == float.class) {
+                if (!Float.isNaN(Float.class.cast(value))) {
+                    generator.write(key, new BigDecimal(value.toString()));
+                }
+            } else {
+                final double doubleValue = Number.class.cast(value).doubleValue();
+                if (!Double.isNaN(doubleValue)) {
+                    generator.write(key, doubleValue);
+                }
             }
             handled = true;
         } else if (type == boolean.class || type == Boolean.class) {