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

org.macrocloud.kernel.toolkit.jackson.BaseNumberModule Maven / Gradle / Ivy

There is a newer version: 1.1.0-RELEASE
Show newest version
package org.macrocloud.kernel.toolkit.jackson;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import java.math.BigDecimal;
import java.math.BigInteger;


/**
 * 大整数序列化为 String 字符串,避免浏览器丢失精度
 *
 * 

* 前端建议采用: * bignumber 库: https://github.com/MikeMcl/bignumber.js * decimal.js 库: https://github.com/MikeMcl/decimal.js *

* * @author macro */ public class BaseNumberModule extends SimpleModule { /** The Constant INSTANCE. */ public static final BaseNumberModule INSTANCE = new BaseNumberModule(); /** * Instantiates a new base number module. */ public BaseNumberModule() { super(BaseNumberModule.class.getName()); // Long 和 BigInteger 采用定制的逻辑序列化,避免超过js的精度 this.addSerializer(Long.class, BigNumberSerializer.instance); this.addSerializer(Long.TYPE, BigNumberSerializer.instance); this.addSerializer(BigInteger.class, BigNumberSerializer.instance); // BigDecimal 采用 toString 避免精度丢失,前端采用 decimal.js 来计算。 this.addSerializer(BigDecimal.class, ToStringSerializer.instance); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy