Skip to content

dump() / parse() not idempotent #76

@benwehrle

Description

@benwehrle

If you construct a string of the JSON payload using dump() and then use parse() to deserialize it, the contents of the former object don't match the latter when there are escape characters. Example pseudo-code

void test_case()
{
    nlohmann::json fields;
    fields["one"] = std::string("one");
    fields["two"] = std::string("two three");
    fields["three"] = std::string("three \"four\"");
    std::string payload = fields.dump();

    nlohmann::json parsed_fields = fields_t::parse(payload);
    test_check_eq(parsed_fields.size(), fields.size());
    test_check_eq(parsed_fields["one"], fields["one"]);
    test_check_eq(parsed_fields["two"], fields["two"]);
    test_check_eq(parsed_fields["three"], fields["three"]);
}

Here parsed_fields["three"] == std::string("three \\\"four\\\""). If this is expected, then what should be done to parsed_fields in this case to recover the original contents?

Metadata

Metadata

Assignees

Labels

solution: invalidthe issue is not related to the library

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions