Mercurial > lbo > hg > pcombinators
changeset 60:be810f4c68c0 draft
json_test: Improve whitespace handling
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Fri, 24 May 2019 01:11:33 +0200 |
parents | 25e52ca4cbac |
children | e3abe28c5a3a |
files | pcombinators/json_test.py |
diffstat | 1 files changed, 15 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/pcombinators/json_test.py Fri May 24 00:55:54 2019 +0200 +++ b/pcombinators/json_test.py Fri May 24 01:11:33 2019 +0200 @@ -11,7 +11,7 @@ JString = Last(Skip(String('"')) + NoneInSet('"') + Skip(String('"'))) -example_json = '{"id":1,"name":"Foo","price":123,"tags":["Bar","Eek"],"stock":{"warehouse":300, "retail":20}}' +example_json = '{"id":1,"name":"Foo","price":123,"tags":["Bar","Eek"],"stock":{"warehouse":300,"retail":20}}' class Value(Parser): """Bare-bones, but fully functioning, JSON parser. Doesn't like escaped quotes. @@ -23,7 +23,7 @@ 'price': 123.0, 'tags': ['Bar', 'Eek'], 'stock': {'warehouse': 300.0, 'retail': 20.0}}, - ParseState({"id":1,"name":"Foo","price":123,"tags":["Bar","Eek"],"stock":{"warehouse":300, "retail":20}}<>)) + ParseState({"id":1,"name":"Foo","price":123,"tags":["Bar","Eek"],"stock":{"warehouse":300,"retail":20}}<>)) """ def parse(self, st): return (Dict | List | JString | Float()).parse(st) @@ -66,5 +66,17 @@ # Convert the list of tuples into a dict. Dict = dct >> dict +def remove_unused_whitespace(s): + acc = [] + lvl = 0 + ws = set(' \n\t\r') + for c in s: + if c == '"': + lvl += 1 if lvl == 0 else -1 + if lvl == 0 and c in ws: + continue + acc.append(c) + return ''.join(acc) + def parse_json(json): - return Dict.parse(ParseState(json.replace(' ', ''))) \ No newline at end of file + return Value().parse(ParseState(remove_unused_whitespace(json))) \ No newline at end of file