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

Binary string causes numbers to be dumped as hex #101

Closed
minus7 opened this issue Jul 1, 2015 · 2 comments
Closed

Binary string causes numbers to be dumped as hex #101

minus7 opened this issue Jul 1, 2015 · 2 comments
Assignees

Comments

@minus7
Copy link

minus7 commented Jul 1, 2015

The following code dumps the value of the number field in hexadecimal format (without leading 0x, but that's not legal json anyway afaik) when there's also a string containing a symbol between \x00 and \x1f.

#include <iostream>
#include "json.hpp"

using namespace std;
using json = nlohmann::json;

int main()
{
    int64_t number = 10;
    cout << "number: " << number << endl;
    string bytes{"\x00" "asdf\n", 6};
    cout << "bytes: " << bytes << endl;
    //json j{{"int64", number}, {"binary string", bytes}};
    json j;
    j["int64"] = number;
    j["binary string"] = bytes;
    cout << j.dump() << endl;
    return 0;
}

The output looks like this:

number: 10
bytes: asdf

{"binary string":"\u0000asdf\n","int64":a}

Using the latest revision (540c589)

@nlohmann nlohmann self-assigned this Jul 1, 2015
@nlohmann
Copy link
Owner

nlohmann commented Jul 1, 2015

I can reproduce the error. I have no idea yet why the dump() function decides to print the number as hex, but the library seems to be confused by the control character inside the string. I'll continue checking.

@nlohmann
Copy link
Owner

nlohmann commented Jul 1, 2015

I found the reason for the problem: The control characters need to be escaped as \uxxxx. In that part of the code, an internal string stream is configured to print the character as hexadecimal number. This needs to be reset, because otherwise all subsequent numbers would be printed as hexadecimal (a instead of 10).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants