From b44bddb5082f7306f0d79ab230108ebb37bb622c Mon Sep 17 00:00:00 2001 From: shuibao Date: Tue, 11 Oct 2022 08:59:07 +0800 Subject: [PATCH] add unit-inplace_array.cpp --- tests/src/unit-inplace_array.cpp | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tests/src/unit-inplace_array.cpp diff --git a/tests/src/unit-inplace_array.cpp b/tests/src/unit-inplace_array.cpp new file mode 100644 index 000000000..b47b093ad --- /dev/null +++ b/tests/src/unit-inplace_array.cpp @@ -0,0 +1,79 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ (supporting code) +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + +#include "doctest_compatibility.h" + +#include +#include +#include + +#include + +using json = nlohmann::json; +using ordered_json = nlohmann::ordered_json; + + +class Person +{ + public: + //Person(){ std::cout<<"Person constructor\n";} + int age; + std::string name; + NLOHMANN_DEFINE_TYPE_INTRUSIVE(Person, name, age); + + int count{1}; // the data must not be reset +}; + +class SchoolA +{ + public: + //SchoolA(){ std::cout<<"School constructor\n";} + std::array persons; + + NLOHMANN_DEFINE_TYPE_INTRUSIVE(SchoolA, persons); +}; +class SchoolB +{ + public: + //SchoolB(){ std::cout<<"School constructor\n";} + + Person persons[2]; + + NLOHMANN_DEFINE_TYPE_INTRUSIVE(SchoolB, persons); +}; + + +TEST_CASE("inplace_array") +{ + + json obj = R"({"persons":[{"age":100, "name":"alex"}, {"age":200, "name":"edmond"}]})"_json; + + { + SchoolA s; + from_json(obj, s); + CHECK(s.persons[0].age == 100); + + s.persons[0].count = 88; + + from_json(obj, s); + CHECK(s.persons[0].count == 88); + } + + { + SchoolB s; + from_json(obj, s); + + CHECK(s.persons[0].age == 100); + + s.persons[0].count = 88; + + from_json(obj, s); + CHECK(s.persons[0].count == 88); + } + +}