-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Ignore nullptr values on constructing json object from a container #1422
Comments
Unfortunately, this is not possible at the moment (maybe @theodelrieu has an idea). In the meantime, you can employ the erase-remove idiom to remove j.erase(std::remove(j.begin(), j.end(), nullptr), j.end()); |
Another way to do this would be to provide an explicit
|
@FrancoisChabot very nice workaround indeed! |
@vassilisw Does this solve your issue? |
Yes it does, thanks (sorry for not closing). |
@nlohmann
you have any suggest ? thanks . nlohmann::json jObj = R"(
{
"callback": null,
"city": null,
"citylimit": null,
"datatype": null,
"key": "123123123",
"keywords": "123123123",
"location": "1.1.123",
"output": "JSON",
"sig": null,
"type": null
}
)"_json;
jObj.erase(std::remove(jObj.begin(), jObj.end(), nullptr), jObj.end());
std::cout << jObj.dump(2) << std::endl;
|
my last code nlohmann::json& clearNull(nlohmann::json& jObj) {
nlohmann::json jClone = jObj;
jObj.clear();
for (const auto& item : jClone.items()) {
if (item.value().is_object()) {
nlohmann::json jSubObj = clearNull(item.value());
if (!jSubObj.empty()) jObj[item.key()] = jSubObj;
} else if (!item.value().is_null()) {
jObj[item.key()] = item.value();
}
}
return jObj;
} |
In order to asses your issue, we need the following information:
|
nlohmann::json &clear_null(nlohmann::json &j)
{
nlohmann::json j_clone = j;
j.clear();
for (const auto& item : j_clone.items()) {
if (item.value().is_object()) {
nlohmann::json jSubObj = clear_null(item.value());
if (!jSubObj.empty()) j[item.key()] = jSubObj;
} else if (item.value().is_array()) {
nlohmann::json jSubArray = item.value();
jSubArray.erase(std::remove_if(jSubArray.begin(), jSubArray.end(), [](const nlohmann::json& el) {
return el.is_null();
}), jSubArray.end());
for (json::iterator it = jSubArray.begin(); it != jSubArray.end(); ++it) {
clear_null(*it);
}
if (!jSubArray.empty()) j[item.key()] = jSubArray;
} else if (!item.value().is_null()) {
j[item.key()] = item.value();
}
}
return j;
} if array is null need to erase. |
thanks, I use version 3.11.3 to use boost::optional, this is my code NLOHMANN_JSON_NAMESPACE_BEGIN
template <typename T>
void to_json(json& j, const boost::optional<T>& opt) {
if (opt) {
j = *opt;
} else {
std::cout << j.dump() << std::endl;
}
}
template <typename T>
void from_json(const json& j, boost::optional<T>& opt) {
if (j.is_null()) {
opt = boost::none;
} else {
opt = j.template get<T>();
}
}
NLOHMANN_JSON_NAMESPACE_END What I want to ask is that in the case of opt::none, will the value of null be filled in the default? {
"json": null
} |
That's an unnecessary copy. This just uses moves:
|
I use
void to_json(json& j, const Bar& b)
function. In some cases I would like this function to just assignnullptr
toj
. For example...Output...
Is there a way for
nullptr
objects to just get ignored when the json object is created?Such that the object to be...
I am aware of the
parser_callback_t
, anything similar for this?The text was updated successfully, but these errors were encountered: