Skip to content
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

dump() / parse() not idempotent #76

Closed
benwehrle opened this issue May 26, 2015 · 3 comments
Closed

dump() / parse() not idempotent #76

benwehrle opened this issue May 26, 2015 · 3 comments
Assignees
Labels
solution: invalid the issue is not related to the library

Comments

@benwehrle
Copy link

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?

@nlohmann nlohmann self-assigned this May 30, 2015
@nlohmann
Copy link
Owner

I cannot reproduce the error. I added the following lines to the unit tests:

// create JSON object
json fields;
fields["one"] = std::string("one");
fields["two"] = std::string("two three");
fields["three"] = std::string("three \"four\"");

// create another JSON object by deserializing the serialization
std::string payload = fields.dump();
json parsed_fields = json::parse(payload);

// check individual fields to match both objects
CHECK(parsed_fields["one"] == fields["one"]);
CHECK(parsed_fields["two"] == fields["two"]);
CHECK(parsed_fields["three"] == fields["three"]);

// check individual fields to match original input
CHECK(parsed_fields["one"] == std::string("one"));
CHECK(parsed_fields["two"] == std::string("two three"));
CHECK(parsed_fields["three"] == std::string("three \"four\""));

// check equality of the objects
CHECK(parsed_fields == fields);

// check equality of the serialized objects
CHECK(fields.dump() == parsed_fields.dump());

// check everything in one line
CHECK(fields == json::parse(fields.dump()));

Am I missing something?

nlohmann added a commit that referenced this issue May 30, 2015
@nlohmann nlohmann added solution: invalid the issue is not related to the library and removed kind: bug labels May 30, 2015
@benwehrle
Copy link
Author

Sorry for the mistake. My local repo was out of date; the current version of the code does not have this problem.

@nlohmann
Copy link
Owner

No worries!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants