-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std::map key conversion with to_json #607
Comments
The problem is already when the unordered map is defined: #include <unordered_map>
enum class EC {
A
};
int main()
{
std::unordered_map<EC, int> imap;
} Error:
This can be fixed by defining a hash for type namespace std
{
template<>
struct hash<EC>
{
std::size_t operator()(const EC& e) const
{
return 0;
}
};
} Unfortunately, this does not yet fix the problem:
I'm not sure about this yet. :-( |
@theodelrieu Any idea on this? This may be related to #600. |
(Not helpful, but worth noting:) The README says:
In this example, there is no conversion from |
Indeed, only However, you can partially specialize namespace nlohmann {
template <typename T>
adl_serializer<std::unordered_map<EC, T>> {
// define to_json/from_json here
};
} |
Cool, thanks. I'll give that a try.
Why is this not supported via to/from_json?
…On Tue, Jun 6, 2017, 06:27 Théo DELRIEU ***@***.***> wrote:
Indeed, only map-like types with a key convertible to std::string are
supported by default.
However, you can partially specialize adl_serializer like this:
namespace nlohmann {template <typename T>
adl_serializer<std::unordered_map<EC, T>> {
// define to_json/from_json here
};
}
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#607 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAG6-RuN3cKIwG78WBgH8dEk1-NUdaK8ks5sBUWVgaJpZM4NvoOD>
.
|
We support JSON serialization for enums and enum classes by default, using the underlying numeric value. However, in this case, we are not converting an enum into a JSON value, but as a part of a JSON value (an object here). The library cannot call a The approach I mentioned ahead will allow you to handle that key "stringification" yourself. |
I agree with @theodelrieu. You need to provide your own enum-to-string conversion. |
@nlohmann and by that you mean via the I'll try out the |
The to/from_json functions only describe how a type can be converted to |
Okay, thanks. What is the proper usage of namespace nlohmann {
template <typename T>
adl_serializer<std::unordered_map<EC, T>> {
// define to_json/from_json here
void to_json(json& j, std::unordered_map<EC, T> const& map) {
// loop on map entries here?
// j = ...
}
// likewise for from_json?
};
} |
I think @theodelrieu can help with that. |
First, do not forget to make your methods Something like this will work: for (auto const& elem : map) {
auto const strKey = convertToString(elem.first);
j[strKey] = elem.second;
} Of course there needs to be a Same thing for the for (auto const& elem : json::iterator_wrapper(j)) {
auto const enumKey = enumFromString(elem.key());
map[enumKey] = elem.value();
} |
@RobotCaleb - Did @theodelrieu s hints work for you? |
I switched away from this to just being explicit with tinyxml2. I appreciate your prompt responses, though. |
I read that you can use maps as long as a
std::string
can be constructed from the key.I don't understand why this doesn't work. As I've written it I think it's fulfilling the constraint I mentioned above.
The text was updated successfully, but these errors were encountered: