Replies: 2 comments
-
Here is an example: #include <iostream>
#include "json.hpp"
using json = nlohmann::json;
struct foo {
explicit foo(int i)
: m_i(i)
{}
bool operator<(const foo& other) const
{
return m_i < other.m_i;
}
/*
operator std::string() const
{
return std::to_string(m_i);
}
*/
int m_i = 0;
};
void to_json(json& j, const foo& f)
{
j["i"] = f.m_i;
}
int main()
{
std::map<foo, std::string> m;
m[foo(1)] = "one";
m[foo(2)] = "two";
std::cout << json(m) << std::endl;
} When [[{"i":1},"one"],[{"i":2},"two"]] If that operator is present, the result is an object: {"1":"one","2":"two"} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Do you need further assistance with this issue? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to dump and parse an
std::map<unsigned int, std::shared_ptr<CustomObject>>
. As you see,CollectionEntry
is just anunsigned int
.On the Readme it describes that the key for the map must be convertible to string, but how? Which function I need to implement?
I'm already using this: #2377 to automatically convert shared_ptr's, but I dont know what to do in this case where I have map with shared_ptrs
I'm trying something like
Beta Was this translation helpful? Give feedback.
All reactions