Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization]Nans and infinity in wide xml archive
From: Robert Ramey (ramey_at_[hidden])
Date: 2017-07-03 15:08:14


On 7/3/17 2:23 AM, Elizabeta via Boost-users wrote:
> Hi
> I am using wide xml boost serialization archive. Because of the problem with
> loading of nans and infinities I tried the nonfinite facet as in the code
> snippet. This works, expect
> it messes with the encoding of the xml arhive which is no longer UTF-8 like
> before the using the facet, and I believe it is because of the no_codecvt
> flag which is passed to the archive. But without this flag the loading of
> nans/infinity does not work. How to use the facet and keep the wanted UTF-8
> encoding ?
>
> float d = std::numeric_limits<float>::quiet_NaN;
> {
> std::wofstream oss("C:\\test.xml");
> std::locale locale1(oss.getloc(), new
> boost::math::nonfinite_num_put<wchar_t>);
>
> oss.imbue(locale1);
> xml_woarchive oar(oss, no_codecvt);
> oar << BOOST_SERIALIZATION_NVP(d);
> }
>
> {
> float d;
> std::wifstream iss("C:\\test.xml");
> std::locale locale1(iss.getloc(), new
> boost::math::nonfinite_num_get<wchar_t>);
> iss.imbue(locale1);
> xml_wiarchive iar(iss, no_codecvt);
> iar >> BOOST_SERIALIZATION_NVP(d);
> std::cout << d << std::endl;
> }
>

This is an old problem going back many years. The problem isn't really
the serialization library, but rather writing and reading NaN values to
a text stream. There have been solutions proposed, but there hasn't
been enough motivation pursue it. These solution address the issue at
the stream level and don't touch serialization. For more information it
would be best to search the mailing list - going back many years.

Note that this problem occurs only with stream/text i/o. Binary formats
don't suffer from this - but they are, of course, do not create/use
portable archives.

One expedient way to address this at the serialization level is to step
back and ask oneself why he is serializing NaN values in the first
place. They generally contain no useful information. One could catch
them on output and replace them with a custom flag or boost.optional or
some ad hoc solution. Or one could fix the NaN text i/o issue at a
lower level.

Robert Ramey


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net