3 __| | __| | | | JSON for Modern C++ (test suite)
4 | | |__ | | | | | | version 3.10.5
5 |_____|_____|_____|_|___| https://github.com/nlohmann/json
7 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8 SPDX-License-Identifier: MIT
9 Copyright (c) 2013-2022 Niels Lohmann <http://nlohmann.me>.
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #include "doctest_compatibility.h"
34 #include <nlohmann/json.hpp>
39 class person_with_private_data
44 json metadata
= nullptr;
47 bool operator==(const person_with_private_data
& rhs
) const
49 return name
== rhs
.name
&& age
== rhs
.age
&& metadata
== rhs
.metadata
;
52 person_with_private_data() = default;
53 person_with_private_data(std::string name_
, int age_
, json metadata_
)
54 : name(std::move(name_
))
56 , metadata(std::move(metadata_
))
59 NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_with_private_data
, age
, name
, metadata
)
62 class person_without_private_data_1
67 json metadata
= nullptr;
69 bool operator==(const person_without_private_data_1
& rhs
) const
71 return name
== rhs
.name
&& age
== rhs
.age
&& metadata
== rhs
.metadata
;
74 person_without_private_data_1() = default;
75 person_without_private_data_1(std::string name_
, int age_
, json metadata_
)
76 : name(std::move(name_
))
78 , metadata(std::move(metadata_
))
81 NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_without_private_data_1
, age
, name
, metadata
)
84 class person_without_private_data_2
89 json metadata
= nullptr;
91 bool operator==(const person_without_private_data_2
& rhs
) const
93 return name
== rhs
.name
&& age
== rhs
.age
&& metadata
== rhs
.metadata
;
96 person_without_private_data_2() = default;
97 person_without_private_data_2(std::string name_
, int age_
, json metadata_
)
98 : name(std::move(name_
))
100 , metadata(std::move(metadata_
))
104 NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_without_private_data_2
, age
, name
, metadata
)
106 class person_with_private_alphabet
109 bool operator==(const person_with_private_alphabet
& other
) const
111 return a
== other
.a
&&
166 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
)
169 class person_with_public_alphabet
172 bool operator==(const person_with_public_alphabet
& other
) const
174 return a
== other
.a
&&
230 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
)
232 } // namespace persons
234 TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE", T
,
235 persons::person_with_private_data
,
236 persons::person_without_private_data_1
,
237 persons::person_without_private_data_2
)
242 T
p1("Erik", 1, {{"haircuts", 2}});
243 CHECK(json(p1
).dump() == "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}");
246 auto p2
= json(p1
).get
<T
>();
250 CHECK(T(json(p1
)) == p1
);
251 CHECK(json(T(json(p1
))) == json(p1
));
253 // check exception in case of missing field
256 CHECK_THROWS_WITH_AS(j
.get
<T
>(), "[json.exception.out_of_range.403] key 'age' not found", json::out_of_range
);
260 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
,
261 persons::person_with_private_alphabet
,
262 persons::person_with_public_alphabet
)
268 nlohmann::json j
= obj1
; //via json object
271 bool ok
= (obj1
== obj2
);
277 nlohmann::json j1
= obj1
; //via json string
278 std::string s
= j1
.dump();
279 nlohmann::json j2
= nlohmann::json::parse(s
);
282 bool ok
= (obj1
== obj2
);
288 nlohmann::json j1
= obj1
; //via msgpack
289 std::vector
<uint8_t> buf
= nlohmann::json::to_msgpack(j1
);
290 nlohmann::json j2
= nlohmann::json::from_msgpack(buf
);
293 bool ok
= (obj1
== obj2
);
299 nlohmann::json j1
= obj1
; //via bson
300 std::vector
<uint8_t> buf
= nlohmann::json::to_bson(j1
);
301 nlohmann::json j2
= nlohmann::json::from_bson(buf
);
304 bool ok
= (obj1
== obj2
);
310 nlohmann::json j1
= obj1
; //via cbor
311 std::vector
<uint8_t> buf
= nlohmann::json::to_cbor(j1
);
312 nlohmann::json j2
= nlohmann::json::from_cbor(buf
);
315 bool ok
= (obj1
== obj2
);
321 nlohmann::json j1
= obj1
; //via ubjson
322 std::vector
<uint8_t> buf
= nlohmann::json::to_ubjson(j1
);
323 nlohmann::json j2
= nlohmann::json::from_ubjson(buf
);
326 bool ok
= (obj1
== obj2
);