smile_elims

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Gson gson = new GsonBuilder().registerTypeAdapter(BigDecimal.class, new BigDecimalAdapter()).serializeNulls().create();

我这块是将bigdecimal转字符串,如果有其他需求比方说监听日期或数字,可对应编写类-extends TypeAdapter<您要做操作的类型>

public class BigDecimalAdapter extends TypeAdapter<BigDecimal> {

/**
* 序列化
*
* @param out 输出流
* @param value 值
* @throws IOException
*/
@Override
public void write(JsonWriter out, BigDecimal value) throws IOException {
if (value != null) {
out.value(""+value);
} else {
out.value("");
}
}

/**
* 反序列化
*
* @param in 输入流
* @return
* @throws IOException
*/
@Override
public BigDecimal read(JsonReader in) throws IOException {
String str = in.nextString();
BigDecimal date = null;
date = new BigDecimal(str);
return date;
}

 

posted on 2020-11-20 14:59  smile_elims  阅读(796)  评论(0编辑  收藏  举报