mirror of
https://github.com/nlohmann/json
synced 2024-11-15 08:40:05 +00:00
[JSON] as per Nlohmann's suggestion, added unit tests for the (de)serialisation macros
This commit is contained in:
parent
75a6888d55
commit
6d9b2040ab
@ -27,6 +27,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "doctest_compatibility.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
@ -100,6 +102,85 @@ class person_without_private_data_2
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_without_private_data_2, age, name, metadata)
|
||||
|
||||
class person_with_private_alphabet
|
||||
{
|
||||
public:
|
||||
bool operator==(const person_with_private_alphabet& other)
|
||||
{
|
||||
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) ==
|
||||
std::tie(other.a, other.b, other.c, other.d, other.e, other.f, other.g, other.h, other.i, other.j, other.k, other.l, other.m, other.n, other.o, other.p, other.q, other.r, other.s, other.t, other.u, other.v, other.w, other.x, other.y, other.z);
|
||||
}
|
||||
|
||||
private:
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
int d;
|
||||
int e;
|
||||
int f;
|
||||
int g;
|
||||
int h;
|
||||
int i;
|
||||
int j;
|
||||
int k;
|
||||
int l;
|
||||
int m;
|
||||
int n;
|
||||
int o;
|
||||
int p;
|
||||
int q;
|
||||
int r;
|
||||
int s;
|
||||
int t;
|
||||
int u;
|
||||
int v;
|
||||
int w;
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_with_private_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
|
||||
};
|
||||
|
||||
class person_with_public_alphabet
|
||||
{
|
||||
public:
|
||||
bool operator==(const person_with_public_alphabet& other)
|
||||
{
|
||||
return std::tie(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) ==
|
||||
std::tie(other.a, other.b, other.c, other.d, other.e, other.f, other.g, other.h, other.i, other.j, other.k, other.l, other.m, other.n, other.o, other.p, other.q, other.r, other.s, other.t, other.u, other.v, other.w, other.x, other.y, other.z);
|
||||
}
|
||||
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
int d;
|
||||
int e;
|
||||
int f;
|
||||
int g;
|
||||
int h;
|
||||
int i;
|
||||
int j;
|
||||
int k;
|
||||
int l;
|
||||
int m;
|
||||
int n;
|
||||
int o;
|
||||
int p;
|
||||
int q;
|
||||
int r;
|
||||
int s;
|
||||
int t;
|
||||
int u;
|
||||
int v;
|
||||
int w;
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_with_public_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
|
||||
|
||||
} // namespace persons
|
||||
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE", T,
|
||||
@ -128,3 +209,69 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU
|
||||
CHECK_THROWS_WITH_AS(p3 = json(j), "[json.exception.out_of_range.403] key 'age' not found", json::out_of_range);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/private member variables via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", T,
|
||||
persons::person_with_private_alphabet,
|
||||
persons::person_with_public_alphabet)
|
||||
{
|
||||
SECTION("person")
|
||||
{
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json j = obj1; //via json object
|
||||
T obj2;
|
||||
j.get_to(obj2);
|
||||
CHECK(obj1 == obj2);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json j1 = obj1; //via json string
|
||||
std::string s = j1.dump();
|
||||
nlohmann::json j2 = nlohmann::json::parse(s);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
CHECK(obj1 == obj2);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json j1 = obj1; //via msgpack
|
||||
std::vector<uint8_t> buf = nlohmann::json::to_msgpack(j1);
|
||||
nlohmann::json j2 = nlohmann::json::from_msgpack(buf);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
CHECK(obj1 == obj2);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json j1 = obj1; //via bson
|
||||
std::vector<uint8_t> buf = nlohmann::json::to_bson(j1);
|
||||
nlohmann::json j2 = nlohmann::json::from_bson(buf);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
CHECK(obj1 == obj2);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json j1 = obj1; //via cbor
|
||||
std::vector<uint8_t> buf = nlohmann::json::to_cbor(j1);
|
||||
nlohmann::json j2 = nlohmann::json::from_cbor(buf);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
CHECK(obj1 == obj2);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json j1 = obj1; //via ubjson
|
||||
std::vector<uint8_t> buf = nlohmann::json::to_ubjson(j1);
|
||||
nlohmann::json j2 = nlohmann::json::from_ubjson(buf);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
CHECK(obj1 == obj2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user