Home Download Buy Blog Forum Support

How-to: Support single quotes on JSON syntax

How-to: Support single quotes on JSON syntax

Postby ZeeStorm on Wed Nov 21, 2012 9:30 pm

If you're searching for this, you know how frustrating it is: Sublime doesn't recognize valid JSON with single quotes. I looked, didn't find anything.. and just decided to make a fix myself (and it was really simple).

Go to your Packages folder in the main Sublime folder.
You'll see a "JavaScript" folder. Open that.
Scroll to JSON.tmLanguage, and open that (in sublime is fine).

Do a search for <key>string</key>, it should be around line 217.
Copy from this line:
<key>string</key>
<dict>
....
</dict>
(ending line should be around 264)

Then paste it immediately afterwards.
Change the <key> to string2.

Inside of string2's <dict> you'll see <key>begin</key> with a <string>"</string> following it. Change that double quote " to single quote '.
Do the same on <key>end</key>.
Then look down until you see <key>patterns</key>, you'll find a <key>match</key> with some fancy regex.
One of those lines is ["\\ ... (etc.), change that double quote to single quote.

Hard part is done.
Do a search for #string and duplicate the 2 <dict>'s you find and change the duplicate #string to #string2

Done! Enjoy support for both double and single quotes in json :)

P.S. You can name the <dict> for string2's <name> from string.quoted.double.json to something like string.quoted.single.json, but you will need to then add support in your theme files to color it. It seems to work fine without changing it though (I didn't change it). :)
ZeeStorm
 
Posts: 1
Joined: Wed Mar 28, 2012 8:15 pm

Re: How-to: Support single quotes on JSON syntax

Postby facelessuser on Wed Nov 21, 2012 10:09 pm

Sorry to break it to you, but the JSON syntax only allows double quoted strings.

Javascript and JSON are not the same thing. If you happen to have a JSON parser that allows single quotes, it does not mean that JSON allows single quotes, just that the parser is not coded to specifications.
facelessuser
 
Posts: 1565
Joined: Tue Apr 05, 2011 7:38 pm


Return to General Discussion

Who is online

Users browsing this forum: Exabot [Bot] and 15 guests