All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.fastjson2.reader.ObjectReaderImplBigDecimal Maven / Gradle / Ivy

Go to download

Fastjson is a JSON processor (JSON parser + JSON generator) written in Java

There is a newer version: 2.0.53.android8
Show newest version
package com.alibaba.fastjson2.reader;

import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.function.Function;
import com.alibaba.fastjson2.function.impl.ToAny;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Map;

final class ObjectReaderImplBigDecimal
        extends ObjectReaderPrimitive {
    private Function converter = new ToAny(BigDecimal.class);
    static final ObjectReaderImplBigDecimal INSTANCE = new ObjectReaderImplBigDecimal(null);

    final Function function;

    public ObjectReaderImplBigDecimal(Function function) {
        super(BigDecimal.class);
        this.function = function;
    }

    @Override
    public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        BigDecimal decimal = jsonReader.readBigDecimal();
        if (function != null) {
            return function.apply(decimal);
        }
        return decimal;
    }

    @Override
    public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        BigDecimal decimal = jsonReader.readBigDecimal();
        if (function != null) {
            return function.apply(decimal);
        }
        return decimal;
    }

    @Override
    public Object createInstance(Map map, long features) {
        Object value = map.get("value");
        if (value == null) {
            value = map.get("$numberDecimal");
        }

        if (!(value instanceof BigDecimal)) {
            value = converter.apply(value);
        }

        BigDecimal decimal = (BigDecimal) value;
        if (function != null) {
            return function.apply(decimal);
        }

        return decimal;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy