Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[QUESTION] BigDecimal和Decimal128转换问题 #427

Closed
lmh2017 opened this issue Jun 7, 2022 · 6 comments
Closed

[QUESTION] BigDecimal和Decimal128转换问题 #427

lmh2017 opened this issue Jun 7, 2022 · 6 comments
Labels
question Further information is requested
Milestone

Comments

@lmh2017
Copy link

lmh2017 commented Jun 7, 2022

请描述您的问题

询问有关本项目的使用和其他方面的相关问题。
JSON字符串如下:

{
	"salaryStart": {
		"$numberDecimal": "6000"
	},
	"salaryEnd": {
		"$numberDecimal": "9000"
	}
}

由于MongoDB不支持BigDecimal类型, 需转换成Decimal128存储。

spring 定义转换器

@ReadingConverter
@WritingConverter
public class BigDecimalToDecimal128Converter implements Converter<Decimal128, BigDecimal> {
    @Override
    public BigDecimal convert(Decimal128 decimal128) {
        return decimal128.bigDecimalValue();
    }
}

对象字段

public class PositionDO{
    //综合薪资范围起
    private BigDecimal salaryStart;
    //综合薪资范围止
    private BigDecimal salaryEnd;
}

所以支持mongodb类型和java类型互转。

现在用原对象接收,抛异常 Exception in thread "Thread-8" com.alibaba.fastjson2.JSONException: TODO : 6

__问:如何用BigDecimal 接收这样的类型字段,怎样将converter使用进去?_

@lmh2017 lmh2017 added the question Further information is requested label Jun 7, 2022
@lmh2017
Copy link
Author

lmh2017 commented Jun 7, 2022

异常是com.alibaba.fastjson2.JSONReader#getBigDecimal 方法抛出来的,因为识别不了此类型,抛异常了
image

@wenshao wenshao added this to the 2.0.7 milestone Jun 7, 2022
@wenshao
Copy link
Member

wenshao commented Jun 7, 2022

Decimal128
这个类是org.bson.types.Decimal128 么?

@wenshao
Copy link
Member

wenshao commented Jun 7, 2022

已经内置支持,请用2.0.7-SNAPSHOT版本验证下
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.7-SNAPSHOT/

@lmh2017
Copy link
Author

lmh2017 commented Jun 8, 2022

是的

@lmh2017
Copy link
Author

lmh2017 commented Jun 8, 2022

已经内置支持,请用2.0.7-SNAPSHOT版本验证下 https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.7-SNAPSHOT/

好的 谢谢 我验证下

@wenshao
Copy link
Member

wenshao commented Jun 11, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.7
问题已经修复,请用新版本

@wenshao wenshao closed this as completed Jun 11, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants