Beispiel #1
0
 def test_arrays(self):
     arr = parse_string('[]')
     self.assertListEqual(arr, [])
     arr = parse_string('["People", "Places", "Things"]')
     self.assertListEqual(arr, ["People", "Places", "Things"])
     arr = parse_string('["Apples", "Bananas", ["Pears", "Limes"]]')
     self.assertListEqual(arr, ["Apples", "Bananas", ["Pears", "Limes"]])
Beispiel #2
0
 def test_arrays(self):
     arr = parse_string('[]')
     self.assertListEqual(arr, [])
     arr = parse_string('["People", "Places", "Things"]')
     self.assertListEqual(arr, ["People", "Places", "Things"])
     arr = parse_string('["Apples", "Bananas", ["Pears", "Limes"]]')
     self.assertListEqual(arr, ["Apples", "Bananas", ["Pears", "Limes"]])
     self.assertRaises(ValueError, parse_string, '["People", "Places", "Things"')
     self.assertRaises(ValueError, parse_string, '["People", "Places" "Things"]')
     self.assertRaises(ValueError, parse_string, '["People", "Places"] "Things"]')
Beispiel #3
0
    def test_objects(self):
        obj = parse_string('{"key1":"value1"}')
        self.assertDictEqual(obj, {"key1": "value1"})

        obj = parse_string("{}")
        self.assertDictEqual(obj, {})

        obj = parse_string('{"name": {"first":"Daniel", "last": "Yule"}}')
        self.assertDictEqual(obj,
                             {"name": {
                                 "first": "Daniel",
                                 "last": "Yule"
                             }})

        # Borrowed from http://en.wikipedia.org/wiki/JSON
        big_file = """{
          "firstName": "John",
          "lastName": "Smith",
          "isAlive": true,
          "age": 25,
          "height_cm": 167.6,
          "address": {
            "streetAddress": "21 2nd Street",
            "city": "New York",
            "state": "NY",
            "postalCode": "10021-3100"
          },
          "phoneNumbers": [
            {
              "type": "home",
              "number": "212 555-1234"
            },
            {
              "type": "office",
              "number": "646 555-4567"
            }
          ],
          "children": [],
          "spouse": null
        }"""
        obj = parse_string(big_file)
        self.assertDictEqual(
            obj, {
                "firstName":
                "John",
                "lastName":
                "Smith",
                "isAlive":
                True,
                "age":
                25,
                "height_cm":
                167.6,
                "address": {
                    "streetAddress": "21 2nd Street",
                    "city": "New York",
                    "state": "NY",
                    "postalCode": "10021-3100"
                },
                "phoneNumbers": [{
                    "type": "home",
                    "number": "212 555-1234"
                }, {
                    "type": "office",
                    "number": "646 555-4567"
                }],
                "children": [],
                "spouse":
                None
            })
Beispiel #4
0
import induce, helpers
import nayajson

with induce.grammar() as g:
    myfile = helpers.slurparg()
    with induce.Tracer(myfile, g):
        nayajson.parse_string(myfile)