2016-10-16 23:29:57 +08:00
|
|
|
/*
|
|
|
|
__ _____ _____ _____
|
|
|
|
__| | __| | | | JSON for Modern C++ (test suite)
|
2016-11-17 03:49:24 +08:00
|
|
|
| | |__ | | | | | | version 2.0.7
|
2016-10-16 23:29:57 +08:00
|
|
|
|_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
|
|
|
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
|
|
|
Copyright (c) 2013-2016 Niels Lohmann <http://nlohmann.me>.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
#include <array>
|
2016-10-16 23:29:57 +08:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
#include "catch.hpp"
|
|
|
|
|
|
|
|
#include "json.hpp"
|
|
|
|
|
|
|
|
namespace udt
|
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
struct age
|
2016-10-18 05:41:53 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
int val;
|
|
|
|
};
|
2016-10-18 05:41:53 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
struct name
|
2016-10-16 23:29:57 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
std::string val;
|
|
|
|
};
|
2016-10-16 23:29:57 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
struct address
|
2016-10-16 23:29:57 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
std::string val;
|
|
|
|
};
|
2016-10-16 23:29:57 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
struct person
|
2016-10-16 23:29:57 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
age age;
|
|
|
|
name name;
|
|
|
|
};
|
2016-10-16 23:29:57 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
struct contact
|
2016-10-16 23:29:57 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
person person;
|
|
|
|
address address;
|
|
|
|
};
|
2016-10-16 23:29:57 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
struct contact_book
|
2016-10-16 23:29:57 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
name book_name;
|
|
|
|
std::vector<contact> contacts;
|
|
|
|
};
|
2016-10-16 23:29:57 +08:00
|
|
|
}
|
2016-10-18 05:41:53 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
// to_json methods for default basic_json
|
|
|
|
namespace udt
|
2016-10-18 05:41:53 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
void to_json(nlohmann::json& j, age a)
|
2016-10-18 05:41:53 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
j = a.val;
|
2016-10-18 05:41:53 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
void to_json(nlohmann::json& j, name const& n)
|
2016-10-18 05:41:53 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
j = n.val;
|
2016-10-18 05:41:53 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
void to_json(nlohmann::json& j, person const& p)
|
2016-10-18 05:41:53 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
using nlohmann::json;
|
|
|
|
j = json{{"age", json{p.age}}, {"name", json{p.name}}};
|
2016-10-18 05:41:53 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
// this unfortunately does not compile ...
|
|
|
|
// j["age"] = p.age;
|
|
|
|
// j["name"] = p.name;
|
2016-10-18 05:41:53 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
void to_json(nlohmann::json& j, address const& a)
|
2016-10-18 05:41:53 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
j = a.val;
|
2016-10-18 05:41:53 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
void to_json(nlohmann::json& j, contact const& c)
|
2016-10-18 05:41:53 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
using nlohmann::json;
|
|
|
|
j = json{{"person", json{c.person}}, {"address", json{c.address}}};
|
2016-10-18 05:41:53 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
void to_json(nlohmann::json& j, contact_book const& cb)
|
2016-10-18 05:41:53 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
using nlohmann::json;
|
|
|
|
j = json{{"name", json{cb.book_name}}, {"contacts", cb.contacts}};
|
2016-10-18 05:41:53 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-15 21:22:12 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
TEST_CASE("basic usage", "[udt]")
|
2016-11-15 21:22:12 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
using nlohmann::json;
|
2016-11-15 21:22:12 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
SECTION("conversion to json via free-functions")
|
2016-11-15 21:22:12 +08:00
|
|
|
{
|
2016-11-29 20:09:51 +08:00
|
|
|
udt::age a{23};
|
2016-11-15 21:22:12 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
CHECK(json{a} == json{23});
|
2016-11-15 21:22:12 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
// a bit narcissic maybe :) ?
|
|
|
|
udt::name n{"theo"};
|
|
|
|
CHECK(json{n} == json{"theo"});
|
2016-11-15 21:22:12 +08:00
|
|
|
|
2016-11-29 20:09:51 +08:00
|
|
|
udt::person sfinae_addict{a, n};
|
|
|
|
CHECK(json{sfinae_addict} == R"({"name":"theo", "age":23})"_json);
|
2016-11-15 21:22:12 +08:00
|
|
|
}
|
2016-11-29 20:09:51 +08:00
|
|
|
}
|