开发者

remove versioning on boost xml serialization

i just can't find a way to remove the version tracking from the boost xmlarchives.

example

    <Settings class_id="0" tracking_level="0" version="1">
     <px class_id="1" tracking_level="1" version="0" object_id="_0">
      <TestInt>3</TestInt>
      <Resolution class_id="2" tracking_level="0" version="0">
       <x>800</x>
       <y>600</y>
      </Resolution>
      <SomeStuff>0</SomeStuff>
     </px>
    </Settings>

I want to get ride of the class_id="0" tracking_level="0" version="1" stuff, because for in this case i just don't need it and want a simple clean config like file

code

void serialize(Archive 开发者_StackOverflow中文版& ar, const unsigned int version)
{
  ar & make_nvp("TestInt", TestInt);

  ar & make_nvp("Resolution", resolution);
  ar & make_nvp("SomeStuff", SomeStuff);
}

i found boost::serialization::track_never, but nowhere to use it


while too late for the original poster I'd like to share what I've found

BOOST_CLASS_IMPLEMENTATION(My_class, object_serializable)

does the trick.


To remove the header of an xml archive file you can use

boost::archive::xml_iarchive ia(is, boost::archive::no_header);

To disable the attributes class_id, tracking_level and version from being displayed you'll have to use

BOOST_CLASS_IMPLEMENTATION( <type>, boost::serialization::object_serializable )
BOOST_CLASS_TRACKING( <type>, boost::serialization::track_never )

for each type. Theses macros need to be called in this order. N.B. you cannot use

BOOST_CLASS_VERSION

with macros described above.


try to create iarchive with "no_header" option:

boost::archive::xml_iarchive ia(is, boost::archive::no_header);


There is some confusion here regarding "tracking".

"tracking" refers to the accumulation of pointers in a table to object which have been already serialized in the archive. This permits archives which happen to serialization the same object twice - e.g. an object referred to by a shared pointer, to be serialized only once. In this case, every object is assigned an "object_id" the first time it is serialized so that subsequent times only the object _id needs to be serialized and not the whole object. This can save lot's of space. It also guarantees that objects serialized through a shared pointer are re-constituted as only one object when loaded - thus preserving the original file structure. In your example above you can see that with tracking an object is assigned an object_id = "_0". When the implementation level is altered so that tracking is not used, there is no object_id assigned. You can see this in the example. The usage of implementation level is usually just set by default. In some cases tracking is very inconvenient. Consider what would happen if serialization of an integer type were tracked by default. This would add a huge amount to the archive for no value. If one really needs to track an integer serialize through a pointer, the best would be to just wrap that integer in it's own class.

Note that this is an entirely different think than versioning. Version is the program serialization specification for that code. IIRC this is also determined by the implementation level setting for the class.

Also there is program version which did the serialization. This is in the archive header. This permits one to make program changes backward compatible even for classes which don't have class versions. This can be suppressed with "no_header" on the archive. Beware of the possibility of creating write only archives.

Sooo... I think the system is working as advertised - though maybe the advertising could be improved.


There is a (AFAIK undocumented) flag called boost::archive::no_tracking but doesn't seem to have any effect. Perhaps it is a bug (in XML archives at least) (see comment below):

    boost::archive::xml_oarchive oa{std::cout, boost::archive::no_header | boost::archive::no_tracking}; 

(http://boost.2283326.n4.nabble.com/serialization-removing-excess-XML-tags-td2582827.html)

Example output (still has the version and tracking at the top level and for all the items).

<my_carbon class_id="0" tracking_level="0" version="0">
    <number>6</number>
    <symbol>C</symbol>
    <AR_electronegativity class_id="1" tracking_level="0" version="0">
        <has_value>1</has_value>
        <value>2.50000000000000000e+00</value>
    </AR_electronegativity>
    <covalent_radius class_id="2" tracking_level="0" version="0">
        <has_value>1</has_value>
        <value class_id="3" tracking_level="0" version="0">
            <value>7.60000000000000009e-01</value>
        </value>
    </covalent_radius>
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜