Mercurial > lbo > hg > pcombinators
changeset 34:02cf2ee329a3 draft
Fix JSON parsing - recent changes in the list handling needed to be accommodated
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Wed, 22 May 2019 01:08:08 +0200 |
parents | 26f8bc28ebfa |
children | 6e7d8f8dfba2 |
files | pcombinators/json_test.py |
diffstat | 1 files changed, 10 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/pcombinators/json_test.py Wed May 22 01:00:50 2019 +0200 +++ b/pcombinators/json_test.py Wed May 22 01:08:08 2019 +0200 @@ -13,6 +13,8 @@ wrapl = lambda l: [l] +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. @@ -35,14 +37,17 @@ # An entry is any value. entry = Value() -# A mid entry is a value followed by a comma. -midentry = entry + Skip(String(',')) +# A mid entry is a value followed by a comma. Last ensures that the result +# is an entry, not a list of one entry. +midentry = Last(entry + Skip(String(','))) # A list is a [, followed by mid entries, followed by a final entry, and a # closing ]. The list is wrapped in a list to prevent merging in other parsers. -List = (Skip(String('[')) + +# Flatten() takes care that the list from Repeat() and the single entry are made +# into one list. +List = Flatten(Skip(String('[')) + Repeat(midentry, -1) + entry + - Skip(String(']'))) >> wrapl # Wrap list inside another list to protect it from flattening. + Skip(String(']'))) # DICTS @@ -50,7 +55,7 @@ separator = Skip((Whitespace() + String(":") + Whitespace())) # Entry is a String followed by a separator and a value. Wrap the value in a list to prevent merging. # The two-element list is converted to a tuple. -entry = (JString + separator + (Value() >> wrapl)) >> (lambda l: tuple(l)) +entry = JString + separator + (Value()) >> (lambda l: tuple(l)) # A mid entry is followed by a comma. midentry = Last(entry + Skip(String(',') + Skip(Whitespace()))) # A dict is a {, followed by entries, followed by a final entry, followed by a closing }