Json: Ignore nullptr values on constructing json object from a container

Created on 10 Jan 2019  路  5Comments  路  Source: nlohmann/json

I use void to_json(json& j, const Bar& b) function. In some cases I would like this function to just assign nullptr to j. For example...

void to_json(nlohmann::json& j, const Bar& b) {
    if (b.value == __FLT_MAX__) {
        j = nullptr;
        return;
    }
    j["name"] = b.name;
    j["value"] = b.value;
}
...

    std::vector<Bar> bars;
    bars.emplace_back("b1", 1.234);
    bars.emplace_back("b2", __FLT_MAX__);
    bars.emplace_back("b3", __FLT_MAX__);

    nlohmann::json j(bars);
    std::cout << j.dump(4) << std::endl;

Output...

[
    {
        "name": "b1",
        "value": 1.232
    },
    null,
    null
]

Is there a way for nullptr objects to just get ignored when the json object is created?
Such that the object to be...

[
    {
        "name": "b1",
        "value": 1.232
    }
]

I am aware of the parser_callback_t, anything similar for this?

question proposed fix

Most helpful comment

Another way to do this would be to provide an explicit to_json() for std::vector<Bar>, and apply the filter there:

void to_json(nlohmann::json& j, const Bar& b) {
    j["name"] = b.name;
    j["value"] = b.value;
}

void to_json(nlohmann::json& j, const std::vector<Bar>& b) {
  for(const auto& i : b) {
    if(i.value == __FLT_MAX__) {continue;}
    j.push_back(i);
  }
}

All 5 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 null values, just like it is possible for std::vector:

j.erase(std::remove(j.begin(), j.end(), nullptr), j.end());

Another way to do this would be to provide an explicit to_json() for std::vector<Bar>, and apply the filter there:

void to_json(nlohmann::json& j, const Bar& b) {
    j["name"] = b.name;
    j["value"] = b.value;
}

void to_json(nlohmann::json& j, const std::vector<Bar>& b) {
  for(const auto& i : b) {
    if(i.value == __FLT_MAX__) {continue;}
    j.push_back(i);
  }
}

@FrancoisChabot very nice workaround indeed!

@vassilisw Does this solve your issue?

Yes it does, thanks (sorry for not closing).

Was this page helpful?
0 / 5 - 0 ratings