Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chars get treated as JSON numbers not JSON strings #1139

Closed
zoe-mcelderry opened this issue Jun 20, 2018 · 5 comments
Closed

chars get treated as JSON numbers not JSON strings #1139

zoe-mcelderry opened this issue Jun 20, 2018 · 5 comments
Labels
solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)

Comments

@zoe-mcelderry
Copy link

  • What is the issue you have?

I am creating some JSON from some C++ datatypes, one of which is the char datatype. I would expect this to be treated as a JSON string, but when I call the dump method it outputs the value as if it were a JSON number.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

nlohmann::json json;
char sample = 'a';
json["char"] = sample;
std::cout << json.dump ();

  • What is the expected behavior?

Expect the output to show : {"char":"a"}

  • And what is the actual behavior instead?

The output I get is : {"char":97}

OS is Debian 9.3, compiler is g++ 6.3.0

  • Did you use a released version of the library or the version from the develop branch?

Tag v3.1.2

@nlohmann nlohmann added documentation solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) labels Jun 20, 2018
@nlohmann
Copy link
Owner

This is known behavior (can’t find the respective issue right now) which can’t be fixed. It should be better explained in the documentation though.

@zoe-mcelderry
Copy link
Author

Ok, thanks anyway. Apologies for raising a duplicate.

@OvermindDL1
Copy link

For note, your expected output of "a" is not a character, but rather it's string (null-terminated once parsed), and what should happen if the input 'string' was multi character for example? An integer is the only logical representation of a character in JSON since JSON doesn't have a specific character type.

Plus a char is just a 1-byte integer, hard to distinguish that from, say, an actual 1-byte integer. ^.^

@nlohmann
Copy link
Owner

Related: #1052 #276

@nlohmann
Copy link
Owner

@zoe-mcelderry No worries. I shall leave #1052 open as we should mention this in the documentation.

@nlohmann nlohmann added solution: duplicate the issue is a duplicate; refer to the linked issue instead and removed documentation labels Jun 20, 2018
nlohmann added a commit that referenced this issue Jun 22, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)
Projects
None yet
Development

No branches or pull requests

3 participants