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
Add example how to serialize C++ data to JSON #53
Conversation
@cdeil |
OK ... I'll have a look at the header and try to figure it out. But I would prefer to just learn by having a working example in the |
@cdeil |
I've attached a commit with I ran into two issue:
Can this be fixed?
Can this be implemented? |
Another thing that would be nice would be if picojson::value::object data;
data["this"] = "that"; // currently doesn't work instead of me having to manually wrap everything into picojson::value::object data;
data["this"] = picojson::value("that"); // currently doesn't work In I'm not sure if implementing the features I'm proposing here for Please let me know what is possible / reasonable and I can file a separate issue for each feature request. |
Thank you for the PR. I would look into it laterwards, but please let me first answer the issues raised.
The answer is no, and it is the outcome of the following reasons.
Even if there is a way to avoid the issue I would hesitate to implement such hacks, since it would IMO confuse the users since it contradicts from the way C++ is designed (as mentioned in the third point).
I am rather against the idea. First of all as a general guideline, it is not a good idea to provide support for a specific type of container in a library. The preferred way is to use the iterators for generalization so that the logic would not be bound to a certain type of container. So it might be more meaningful to provide a interface like
It is unlikely that such a feature would ever be added. Picojson by design requires users to write down explicit code for converting to and from JSON values. As the discussion regarding |
@kazuho Thanks for your thoughtful comments! I still don't understand what the problem would be if And I guess this generic way of handling containers is not possible in C++98? |
I consider handling
You are right. IIRC |
I've started to add some utility functions to my copy of template <typename T>
inline value vec_to_json(const std::vector<T> in) {
value::array out;
for (unsigned int i=0; i < in.size(); i++) {
out.push_back(value(in[i]));
}
return value(out);
}
inline value vec_to_json(const std::vector<int> in) {
std::vector<double> temp(in.begin(), in.end());
return vec_to_json(temp);
}
inline value num_to_json(double num) {
const double NA_VALUE = -999;
num = std::isnan(num) ? NA_VALUE : num;
return value(num);
} |
I don't need to parse JSON, but want to write some data I have as
std::string
,std:vector<float>
,int
, ... to JSON.Is it possible to create and fill a
picojson::value
object programatically?(I think this is not covered by the README and the examples ... sorry if I missed it.)