com.alibaba.fastjson2.reader.ObjectReaderImplBigDecimal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastjson2 Show documentation
Show all versions of fastjson2 Show documentation
Fastjson is a JSON processor (JSON parser + JSON generator) written in Java
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;
}
}