jorgen / json_struct

json_struct is a single header only C++ library for parsing JSON directly to C++ structs and vice versa
Other
422 stars 57 forks source link

how to serialize std::map and std::set ? #27

Closed yanzixiang closed 2 years ago

yanzixiang commented 2 years ago

can I use this to serialize std::map and std::set ? for example i define a set A, I use JS::serializeStruct(A) give me a error. expect the output is ["1" , "2", "3" ]...

jorgen commented 2 years ago

Hi, I have been putting off making these TypeHandlers since then we have to include <map>, <set>, <unordered_set> in json_struct.h and suddenly everyone has has these includes just by including json_struct. I regret most of the includes in there already, but changing this can break user code, (I still have some members with spelling mistakes because chaning it will break code).

I have come up with this scheme where the TypeHandlers are guarded by their own include guards. So if you want the std::map TypeHandler then you will have to define JS_STL_MAP before including json_struct.h. The unit test should give you the idea: https://github.com/jorgen/json_struct/blob/master/tests/json-unordered-map.cpp#L4.

This is available in 3ddf92eef27b2b8388dcb06d43dd8bdf473d59bf.

yanzixiang commented 2 years ago

you just add JS_STL_MAP to support map , what about set ? without this, how to serialize a list without knowing how many items in it ?