Skip to content

Commit

Permalink
fix(JOHNZON-397): sets the maxBigDecimalScale if it's our JsonProvide…
Browse files Browse the repository at this point in the history
…r impl

Signed-off-by: Jean-Louis Monteiro <jlmonteiro@tomitribe.com>
  • Loading branch information
jeanouii committed May 22, 2023
1 parent 8e2b281 commit 34ad9a6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -123,6 +124,7 @@ public Mapper build() {
provider = this.provider;
} else {
provider = JsonProvider.provider();
JsonProviderUtil.setMaxBigDecimalScale(provider, maxBigDecimalScale);
this.provider = provider;
}
final Map<String, Object> config = new HashMap<String, Object>();
Expand All @@ -141,7 +143,7 @@ public Mapper build() {
}

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");
}
Expand All @@ -158,6 +160,7 @@ public Mapper build() {
}
} else if (this.provider == null) {
this.provider = JsonProvider.provider();
JsonProviderUtil.setMaxBigDecimalScale(provider, maxBigDecimalScale);
}
if (builderFactory == null) {
builderFactory = provider.createBuilderFactory(emptyMap());
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

}

0 comments on commit 34ad9a6

Please sign in to comment.