What type trait is used to select between object and list-of-pairs serialisation for map types? #2719
Unanswered
eddyashton
asked this question in
Q&A
Replies: 1 comment 1 reply
-
We eventually found |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When converting a
std::map
(or similar type) to JSON, it will sometimes be converted to a JSON object and sometimes to a list of lists. This choice depends on the key type; if the key can be converted to string, then the map can be converted to an object. eg:The object case is not just for when the key is
std::string
, but somehow detects that it is convertible tostd::string
, so this even works for user-defined types:What makes this choice? I've poked around
json.hpp
but can't work out which bit of template machinery is doing this.Is this logic exposed as a type trait that I can reuse elsewhere, to determine whether my type will become an object?
My use case is that I'm trying to generate JSON schema that describe how my C++ types will be serialised to JSON. This works correctly for most types, but not for some map types. I currently only check if the key type is
std::string
, butnlohmann::json
will also produce an object for other types (ie - hasoperator std::string() const
defined, and potentially some other cases?). The relevant snippet is here:Is there something I can drop in to replace the
//< Inaccurate
line which will correctly detect all the types which are convertible to objects?Beta Was this translation helpful? Give feedback.
All reactions