You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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)
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.
Activity
nlohmann commentedon May 29, 2016
Newline characters must be escaped, see RFC 7159, Section 7:
As
\nis U+000A, it must be escaped. In your example,std::string s = "{\"text\":\"line 1\\nline 2\"}";would be correct.nlohmann commentedon May 29, 2016
(Side remark: Python fails to parse the string:
ins2718 commentedon 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 commentedon May 29, 2016
For Python 3.5.1, I get the same error:
But you are right for PHP's
json_encode- strange that this function is not compliant to the JSON spec.