Sublime Forum

JSON written by ST3 can contain literal tab character

#1

If you use the find bar to search for a TAB character ('\t') and then close ST to save the session, the resulting JSON is invalid. Parsing Session.sublime_session using JSON.parse from Node.js 4.x.x fails. This failed because there was an unescaped tab character in a string literal.

From the JSON specification:

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.

Whatever ST uses to write JSON files seems to be able to produce non-spec compliant JSON.

Why am I concerned? I developed an npm package that is able to clean workspaces that no longer exist from the Sublime.sublime_session file. It no longer works if the JSON can’t be parsed.

0 Likes

#2

I’ll change the encoder to escape control characters out of principle, but keep in mind that Sublime Text doesn’t use actual JSON anywhere, a looser variant is used that allows for comments and trailing characters (and unescaped control characters apparently).

It’s unlikely the session files will ever be written with trailing commas or comments, but it’s not impossible. If you want to parse these files according to Sublime Text’s own rules, you can use sublime.decode_value()

4 Likes

#3

I totally forgot about that. Thanks for your reply, looking forward to the update.

0 Likes