Closed ghost closed 5 years ago
I would like to use your lib with the following use-case:
JSON input can contain more values than the class has members Supported as the default behavior.
Some JSON values are optional, so I use pointers in the class Supported as the default behavior.
Some values are required, and I want an exception if it is missing Unfortunately this is not supported. If you want to add the code I will accept a pull request.
How do I get the behavior that
class members that are pointers are imported as nullptr when missing in JSON This can be done externally by creating correct constructors that correctly initialize pointers to nullptr.
JSON values that have no class members are ignored Supported as the default behavior.
class members that are no pointers have to be present in the JSON, otherwise, an exception should be thrown Unfortunately this is not supported. If you want to add the code I will accept a pull request.
Note: Declaring variables like this:
Test t1, t2, t3, t4;
Is probably not a good idea when you have not define the constructor. The members of these objects are uninitialized. It is just random coincidence that that member optionalNum
is nullptr
when you check.
If you explicitly invoke the zero-initialization then you know that the object is zero-ed before use:
Test t1{}, t2{}, t3{}, t4{}; // Still not good practice to do this on one line.
Thanks for the help. When I have time I will come up with a PR, since we need such a feature. But I cannot promise it.
I think a goof place to look start is:
`DeSerializationForBlock::scanObject()`
ThorsSerializer/src/Serialize/Serialize.tpp Line 57.
To maintain backward compatibility. We should add another type of parser. ParserType::Exact
. If the parser type is Exact
then we don't allow extra members and we expect exactly all members to be specified. I don't think special casing pointer is a good idea. Pointer members can still be null
in the JSON. That way a Exact
parser requires all members and no extra members.
To implement this:
Add a std::map<std::string, bool>
here scanObject()
. Then each time around the loop mark the scanned member as true in your map. Then just before exiting if the parser is Exact
then check to see if all members have been parsed. You can find all the members that need to be parsed by calling:
Traits<T>:: getMembers()
This will return a tuple. Each member is a name and a function. You need to go through the tuple members to make sure that each member is in the map you built above. If you can not find a tuple member in the map you built then throw an exception.
OK. Just pushed a commit that makes this work. I'll submit to brew over the weekend
The new version is now live with brew.
brew install thors-serializer
Hi,
I would like to use your lib with the following use-case:
How do I get the behavior that
The following code has all 4 examples I consider: Is there a way to make t1, t2 and t3 work and let import into t4 throw an exception?
Thanks