Skip to content

Instantly share code, notes, and snippets.

@Rapptz

Rapptz/a.cpp Secret

Created February 8, 2016 03:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rapptz/0a7c051f5d027382c564 to your computer and use it in GitHub Desktop.
Save Rapptz/0a7c051f5d027382c564 to your computer and use it in GitHub Desktop.
#include <catch.hpp>
#include <jsonpp/convert.hpp>
#include <jsonpp/dump.hpp>
#include <jsonpp/parser.hpp>
#include <tuple>
namespace ns {
struct user {
std::string name;
json::value avatar;
std::vector<std::string> roles;
};
struct user_schema {
template<typename Algo>
void operator()(Algo& algo, const user& u) const {
algo.member("name", u.name);
algo.member("avatar", u.avatar);
algo.member("roles", u.roles);
}
template<typename Algo>
void operator()(Algo& algo, user& u) const {
algo.member("name", u.name);
algo.member("avatar", u.avatar);
algo.member("roles", u.roles);
}
};
inline bool operator==(const user& lhs, const user& rhs) JSONPP_NOEXCEPT {
return std::tie(lhs.name, lhs.avatar, lhs.roles) == std::tie(rhs.name, rhs.avatar, rhs.roles);
}
} // ns
namespace json {
template<> struct json_schema<ns::user> : ns::user_schema {};
} // json
TEST_CASE("conversion", "[conversion]") {
constexpr auto& rawtext = R"js(
{
"avatar": null,
"name": "Danny",
"roles": [
"Cool",
"Programmer",
"Admin"
]
})js";
const std::string payload = { rawtext + 1, std::end(rawtext) - 1 };
ns::user danny = {
"Danny",
nullptr,
{ "Cool", "Programmer", "Admin" }
};
SECTION("serialisation") {
auto js = json::dump_string(json::to_json(danny));
REQUIRE(js == payload);
}
SECTION("deserialisation") {
json::value v;
REQUIRE_NOTHROW(json::parse(rawtext, v));
REQUIRE(v.is<json::object>());
ns::user otherway;
REQUIRE_NOTHROW(json::from_json(v, otherway));
REQUIRE(danny == otherway);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment