增强序列化和双打

发布于 2024-11-11 13:25:53 字数 998 浏览 2 评论 0原文

我正在尝试使用 boost 序列化库将类序列化为字符串,并且我的类中包含几个双成员变量。

下面是我用来序列化的代码:

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>

std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << mPoint;

这是我的 Point 类中的序列化方法:

friend class boost::serialization::access;

template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
    if (version > 0)
    {
        ar & mLatitude;
        ar & mLongitude;
    }
}

当我序列化为字符串时,boost 似乎没有按照我的预期处理双精度到字符串转换(似乎有舍入)问题)。经过一番研究,其他人似乎也报告了同样的行为。我还了解与将双精度数转换为字符串相关的精度相关问题,反之亦然,以及这如何导致该问题。

奇怪的是,我不明白的是,当我使用 stringstream 本身并将 double 重定向到流时,或者当我使用 boost 的 lexical_cast 函数从 stringstream.str() 转换回来时,这似乎不会发生到一个双倍。在发现 boost 有自己的序列化/反序列化类之前,我实际上已经使用 stringstream 和 lexical_cast 调用编写了自己的序列化/反序列化类,并且它的工作没有问题。我真的希望我不必放弃序列化库并回到以前的状态。希望只有一些设置/特征/等等。我失踪了。

I am trying to serialize a class to a string using the boost serialization library and included in my class are several double member variables.

Below is the code I'm using to serialize:

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>

std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << mPoint;

Here is the serialiation method within my Point class:

friend class boost::serialization::access;

template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
    if (version > 0)
    {
        ar & mLatitude;
        ar & mLongitude;
    }
}

When I serialize to a string, boost doesn't appear to handle the double to string conversion as I would expect (there appear to be rounding issues). Researching a bit it looks like others have reported the same behavior. I also understand the precision related issues associated with converting a double to a string and vice versa and how this could cause the issue.

What is strange and I don't understand though is this doesn't appear to happen when I'm using a stringstream itself and redirecting the double to the stream nor when I use boost's lexical_cast function to convert from a stringstream.str() back to a double. Before discovering boost had its own serialization/deserialization classes, I had actually written my own using stringstream and lexical_cast calls and it worked w/o issue. I'm really hoping I don't have to abandon the serialization library and go back to what I had before. Hopefully there is just some setting/trait/etc. I'm missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2024-11-18 13:25:53

您可以尝试在序列化流之前强制流使用浮点科学格式:

ss << std::scientific;

看起来 boost 库正确设置了精度,但似乎没有设置格式。或者,我可以认为派生并覆盖用于保存和/或加载浮点数的逻辑,而不丢弃库的其余部分 - start 此处

看起来还有支持无穷大等方面的工作正在进行中

You could try forcing your stream to use scientific format for floating point before serialising to it:

ss << std::scientific;

It looks like the boost library sets the precision correctly, but doesn't appear to set the format. Alternatively, you can I think derive and override the logic for saving and/or loading floating point numbers without throwing away the rest of the library - start here.

It also looks like there is work in progress on supporting infinities etc.

眼藏柔 2024-11-18 13:25:53

这并不能直接回答 Boost.Serialization 问题,而是一种可能的解决方法。

从上面的问题来看,我不清楚您是否需要字符串表示形式。如果您正在寻找跨平台表示(二进制或其他形式),请考虑使用 protobuf,它确实支持编码 double 。

http://code.google.com/apis/protocolbuffers/docs/proto .html#标量

This doesn't directly answer the Boost.Serialization question, but is a possible workaround.

From the above question, it's not clear to me that you need a string representation or not. If you are looking for a cross-platform representation (binary or otherwise), consider using protobuf, which does have support for encoding doubles.

http://code.google.com/apis/protocolbuffers/docs/proto.html#scalar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文