json icon indicating copy to clipboard operation
json copied to clipboard

std::map<Key, Value> serialiaze

Open heretic13 opened this issue 4 years ago • 2 comments

Hello.

Maybe I didn't read the documentation carefully, but I didn't see an opportunity to serialize the following entity:

std :: map <Key, Value>; where struct Key {...}; struct Value {...};

Your implementation works if Key is a "simple entity" such as a string or an integer.

In general, you should provide programmers with the ability to write a function to convert a custom class / structure to and from a string. Your implementation should use this function to convert the Key element to a string and use the resulting string in a json object.

heretic13 avatar Nov 22 '20 08:11 heretic13

you should provide programmers with the ability to write a function to convert a custom class / structure to and from a string

That function already exists, it is a conversion operator. e.g. T::operator std::string() for your T.

vinniefalco avatar Nov 22 '20 19:11 vinniefalco

I have comments:

  1. Is this documented in your library?
  2. What about the reverse transformation? std::string -> "Key" object?
  3. This does not work in general. Imagine that there are structures or classes written by another person / company (for example, this is the Windows SDK) and you have no right to modify header files. For this case, external functions (not class members) are needed for converting Type->std::string ang std::string-> Type. A couple of years ago I already had to write something similar for jsoncpp and then I used external functions.

Of course, you can make your classes inherited from foreign ones (Windows SDK) and define operator std::string () const; for a new class, but I think it's ugly.

heretic13 avatar Nov 23 '20 08:11 heretic13

value_to converts maps to objects when the key is is_string_like. If not, it is converted to array.

If your key type is not is_string_like, but you can "stringize" it and you want the map to be converted to object, then you can go for contextual conversions. The linked section has an example of exactly such conversion.

So, I'm closing this for now. If you feel like your case isn't addressed still, please reopen and explain why.

grisumbras avatar Aug 25 '23 17:08 grisumbras