Skip to content

Commit

Permalink
Confirm that the container cereal_override works for nested lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidenori Shinohara committed Feb 20, 2021
1 parent 5b37d74 commit abe8047
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/cereal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,33 @@ cereal_override(cereal::JSONOutputArchive &ar,
xdr::archive(ar, 9999, "bort");
}

void
cereal_override(cereal::JSONOutputArchive &ar,
const testns::elem &t,
const char* field)
{
xdr::archive(ar,
std::make_tuple(cereal::make_nvp(std::string("Custom Member ID"), t.memberId),
cereal::make_nvp(std::string("Custom Member name"), t.memberName)),
field);
}

template <typename T>
std::enable_if_t<xdr::xdr_traits<T>::is_container>
cereal_override(cereal::JSONOutputArchive& ar, T const& t,
const char* field)
{
ar.setNextName(field);
ar.startNode();
ar(cereal::make_size_tag(0));
for (auto const& element : t)
{
xdr::archive(ar, element);
}
ar.finishNode();
}


#include <xdrpp/cereal.h>

using namespace std;
Expand Down Expand Up @@ -78,5 +105,52 @@ main()
assert(obuf2.str().find("\"bort\": 9999") != string::npos);
}

{
testns::elem e;
e.memberId = 3;
e.memberName = 500;
ostringstream obuf;
{
cereal::JSONOutputArchive ar(obuf);
xdr::archive(ar, e, "SingleElement");
}
std::cout << obuf.str() << std::endl;
}

{
testns::array1 ary1;
ary1.id = 123;
for (int j = 0; j < 3; j++)
{
ary1.ary1.extend_at(j).memberId = j;
ary1.ary1.extend_at(j).memberName = 1000 + j;
}
ostringstream obuf;
{
cereal::JSONOutputArchive ar(obuf);
xdr::archive(ar, ary1, "Flat Array");
}
std::cout << obuf.str() << std::endl;
}
{
testns::array2 a;
a.id = 123;
for (int i = 0; i < 3; i++)
{
a.ary2.extend_at(i).id = 100 * i;
for (int j = 0; j < 3; j++)
{
a.ary2.extend_at(i).ary1.extend_at(j).memberId = 10 * i + j;
a.ary2.extend_at(i).ary1.extend_at(j).memberName = 1000 * i + j;
}
}
ostringstream obuf;
{
cereal::JSONOutputArchive ar(obuf);
xdr::archive(ar, a, "nestedArray");
}
std::cout << obuf.str() << std::endl;
}

return 0;
}
16 changes: 16 additions & 0 deletions tests/xdrtest.x
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,20 @@ struct outer {
inner in;
};

struct elem {
int memberId;
int memberName;
};

struct array1 {
int id;
elem ary1<3>;
};

struct array2 {
int id;
array1 ary2<3>;
};


}

0 comments on commit abe8047

Please sign in to comment.