Java BigDecimal bigDecimalProperty(ObjectNode node, String propertyName, BigDecimal propertyValue)

Here you can find the source of bigDecimalProperty(ObjectNode node, String propertyName, BigDecimal propertyValue)

Description

Sets the value of a property for a given json node.

License

Apache License

Parameter

Parameter Description
node a parameter
propertyName a parameter
propertyValue a parameter

Declaration

public static void bigDecimalProperty(ObjectNode node, String propertyName, BigDecimal propertyValue) 

Method Source Code

//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;
    }
}

Related

  1. average(final BigDecimal... numbers)
  2. bigDecimalListToArray(List list)
  3. bigDecimalObjectValue(Object input)
  4. bigDecimalParse(String value)
  5. bigDecimalPrint(BigDecimal value)
  6. byteOverflow(BigDecimal value)
  7. cloneBigDecimal(BigDecimal bdOriginal)
  8. createJsonNumber(BigDecimal d)
  9. createReconFileHeader(int totalCount, BigDecimal totalAmount)