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

multiline text values #255

Closed
ins2718 opened this issue May 29, 2016 · 4 comments
Closed

multiline text values #255

ins2718 opened this issue May 29, 2016 · 4 comments
Labels
solution: invalid the issue is not related to the library

Comments

@ins2718
Copy link

ins2718 commented May 29, 2016

std::string s = "{\"text\":\"line 1\nline 2\"}";
auto json = nlohmann::json::parse(s);

This json-object causes an error. Standard facilities python, php and js correctly handle it.

@nlohmann
Copy link
Owner

Newline characters must be escaped, see RFC 7159, Section 7:

All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

As \n is U+000A, it must be escaped. In your example, std::string s = "{\"text\":\"line 1\\nline 2\"}"; would be correct.

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label May 29, 2016
@nlohmann
Copy link
Owner

(Side remark: Python fails to parse the string:

Python 2.7.11 (default, Mar  6 2016, 18:11:07) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.21)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "{\"text\":\"line 1\nline 2\"}"
>>> s
'{"text":"line 1\nline 2"}'
>>> print s
{"text":"line 1
line 2"}
>>> import json
>>> json.loads(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/homebrew/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/local/homebrew/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/homebrew/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 1 column 16 (char 15)

@ins2718
Copy link
Author

ins2718 commented May 29, 2016

Thank you! Standard php-function "json_encode" not escaping new lines, so I thought it was the correct json-object. In python 3.5.1 is no exception.

@nlohmann
Copy link
Owner

For Python 3.5.1, I get the same error:

Python 3.5.1 (default, Dec  8 2015, 17:46:37) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "{\"text\":\"line 1\nline 2\"}"
>>> s
'{"text":"line 1\nline 2"}'
>>> print(s)
{"text":"line 1
line 2"}
>>> import json
>>> json.loads(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/homebrew/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/local/homebrew/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/homebrew/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 1 column 16 (char 15)

But you are right for PHP's json_encode - strange that this function is not compliant to the JSON spec.

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