0

Is it possible for XML serialization to use more human friendly class_id as GUID, described using BOOST_CLASS_EXPORT_GUID ???

Consider serializing class:

SomeClass* b=new SomeClass("c");
{
    boost::archive::xml_oarchive oa(cout);
    oa.register_type<SomeClass>();
    oa << boost::serialization::make_nvp("b",b);
}

Output will be like:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="5">
<b class_id="0" tracking_level="1" version="0" object_id="_0">
<name>c</name>
</b>
</boost_serialization>

When you remove class_id="0" this will not deserialize. I would prefer class_id="SomeClass" or something similar.

Arpegius
  • 5,817
  • 38
  • 53

1 Answers1

4

Yes, the solution is to serialize your class in a name-value-pair. See this item at boost documentation.

If you want two diferent behaviours, you will have to implement them. Try with template specialization:

template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
    ar & degrees;
    ar & minutes;
    ar & seconds;
}

template<class Archive>
void serialize_with_name(Archive & ar, const unsigned int version)
{
    ar & make_nvp("degrees", degrees);
    ar & make_nvp("minutes", minutes);
    ar & make_nvp("seconds", seconds);
}

template<>
void serialize<>(xml_iarchive & ar, const unsigned int version)
{
    serialize_with_name(ar, version);
}

template<>
void serialize<>(xml_oarchive & ar, const unsigned int version)
{
    serialize_with_name(ar, version);
}

By default object_id_type is unsigned int (basic_archive.hpp). If you want something diferent you need to implement your own archive class.

Fernando N.
  • 6,369
  • 4
  • 27
  • 30
  • It's great idea to write warper over pointer, save the class name and dereferenced pointer( 2 additional nvp), and serialize using hint boost::serialization::object_serializable (remove class_id). But when you change to binnary_oarchive you need to serialize the class name not just integer. – Arpegius Jul 29 '09 at 23:54
  • “need to implement your own archive class” thanks, that's what i want to know. I currently choose YAML++, it's look good for that task. – Arpegius Aug 03 '09 at 08:09