fastjson2 [BUG]Long BigDecimal 自定义序列化失效

zsbz8rwp  于 2022-10-25  发布在  其他
关注(0)|答案(8)|浏览(764)

问题描述

  • 简要描述您碰到的问题。*

环境信息

  • 请填写以下信息:*

  • OS信息: win10

  • JDK信息: jdk11

  • 版本信息:2.0.15

package cn.nextop.gadget.json.fastjson;

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.writer.ObjectWriter;

/**

* @author Baoyi Chen
* /

public class Main {
	public static void main(String[] args) {
		JSON.register(Long.class, new ObjectWriter<Long>() {
			@Override
			public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
				Long value = (Long) object;
				if (value == null) jsonWriter.writeNull(); else jsonWriter.writeString(Long.toString(value));
			}
		});

		JSON.register(BigDecimal.class, new ObjectWriter<BigDecimal>() {
			@Override
			public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
				BigDecimal value = (BigDecimal) object;
				if (value == null) jsonWriter.writeNull(); else jsonWriter.writeString(value.toPlainString());
			}
		});

		Map<String, Long> map = new HashMap<>();
		map.put("0", 0L);
		String s = JSON.toJSONString(map);
		System.out.println(s);

		Test t = new Test();
		t.setValue(0L);
		t.setValue1(new BigDecimal("0.11"));
		s = JSON.toJSONString(t);
		System.out.println(s);
	}

	public static class Test {
		private Long value;
		private BigDecimal value1;

		public Long getValue() {
			return value;
		}

		public void setValue(Long value) {
			this.value = value;
		}

		public BigDecimal getValue1() {
			return value1;
		}

		public void setValue1(BigDecimal value1) {
			this.value1 = value1;
		}
	}
}

期待输出

{"0":"0"}
{"value":"0","value1":"0.11"}

实际输出

{"0":0}
{"value":0,"value1":0.11}

搞不懂好多版本为什么有的类型写自定义序列化器好用,有的类型不好用,自定义任何类型的序列化器应该完全覆盖默认行为。

oymdgrw7

oymdgrw71#

Date/BigDecimal这些类型,自定义序列化和反序列化都容易导致性能变差,内置的性能优化实现,有些地方没考虑自定义序列化和反序列的情况。

遇到问题反馈过来,会逐步修复。

11dmarpk

11dmarpk2#

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.16-SNAPSHOT/
问题已经修复,请用2.0.16-SNAPSHOT帮忙验证,2.0.16版本预计在10月30日前发布

icnyk63a

icnyk63a3#

JSON.toJSONString(obj, JSONWriter.Feature.BrowserCompatible, Feature.WriteBigDecimalAsPlain)

这两个内置的Feature可能就是要的,内置的性能会更好

tcomlyy6

tcomlyy64#

@wenshao
2.0.16-SNAPSHOT map里的BigDecimal还是序列化成非字符串了

package cn.nextop.gadget.json;

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.writer.ObjectWriter;

/**

* @author Baoyi Chen
* /

public class Main {
	public static void main(String[] args) {
		JSON.register(Long.class, new ObjectWriter<Long>() {
			@Override
			public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
				Long value = (Long) object;
				if (value == null) jsonWriter.writeNull(); else jsonWriter.writeString(Long.toString(value));
			}
		});

		JSON.register(BigDecimal.class, new ObjectWriter<BigDecimal>() {
			@Override
			public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
				BigDecimal value = (BigDecimal) object;
				if (value == null) jsonWriter.writeNull(); else jsonWriter.writeString(value.toPlainString());
			}
		});

		Map<String, BigDecimal> map = new HashMap<>();
		map.put("0", new BigDecimal("0.11"));
		String s = JSON.toJSONString(map);
		System.out.println(s);

		Test t = new Test();
		t.setValue(0L);
		t.setValue1(new BigDecimal("0.11"));
		s = JSON.toJSONString(t);
		System.out.println(s);
	}

	public static class Test {
		private Long value;
		private BigDecimal value1;

		public Long getValue() {
			return value;
		}

		public void setValue(Long value) {
			this.value = value;
		}

		public BigDecimal getValue1() {
			return value1;
		}

		public void setValue1(BigDecimal value1) {
			this.value1 = value1;
		}
	}
}

期待结果

{"0":"0.11"}
{"value":"0","value1":"0.11"}

实际

{"0":0.11}
{"value":"0","value1":"0.11"}
qmb5sa22

qmb5sa225#

@wenshao
包括在List中的BigDecimal和Long类型,都没有按照自定义的序列化方式进行序列化

wgx48brx

wgx48brx6#

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.16-SNAPSHOT/
问题已经修复,请用2.0.16-SNAPSHOT帮忙验证,2.0.16版本预计在10月23日前发布

uemypmqf

uemypmqf8#

@wenshao 还是有问题

public class Main {
	public static void main(String[] args) {
		JSON.register(Long.class, new ObjectWriter<Long>() {
			@Override
			public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
				Long value = (Long) object;
				if (value == null) jsonWriter.writeNull(); else jsonWriter.writeString(Long.toString(value));
			}
		});

		JSON.register(BigDecimal.class, new ObjectWriter<BigDecimal>() {
			@Override
			public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
				BigDecimal value = (BigDecimal) object;
				if (value == null) jsonWriter.writeNull(); else jsonWriter.writeString(value.toPlainString());
			}
		});

		Map<String, Long> map = new HashMap<>();
		map.put("0", 0L);
		String s = JSON.toJSONString(map);
		System.out.println(s);
	}
}

相关问题