将 BigDecimal/BigInteger 序列化为 ProtocolBuffers 的最佳方法是什么
我开始将自定义序列化机制迁移到协议缓冲区。 BigDecimal 是一种特别经常使用的数据类型。
有谁知道在协议缓冲区中序列化这个的好方法? 我们当前的序列化例程使用 BigDecimal.toPlainString() 进行序列化,并使用 new BigDecimal(String) 进行反序列化 - 我假设有更好的方法。
我的猜测是将 BigDecimal 定义为:
message BDecimal {
required int32 scale = 1;
required BInteger int_val = 2;
}
但我不太确定如何定义 BigInteger - 也许使用其 toByteArray() 方法?
I am starting to migrate a custom serialization mechanism to Protocol Buffers. One data type that will be used particularly regularly is BigDecimal
.
Does anyone know of a good way of serializing this within Protocol Buffers? Our current serialization routine uses BigDecimal.toPlainString()
for serialization, and new BigDecimal(String)
for deserialization - I'm assuming there's a better way.
My guess is to define a BigDecimal
as:
message BDecimal {
required int32 scale = 1;
required BInteger int_val = 2;
}
But I am not too sure how to define BigInteger
- perhaps using its toByteArray()
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。 您应该将 BigInteger 定义为 BigInteger.toByteArray() 。
我的猜测是 BigDecimal 将是:
而 BigInteger 可能定义为
处理 BigInteger 的代码将是:
Yes. You should define BigInteger as BigInteger.toByteArray() .
My guess is that BigDecimal would be:
while BigInteger may be defined as
The code to handle BigInteger would be:
我最近有与OP相同的需求,并使用了与@notnoop提出的类似的解决方案。 看到 @stikkos 的评论后将其发布在这里:
根据
BigDecimal
类文档:因此,可以考虑三个属性来序列化 java.math.BigDecimal:
unscaledValue
scale
precision
现在,代码。
Protobuf v3 消息定义:
如何将
java.math.BigDecimal
序列化为 Protobuf 消息:如何将 Protobuf 消息反序列化回
java.math.BigDecimal
:我们可以检查获得的 java.math.BigDecimal 是否产生相同的原始值,如下所示:
注意:OP 假设有一种比 java.math.BigDecimal 更好的方法来序列化 java.math.BigDecimal使用普通的
字符串
。 在存储方面,这个解决方案更好。例如,要序列化 π 的前 100 位小数:
这会产生:
I have recently had the same need as OP, and used a similar solution to the one proposed by @notnoop. Posting it here after seeing this comment from @stikkos:
According to the
BigDecimal
class documentation:So, a
java.math.BigDecimal
can be serialized considering three properties:unscaledValue
scale
precision
Now, the code.
Protobuf v3 message definition:
How to serialize a
java.math.BigDecimal
to a Protobuf message:How to deserialize the Protobuf message back to a
java.math.BigDecimal
:We can check the obtained
java.math.BigDecimal
yields the same original value as follows:Note: OP assumed there is a better way to serialize a
java.math.BigDecimal
than using a plainString
. In terms of storage, this solution is better.For example, to serialize the first 100 decimals of π:
This yields:
为什么要改变它? 只是因为您可以或者确实有需要(例如分析会话确认,序列化/反序列化花费了大部分时间)。
我会使用一个字符串,只是因为它是内置的:)
建议的字节数组方法(将 BigDecimal/BigInteger 序列化到 ProtocolBuffers 的最佳方法是什么)对我来说似乎很好,如果字符串表示似乎是一个问题。
Why do you want to change it? Just because you can or is there a real need (like a profiling session confirming, that serialization/deserialization takes most of the time).
I would use a string, just because it is built in :)
The proposed byte array approach (What is the best approach for serializing BigDecimal/BigInteger to ProtocolBuffers) seems fine to me, if string representation seems to be an issue.