Search code examples
phpjsonphp-7

New lines and tabs in json_decode() (PHP 7)


My code using json_decode() worked correctly with PHP 5.6. After migration to PHP 7.0, json_decode() returns NULL and json_last_error() tells me that my error is:

Control character error, possibly incorrectly encoded

After debugging, I found out that my problem are both tabs and new line characters in string values. If I remove them both, it works. If I leave either new lines or tabs, the error occurs.

Is json_decode() behavior changed in PHP 7? I would like to keep tabs and new lines in my .json files for better readability. The code works if I replace tabs to \t and new lines to \n.

How can I keep new lines and tabs?


Solution

  • Due to a software licensing issue, the old json module was replaced with the jsond module. You can see the discussion of this change and the attached pull request here. Now, there's not much information about the changes or about workarounds for things, but I can see that all control characters inside strings ([\0x00-\0x1F]) trigger an error. Unfortunately for you, it seems that this behavior is correct per the JSON Standard:

    Insignificant whitespace is allowed before or after any token. The whitespace characters are: character tabulation (U+0009), line feed (U+000A), carriage return (U+000D), and space (U+0020). Whitespace is not allowed within any token, except that space is allowed in strings.

    So, in other words, literal tabs are not allowed inside JSON strings at all; they must be \t or \u0009. So, the JSON you're consuming is in direct violation of the standard. Ideally, you should get your JSON source to return standards-compliant JSON. If that won't work, you'll have to pre-process the JSON and convert tabs inside strings to \t.