Skip to content

Binary string causes numbers to be dumped as hex #101

Closed

Description

@minus7

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)

Activity

  1. self-assigned this
    on Jul 1, 2015
  2. nlohmann commented on Jul 1, 2015

    @nlohmann
    Owner

    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.

  3. nlohmann commented on Jul 1, 2015

    @nlohmann
    Owner

    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

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions