Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using boost::describe to serialize/unserialize xml (xml_oarchive/xm_iarchive is too limited) #242

Open
vricosti opened this issue Oct 11, 2021 · 0 comments

Comments

@vricosti
Copy link

vricosti commented Oct 11, 2021

Hi,

I am using describe to serialize.unserialize some structs to json (thanks to boost::json) but now I would also be able to do it in xml.
I tried boost::archive::: (xml_oarchive/xml_iarchive) but the resulting xml always have some useless nodes ( <xxx_version>) that cannot be easily removed from what I understand.
Let's consider the following code:

namespace JsonSerializer
{
   struct Item
   {
      std::string id;
      std::string label;
      std::string iconFile;
   };
   BOOST_DESCRIBE_STRUCT(Item, (), (id, label, iconFile))

   struct Items
   {
      using Type = std::vector<Item>;
      Type items;
   };
   BOOST_DESCRIBE_STRUCT(Items, (), (items));

} // namespace JsonSerializer


// Lines below allows to not add bunch of ugly tags/info when serializing in xml
BOOST_CLASS_IMPLEMENTATION(JsonSerializer::Item, boost::serialization::object_serializable);
BOOST_CLASS_IMPLEMENTATION(JsonSerializer::Items::Type, boost::serialization::object_serializable);
BOOST_CLASS_IMPLEMENTATION(JsonSerializer::Items, boost::serialization::object_serializable);

Then I try to fill an Items object and I serialize it:

Items items;
// ... some code to fill items list ...
std::ostringstream os;
unsigned int flags = boost::archive::no_header;
boost::archive::xml_oarchive ar(os, flags);
ar << boost::make_nvp("items", items.items);
std::string sXml = os.str();

And the resulting xml looks like this:

<items>
	<count>1</count>
	<item_version>0</item_version>
	<item>
		<id>standalone</id>
		<label>StandAlone</label>
		<iconFile>Logo_StandAlone.png</iconFile>
	</item>
</items>

Even with all the ugly BOOST_CLASS_IMPLEMENTATION macros and the use of boost::archive::no_header, the result is not satisfying (useless item_version, count), so where should I start to get a clean xml ?
What can boost offers to serialize/unserialize in xml with boost/describe ?
It works fine with json so why not with xml ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant