67 const auto inputFilePath = testFilePath(
"some_structs.h");
68 const auto inputFiles = vector<const char *>{ inputFilePath.data() };
69 const auto clangOptions
70 = vector<std::string_view>{
"-resource-dir", REFLECTION_GENERATOR_CLANG_RESOURCE_DIR,
"-std=c++17",
"-I", CPP_UTILITIES_INCLUDE_DIRS,
71#ifdef RAPIDJSON_INCLUDE_DIRS
72 "-I", RAPIDJSON_INCLUDE_DIRS
79 jsonOptions.
additionalClassesArg.occurrenceInfo().back().values.emplace_back(
"TestNamespace2::ThirdPartyStruct");
80 CodeFactory factory(TestApplication::appPath(), inputFiles, clangOptions, buffer);
82 CPPUNIT_ASSERT(factory.
run());
94 string stdout, stderr;
96 const string inputFilePath(testFilePath(
"some_structs.h"));
97 const char *
const args1[] = { PROJECT_NAME,
"--input-file", inputFilePath.data(),
"--json-classes",
"TestNamespace2::ThirdPartyStruct",
98 "--clang-opt",
"-resource-dir", REFLECTION_GENERATOR_CLANG_RESOURCE_DIR,
"-std=c++17",
"-I", CPP_UTILITIES_INCLUDE_DIRS,
99#ifdef RAPIDJSON_INCLUDE_DIRS
100 "-I", RAPIDJSON_INCLUDE_DIRS,
103 TESTUTILS_ASSERT_EXEC(args1);
119 const string expectedJSON(
120 "{\"someInt\":42,\"someSize\":2863311530,\"someString\":\"the answer\",\"yetAnotherString\":\"but what was the question\"}");
123 CPPUNIT_ASSERT_EQUAL(expectedJSON,
string(test.toJson().GetString()));
126 const TestStruct parsedTest(TestStruct::fromJson(expectedJSON));
144 nested.
nested.emplace_front(vector<TestStruct>{ test });
145 nested.deq.emplace_back(3.14);
146 const string expectedJSON(
147 "{\"nested\":[[{\"someInt\":42,\"someSize\":2863311530,\"someString\":\"the answer\",\"yetAnotherString\":\"but what was the "
148 "question\"}]],\"ptr\":null,\"deq\":[3.14]}");
151 CPPUNIT_ASSERT_EQUAL(expectedJSON,
string(nested.toJson().GetString()));
154 const NestedTestStruct parsedNested(NestedTestStruct::fromJson(expectedJSON));
155 CPPUNIT_ASSERT_EQUAL(1_st, parsedNested.
nested.size());
156 CPPUNIT_ASSERT_EQUAL(1_st, parsedNested.
nested.front().size());
157 CPPUNIT_ASSERT_EQUAL(1_st, parsedNested.deq.size());
163 CPPUNIT_ASSERT_EQUAL(3.14, parsedNested.deq.front());
177 const string expectedJSONForBase(
178 "{\"someInt\":42,\"someSize\":5,\"someString\":\"the answer\",\"yetAnotherString\":\"but what was the question\"}");
179 const string expectedJSONForDerived(
180 "{\"someInt\":42,\"someSize\":5,\"someString\":\"the answer\",\"yetAnotherString\":\"but what was the question\",\"someBool\":false}");
183 CPPUNIT_ASSERT_EQUAL(expectedJSONForBase,
string(as<TestStruct>(test).toJson().GetString()));
184 CPPUNIT_ASSERT_EQUAL(expectedJSONForDerived,
string(as<DerivedTestStruct>(test).toJson().GetString()));
187 const DerivedTestStruct parsedTest(JsonSerializable<DerivedTestStruct>::fromJson(expectedJSONForDerived));
207 const string expectedJSONForDerived(
208 "{\"someInt\":42,\"someSize\":4294967274,\"someString\":\"the answer\",\"yetAnotherString\":\"but what was the "
209 "question\",\"arrayOfStrings\":[\"array\",\"of\",\"strings\"],\"someBool\":false}");
212 CPPUNIT_ASSERT_EQUAL(expectedJSONForDerived,
string(as<MultipleDerivedTestStruct>(test).toJson().GetString()));
215 const MultipleDerivedTestStruct parsedTest(JsonSerializable<MultipleDerivedTestStruct>::fromJson(expectedJSONForDerived));
248 ReflectiveRapidJSON::AdaptedJsonSerializable<NotJsonSerializable>::value,
"can serialize NotJsonSerializable because of adaption macro");
249 static_assert(ReflectiveRapidJSON::AdaptedJsonSerializable<NestedNotJsonSerializable>::value,
250 "can serialize NestedNotJsonSerializable because of adaption macro");
251 static_assert(!ReflectiveRapidJSON::AdaptedJsonSerializable<OtherNotJsonSerializable>::value,
252 "can not serialize OtherNotJsonSerializable because adaption macro missing");
253 static_assert(!ReflectiveRapidJSON::AdaptedJsonSerializable<ReallyNotJsonSerializable>::value,
"can not serialize ReallyNotJsonSerializable");
256 const string strSimple(
"{\"butSerializableAnyways\":\"useful to adapt 3rd party structs\"}");
258 const string strNested(
"{\"asMember\":{\"butSerializableAnyways\":\"foo\"},\"asArrayElement\":[{\"butSerializableAnyways\":\"1\"},{"
259 "\"butSerializableAnyways\":\"2\"},{\"butSerializableAnyways\":\"3\"}],\"asTupleElement\":[42,{\"butSerializableAnyways\":"
263 CPPUNIT_ASSERT_EQUAL(strSimple,
string(ReflectiveRapidJSON::JsonReflector::toJson(simple).GetString()));
264 CPPUNIT_ASSERT_EQUAL(strNested,
string(ReflectiveRapidJSON::JsonReflector::toJson(nested).GetString()));
267 JsonDeserializationErrors errors;
268 const auto parsedSimple(ReflectiveRapidJSON::JsonReflector::fromJson<NotJsonSerializable>(strSimple, &errors));
269 CPPUNIT_ASSERT_EQUAL(0_st, errors.size());
271 const auto parsedNested(ReflectiveRapidJSON::JsonReflector::fromJson<NestedNotJsonSerializable>(strNested, &errors));
272 CPPUNIT_ASSERT_EQUAL(0_st, errors.size());
275 CPPUNIT_ASSERT_EQUAL(nested.
asArrayElement.size(), parsedNested.asArrayElement.size());
276 CPPUNIT_ASSERT_EQUAL(nested.
asArrayElement.at(0).butSerializableAnyways, parsedNested.asArrayElement.at(0).butSerializableAnyways);
277 CPPUNIT_ASSERT_EQUAL(nested.
asArrayElement.at(1).butSerializableAnyways, parsedNested.asArrayElement.at(1).butSerializableAnyways);
278 CPPUNIT_ASSERT_EQUAL(nested.
asArrayElement.at(2).butSerializableAnyways, parsedNested.asArrayElement.at(2).butSerializableAnyways);
279 CPPUNIT_ASSERT_EQUAL(get<0>(nested.
asTupleElement), get<0>(parsedNested.asTupleElement));
280 CPPUNIT_ASSERT_EQUAL(get<1>(nested.
asTupleElement).butSerializableAnyways, get<1>(parsedNested.asTupleElement).butSerializableAnyways);
291 const string strConstStruct(
"{\"modifiableInt\":24,\"constInt\":42}");
292 CPPUNIT_ASSERT_EQUAL(strConstStruct,
string(constStruct.toJson().GetString()));
294 JsonDeserializationErrors errors;
295 auto parsedConstStruct(constStruct.fromJson(strConstStruct));
296 CPPUNIT_ASSERT_EQUAL(0_st, errors.size());
297 CPPUNIT_ASSERT_EQUAL(24, parsedConstStruct.modifiableInt);
298 CPPUNIT_ASSERT_EQUAL(42, parsedConstStruct.constInt);