Skip to content

Instantly share code, notes, and snippets.

@mbarkhau
Last active March 29, 2016 16:34
Show Gist options
  • Save mbarkhau/9d1df7cf79a3bdf120cc to your computer and use it in GitHub Desktop.
Save mbarkhau/9d1df7cf79a3bdf120cc to your computer and use it in GitHub Desktop.
Parses JSON allowing for trailing commas and comments using //
import re
import json
ONELINE_COMMENT_RE = re.compile(r"""
^ # comment must be at the start of the line
\s* # arbitrary whitespace
// # start of the comment
(.*) # the comment
$ # until the end of line
""", re.MULTILINE | re.VERBOSE)
INLINE_COMMENT_RE = re.compile(r"""
([\,\"\[\]\{\}\d]) # anythig that might end a expression
\s+ # comment must be preceded by whitespace
// # start of the comment
\s # and succeded by whitespace
(?:[^\"\]\}\{\[]*) # the comment (except things which might be json)
$ # until the end of line
""", re.MULTILINE | re.VERBOSE)
TRAILING_COMMA_RE = re.compile(r"""
, # the comma
(?:\s*) # arbitrary whitespace
$ # only works if the trailing comma is followed by newline
(\s*) # arbitrary whitespace
([\]\}]) # end of an array or object
""", re.MULTILINE | re.VERBOSE)
def parse_json_cfg(raw_data):
"""Parses JSON allowing for trailing commas and comments using //
Note: Comments must be preceded by whitespace or newline.
"""
if isinstance(raw_data, bytes):
raw_data = raw_data.decode('utf-8')
raw_data = ONELINE_COMMENT_RE.sub(r"", raw_data)
raw_data = INLINE_COMMENT_RE.sub(r"\1", raw_data)
raw_data = TRAILING_COMMA_RE.sub(r"\1\2", raw_data)
return json.loads(raw_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment