Skip deserializing const member variables
This commit is contained in:
parent
c5968f1663
commit
2fa1e923f9
|
@ -39,6 +39,7 @@ The following table shows the mapping of supported C++ types to supported JSON t
|
||||||
* iteratables must provide an `emplace_back` method. So deserialization of eg. `std::forward_list`
|
* iteratables must provide an `emplace_back` method. So deserialization of eg. `std::forward_list`
|
||||||
is currently not supported.
|
is currently not supported.
|
||||||
* custom types must provide a default constructor.
|
* custom types must provide a default constructor.
|
||||||
|
* constant member variables are skipped.
|
||||||
* For custom (de)serialization, see the section below.
|
* For custom (de)serialization, see the section below.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
|
@ -245,6 +245,10 @@ void JsonSerializationCodeGenerator::generate(ostream &os) const
|
||||||
" }\n"
|
" }\n"
|
||||||
" // pull members\n";
|
" // pull members\n";
|
||||||
for (const clang::FieldDecl *field : relevantClass.record->fields()) {
|
for (const clang::FieldDecl *field : relevantClass.record->fields()) {
|
||||||
|
// skip const members
|
||||||
|
if (field->getType().isConstant(field->getASTContext())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (pullPrivateMembers || field->getAccess() == clang::AS_public) {
|
if (pullPrivateMembers || field->getAccess() == clang::AS_public) {
|
||||||
os << " pull(reflectable." << field->getName() << ", \"" << field->getName() << "\", value, errors);\n";
|
os << " pull(reflectable." << field->getName() << ", \"" << field->getName() << "\", value, errors);\n";
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ class JsonGeneratorTests : public TestFixture {
|
||||||
CPPUNIT_TEST(testMultipleInheritence);
|
CPPUNIT_TEST(testMultipleInheritence);
|
||||||
CPPUNIT_TEST(testCustomSerialization);
|
CPPUNIT_TEST(testCustomSerialization);
|
||||||
CPPUNIT_TEST(test3rdPartyAdaption);
|
CPPUNIT_TEST(test3rdPartyAdaption);
|
||||||
|
CPPUNIT_TEST(testHandlingConstMembers);
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -46,6 +47,7 @@ public:
|
||||||
void testMultipleInheritence();
|
void testMultipleInheritence();
|
||||||
void testCustomSerialization();
|
void testCustomSerialization();
|
||||||
void test3rdPartyAdaption();
|
void test3rdPartyAdaption();
|
||||||
|
void testHandlingConstMembers();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const vector<string> m_expectedCode;
|
const vector<string> m_expectedCode;
|
||||||
|
@ -258,6 +260,24 @@ void JsonGeneratorTests::test3rdPartyAdaption()
|
||||||
CPPUNIT_ASSERT_EQUAL(get<1>(nested.asTupleElement).butSerializableAnyways, get<1>(parsedNested.asTupleElement).butSerializableAnyways);
|
CPPUNIT_ASSERT_EQUAL(get<1>(nested.asTupleElement).butSerializableAnyways, get<1>(parsedNested.asTupleElement).butSerializableAnyways);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Tests whether const member variables are ignored when deserializing.
|
||||||
|
* \remarks This test is very simple since no checks/diagnostics for const members have been implemented yet.
|
||||||
|
*/
|
||||||
|
void JsonGeneratorTests::testHandlingConstMembers()
|
||||||
|
{
|
||||||
|
ConstStruct constStruct;
|
||||||
|
constStruct.modifiableInt = 24;
|
||||||
|
const string strConstStruct("{\"modifiableInt\":24,\"constInt\":42}");
|
||||||
|
CPPUNIT_ASSERT_EQUAL(strConstStruct, string(constStruct.toJson().GetString()));
|
||||||
|
|
||||||
|
JsonDeserializationErrors errors;
|
||||||
|
auto parsedConstStruct(constStruct.fromJson(strConstStruct));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(0_st, errors.size());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(24, parsedConstStruct.modifiableInt);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(42, parsedConstStruct.constInt);
|
||||||
|
}
|
||||||
|
|
||||||
// include file required for reflection of TestStruct and other structs defined in structs.h
|
// include file required for reflection of TestStruct and other structs defined in structs.h
|
||||||
// NOTE: * generation of this header is triggered using the CMake function add_reflection_generator_invocation()
|
// NOTE: * generation of this header is triggered using the CMake function add_reflection_generator_invocation()
|
||||||
// * the include must happen in exactly one translation unit of the project at a point where the structs are defined
|
// * the include must happen in exactly one translation unit of the project at a point where the structs are defined
|
||||||
|
|
|
@ -19,4 +19,13 @@ struct IncludedStruct : public JsonSerializable<IncludedStruct> {
|
||||||
int someInt = 0;
|
int someInt = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief The ConstStruct struct is used to test handling of const members.
|
||||||
|
* \remarks Those members should be ignored when deserializing.
|
||||||
|
*/
|
||||||
|
struct ConstStruct : public JsonSerializable<ConstStruct> {
|
||||||
|
int modifiableInt = 23;
|
||||||
|
const int constInt = 42;
|
||||||
|
};
|
||||||
|
|
||||||
#endif // REFLECTIVE_RAPIDJSON_TESTS_MORE_STRUCTS_H
|
#endif // REFLECTIVE_RAPIDJSON_TESTS_MORE_STRUCTS_H
|
||||||
|
|
Loading…
Reference in New Issue