Skip to content

multiline text values #255

Closed
Closed

Description

@ins2718
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.

Activity

  1. nlohmann commented on May 29, 2016

    @nlohmann
    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.

  2. nlohmann commented on May 29, 2016

    @nlohmann
    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)
    
  3. ins2718 commented on May 29, 2016

    @ins2718
    Author

    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.

  4. nlohmann commented on May 29, 2016

    @nlohmann
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: invalidthe issue is not related to the library

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions