Simplification of use #455
Replies: 2 comments
-
@johan-gson Regarding deserialization, on the other hande, it should already be possible to do your suggestion with either the
Regarding the synchronize() function you suggested, I'm not sure I fully understand it. Do you assume it to be usable for both serialization and deserialization? I agree to this part "you only need to implement this function for the custom stuctures, so the custom code is very small and simple", but I'm not yet convinced to provide a single interface for serialization and deserialization instead of separating them. |
Beta Was this translation helpful? Give feedback.
-
Thanks, I didn't realize it was supported for deserialization, I started with serialization, good to know! Yep, you are right, it should be possible to only define one function that both serializes and deserializes, although possible it is only in rare cases you need to treat them differently in the custom code (but that is needed in simple types, vectors, etc., which are all part of the library). We have done this previously for applications like network communication and reading/writing xml config files, in the end it reduces the serialization code to almost half. An idea would be to change the default implementation of synchronize to template<typename NodeType, typename Target> this would keep everything backwards compatible. I envision that it would work something like this: template template //don't remember the syntax for these templates, this is maybe not exactly right //.... Repeat for all basic types, I haven't been digging in the code enough to figure out how to do it, but this should be doable //and now the function "member" //example: synchronize for vector (in principle, I didn't look into what is pointers and not, the code may have to be modified accordingly) I think this would do the trick - probably some bugs in it, but in principle. - a downside is of course that it requires a default constructor to be available and that you may need to do a const_cast somewhere in serialize, or copy the input. It would also be possible to allow for having synchronize as a member function perhaps as a way to treat private members. I don't have the time to implement stuff, but if someone would have the energy to do it, I think this would improve the package! |
Beta Was this translation helpful? Give feedback.
-
Hi, great work!
If anyone has the energy, it should be possible to replace the to_node and from_node for custom structures with something like this:
Unless I misunderstood something!
template<typename NodeType, typename Target>
void synchronize(NodeType& node, Target& target, bool toNode) { throw std::runtime_error("type not impl"); }
template
void synchronize(NodeType& node, MyTarget& target, bool toNode) {
fkyaml::member(node, "var1", target.var1, toNode);
fkyaml::member(node, "var2", target.var2, toNode);
fkyaml::member(node, "vector1", target.vector1, toNode); //this vector could contain anything as long as there is a synchronize function for that type, the code should be able to handle that automatically
}
synchronize (or whatever it should be called) would then be called from both serialize and deserialize, with toNode set to true for deserialize and false for serialize. The toNode flag only rarely needs to be used
in customized code (i.e., by users of the library) but would be used for basic types, vectors, etc.
The function member would either go down in the existing tree (serialize) or create a new node. It may be necessary to also have a node_type function similar to synchronize, which would be default implemented to be mapping.
You would then implement generic functions for both basic types (i.e., a specialization of Target for a different type) and std::vector etc. that is part of fkyaml. We have done similar things in the past, works nicely, it means you only need to implement this function for the custom stuctures, so the custom code is very small and simple.
Unless I am mistaken, the support for custom types in vectors etc. is currently not that well supported, you have to implement serialization/deserialization at the higher level that includes the vector, loop through the subnodes, and deserialize the custom structures. With the pattern above, that would just work automatically. The template could also possibly contain a type traits that could contain options, such as formatting etc. But I could be wrong, maybe it is supported and I just didn't understand how to do it in the current code.
Just a thought after a bit of struggling and debugging!
Beta Was this translation helpful? Give feedback.
All reactions