unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Hack for JSON sequences with trailing commas?
@ 2018-07-31 16:58 Skip Montanaro
  0 siblings, 0 replies; 8+ messages in thread
From: Skip Montanaro @ 2018-07-31 16:58 UTC (permalink / raw)
  To: Help GNU Emacs

I've been using programming languages where sequences can contain
trailing commas since my earliest days with C back in the early 80s.
Of course, Python (the language in which I write most things these
days) supports that. As does JavaScript, though for some crazy reason,
not if that JavaScript is just JSON. This is a valid JSON input
string:

"[1,2,3]"

but attempts to decode this string using Python's json.loads()
function provoke a JSONDecodeError:

"[1,2,3,]"

*sigh*

I suppose if I was to dig enough I'd find a reason JSON's author
thought that was a good idea.

I'm using JSON as a config file format for some code (not as an
interchange format, so I could care less what Chrome or IE think about
it), so I'm actually writing it by hand. Emacs tells me "*.json" files
are JavaScript, so it's quite happy with trailing commas.I installed
json-mode, but it's also happy with trailing commas.

Is there some clever hack to coax Emacs into stripping those trailing
commas I love so much? Maybe a JSON write-file hook to strip them and
a read-file-hook to add them back?

For the moment, for sequences which extend across multiple lines, I've
sort of adopted preceding the second through n-th elements with commas
instead of appending commas to the first through n-1st elements.

Thanks,

Skip, missing his commas in Illinois...



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2018-08-04 23:12 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.4482.1533056121.1292.help-gnu-emacs@gnu.org>
2018-08-01  6:53 ` Hack for JSON sequences with trailing commas? Barry Margolin
2018-08-01  7:22   ` Yuri Khan
2018-08-01  9:58     ` Skip Montanaro
2018-08-01 14:40   ` Drew Adams
     [not found]   ` <mailman.4559.1533134429.1292.help-gnu-emacs@gnu.org>
2018-08-02 22:42     ` Barry Margolin
     [not found]       ` <mailman.4627.1533257510.1292.help-gnu-emacs@gnu.org>
2018-08-04 23:12         ` Barry Margolin
     [not found]     ` <<barmar-81F7D5.18423702082018@reader.eternal-september.org>
2018-08-03  0:51       ` Drew Adams
2018-07-31 16:58 Skip Montanaro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).