Source

copytrans / utils.py

Full commit
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json, re

#from pyparsing import *
#def setup_eval_parser():
#    TRUE = Keyword("true").setParseAction(replaceWith(True))
#    FALSE = Keyword("false").setParseAction(replaceWith(False))
#    NULL = Keyword("null").setParseAction(replaceWith(None))
#
#    jsonString = dblQuotedString.setParseAction(removeQuotes)
#    jsonNumber = Combine(
#        Optional('-') + ('0' | Word('123456789', nums)) +
#        Optional('.' + Word(nums)) +
#        Optional(Word('eE', exact=1) + Word(nums + '+-', nums)))
#
#    jsonObject = Forward()
#    jsonValue = Forward()
#    # black magic begins
#    commaToNull = Word(',,', exact=1).setParseAction(replaceWith(None))
#    jsonElements = ZeroOrMore(commaToNull) + Optional(jsonValue) + ZeroOrMore((Suppress(',') + jsonValue) | commaToNull)
#    # black magic ends
#    jsonArray = Group(Suppress('[') + Optional(jsonElements) + Suppress(']'))
#    jsonValue << (jsonString | jsonNumber | Group(jsonObject) | jsonArray | TRUE | FALSE | NULL)
#    memberDef = Group(jsonString + Suppress(':') + jsonValue)
#    jsonMembers = delimitedList(memberDef)
#    jsonObject << Dict(Suppress('{') + Optional(jsonMembers) + Suppress('}'))
#
#    jsonComment = cppStyleComment
#    jsonObject.ignore(jsonComment)
#
#    def convertNumbers(s, l, toks):
#        n = toks[0]
#        try:
#            return int(n)
#        except ValueError:
#            return float(n)
#
#    jsonNumber.setParseAction(convertNumbers)
#
#    return jsonValue

def parse_eval(s):
    return json.loads(re.sub(r'(?<=,)\s*,', ' null,', s))

def test():
    tests = (
        '{"foo":["nar",1,2,,3]}',
        '[1,2]',
        '[1,,2]',
    )
    for test in tests:
        res = parse_eval(test)
        print(res)

if __name__ == '__main__':
    test()