Skip deserializing const member variables

This commit is contained in:
Martchus 2017-12-31 00:48:51 +01:00
parent c5968f1663
commit 2fa1e923f9
4 changed files with 34 additions and 0 deletions

View File

@ -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`
is currently not supported.
* custom types must provide a default constructor.
* constant member variables are skipped.
* For custom (de)serialization, see the section below.
## Usage

View File

@ -245,6 +245,10 @@ void JsonSerializationCodeGenerator::generate(ostream &os) const
" }\n"
" // pull members\n";
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) {
os << " pull(reflectable." << field->getName() << ", \"" << field->getName() << "\", value, errors);\n";
}

View File

@ -34,6 +34,7 @@ class JsonGeneratorTests : public TestFixture {
CPPUNIT_TEST(testMultipleInheritence);
CPPUNIT_TEST(testCustomSerialization);
CPPUNIT_TEST(test3rdPartyAdaption);
CPPUNIT_TEST(testHandlingConstMembers);
CPPUNIT_TEST_SUITE_END();
public:
@ -46,6 +47,7 @@ public:
void testMultipleInheritence();
void testCustomSerialization();
void test3rdPartyAdaption();
void testHandlingConstMembers();
private:
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);
}
/*!
* \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
// 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

View File

@ -19,4 +19,13 @@ struct IncludedStruct : public JsonSerializable<IncludedStruct> {
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