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

jlmonteiro pushed a commit to branch johnzon-1.2.x
in repository https://gitbox.apache.org/repos/asf/johnzon.git

commit 34ad9a6b296ae7b4667c3cf0037998e451499ea4
Author: Jean-Louis Monteiro <jlmonte...@tomitribe.com>
AuthorDate: Tue May 16 10:43:53 2023 +0200

    fix(JOHNZON-397): sets the maxBigDecimalScale if it's our JsonProvider impl
    
    Signed-off-by: Jean-Louis Monteiro <jlmonte...@tomitribe.com>
---
 .../org/apache/johnzon/mapper/MapperBuilder.java   |  5 +-
 .../johnzon/mapper/util/JsonProviderUtil.java      | 67 ++++++++++++++++++++++
 2 files changed, 71 insertions(+), 1 deletion(-)

diff --git 
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java
index 013742cb..d58a4409 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java
@@ -23,6 +23,7 @@ import static java.util.Collections.emptyMap;
 import static java.util.Locale.ROOT;
 
 // import org.apache.johnzon.core.JsonParserFactoryImpl; // don't depend on 
core in mapper
+import org.apache.johnzon.mapper.util.JsonProviderUtil;
 import org.apache.johnzon.mapper.access.AccessMode;
 import org.apache.johnzon.mapper.access.BaseAccessMode;
 import org.apache.johnzon.mapper.access.FieldAccessMode;
@@ -123,6 +124,7 @@ public class MapperBuilder {
                 provider = this.provider;
             } else {
                 provider = JsonProvider.provider();
+                JsonProviderUtil.setMaxBigDecimalScale(provider, 
maxBigDecimalScale);
                 this.provider = provider;
             }
             final Map<String, Object> config = new HashMap<String, Object>();
@@ -141,7 +143,7 @@ public class MapperBuilder {
             }
 
             if (readerFactory == null) {
-                config.remove(JsonGenerator.PRETTY_PRINTING); // doesnt mean 
anything anymore for reader
+                config.remove(JsonGenerator.PRETTY_PRINTING); // doesn't mean 
anything anymore for reader
                 if (supportsComments) {
                     config.put("org.apache.johnzon.supports-comments", "true");
                 }
@@ -158,6 +160,7 @@ public class MapperBuilder {
             }
         } else if (this.provider == null) {
             this.provider = JsonProvider.provider();
+            JsonProviderUtil.setMaxBigDecimalScale(provider, 
maxBigDecimalScale);
         }
         if (builderFactory == null) {
             builderFactory = provider.createBuilderFactory(emptyMap());
diff --git 
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/util/JsonProviderUtil.java
 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/util/JsonProviderUtil.java
new file mode 100644
index 00000000..45f05712
--- /dev/null
+++ 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/util/JsonProviderUtil.java
@@ -0,0 +1,67 @@
+/*
+ * 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.mapper.util;
+
+import jakarta.json.spi.JsonProvider;
+import org.apache.johnzon.core.JsonProviderImpl;
+
+import java.lang.reflect.Method;
+
+/**
+ * ClassLoader related utils to avoid direct access to our JSON provider from 
the mapper
+ */
+public final class JsonProviderUtil {
+
+    private final static Method SET_MAX_BIG_DECIMAL_SCALE;
+
+    static {
+        try {
+            SET_MAX_BIG_DECIMAL_SCALE =
+                
JsonProviderImpl.class.getDeclaredMethod("setMaxBigDecimalScale", Integer.TYPE);
+            SET_MAX_BIG_DECIMAL_SCALE.setAccessible(true);
+        } catch (final NoSuchMethodException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private JsonProviderUtil() {
+        // private utility class ct
+    }
+
+    /**
+     * Sets the max big decimal scale property on the given provider instance.
+     * <p>
+     * This method is intentionally not receiving the property name, so we 
know exactly what will be passed in and what
+     * the method is supposed to set on the provider.
+     * <p>
+     * If the provider is not an instance of our JohnzonProviderImpl 
(org.apache.johnzon.core.JsonProviderImpl), the
+     * method is a noop.
+     *
+     * @param provider the provider to configure. Must be an instance of 
org.apache.johnzon.core.JsonProviderImpl
+     * @param value the max big decimal scale to set on the provider
+     */
+    public static void setMaxBigDecimalScale(final JsonProvider provider, 
final int value) {
+        if 
(!"org.apache.johnzon.core.JsonProviderImpl".equals(provider.getClass().getName()))
 return;
+
+        try {
+            SET_MAX_BIG_DECIMAL_SCALE.invoke(provider, value);
+        } catch (final Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+}
\ No newline at end of file

Reply via email to