Skip to content

Instantly share code, notes, and snippets.

@mkmkme
Last active December 15, 2016 13:39
Show Gist options
  • Save mkmkme/9ac04f63156a2a2a34f4b7aa750819e2 to your computer and use it in GitHub Desktop.
Save mkmkme/9ac04f63156a2a2a34f4b7aa750819e2 to your computer and use it in GitHub Desktop.
little helper for comments in JSON
#!/usr/bin/env python3
import json
import sys
def uncomment_line(line):
# print('uncommenting', line)
i = line.find('//')
if i == -1:
return line
rq = line.find('"', i)
if rq != -1 and line.rfind('"', 0, i) != -1:
return line[:rq+1] + uncomment_line(line[rq+1:])
return line[:i]
def uncomment_line_wo_recursion(line):
ret = ''
while line:
i = line.find('//')
if i == -1:
ret += line
break
rq = line.find('"', i)
if rq != -1 and line.rfind('"', 0, i) != -1:
ret += line[:rq+1]
line = line[rq+1:]
else:
ret += line[:i]
break
return ret
def uncomment(s):
r = ''
for l in s.split('\n'):
r += uncomment_line_wo_recursion(l)
return r
if __name__ == '__main__':
print(json.loads(uncomment_line_wo_recursion('{"foo": [' + ','.join(['"a//a"'] * 10000) + ']}')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment