Here you can find the source of bigDecimalProperty(ObjectNode node, String propertyName, BigDecimal propertyValue)
Parameter | Description |
---|---|
node | a parameter |
propertyName | a parameter |
propertyValue | a parameter |
public static void bigDecimalProperty(ObjectNode node, String propertyName, BigDecimal propertyValue)
//package com.java2s; /**//from w w w . jav a2s. co m * Copyright 2017 Red Hat, Inc, and individual contributors. * * Licensed 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. */ import java.math.BigDecimal; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; public class Main { private static final JsonNodeFactory factory = JsonNodeFactory.instance; /** * Extract a BigDecimal property from the given json tree. Returns null if no * property exists or is not a boolean node. * @param node * @param propertyName */ public static BigDecimal bigDecimalProperty(JsonNode node, String propertyName) { JsonNode propertyNode = node.get(propertyName); if (propertyNode != null) { return new BigDecimal(propertyNode.asText()); } return null; } /** * Sets the value of a property for a given json node. If the value is null, * then the property is not written. * @param node * @param propertyName * @param propertyValue */ public static void bigDecimalProperty(ObjectNode node, String propertyName, BigDecimal propertyValue) { if (propertyValue == null) { return; } if (isIntegerValue(propertyValue)) { node.set(propertyName, factory.numberNode(propertyValue.toBigInteger())); } else { node.set(propertyName, factory.numberNode(propertyValue)); } } private static boolean isIntegerValue(BigDecimal bd) { return bd.signum() == 0 || bd.scale() <= 0 || bd.stripTrailingZeros().scale() <= 0; } }