My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python2.4

"""Test harness for json_validator.py

Copyright 2009 Google Inc.
http://code.google.com/p/google-mobwrite/

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

__author__ = 'fraser@google.com (Neil Fraser)'

import unittest
import json_validator
# Force a module reload to make debugging easier (at least in PythonWin).
reload(json_validator)

class JsonValidatorTest(unittest.TestCase):

def assertValid(self, json):
self.assertTrue(json_validator.is_valid(json))

def assertInvalid(self, json):
self.assertFalse(json_validator.is_valid(json))

def testJsonValid(self):
# Valid expressions.
self.assertValid('["\\u1abc"]')
self.assertValid('[""]')
self.assertValid('["\\""]')
self.assertValid('[123]')
self.assertValid('[]')
self.assertValid('[0]')
self.assertValid('[0e0]')
self.assertValid('[0, -1, 1.2, -3.4, 5e+6, 7.8E-90]')
self.assertValid('[true, false, null]')
self.assertValid('{}')
self.assertValid('{"foo":"bar"}')
self.assertValid('{"1":"one", "2":["deux", "zwei"], "3":null}')

def testJsonInvalid(self):
# Invalid expressions.
self.assertInvalid('')
self.assertInvalid(' ')
self.assertInvalid('1')
self.assertInvalid('1.2')
self.assertInvalid('"Hi"')
self.assertInvalid('true')
self.assertInvalid('[,,,]')
self.assertInvalid('{[]}')
self.assertInvalid('{"1", "2"}')
self.assertInvalid('{"zero"}')
self.assertInvalid('{1:"one"}')
self.assertInvalid('{null:[]}')
self.assertInvalid('{true:"true"}')
self.assertInvalid('{[false]:"false"}')
self.assertInvalid('{{}:"object"}')
self.assertInvalid('["]')
self.assertInvalid('["\\x"]')
self.assertInvalid('["\\u1ab"]')
self.assertInvalid('[{]}')
self.assertInvalid('[1:2]')
self.assertInvalid('[1, 2')
self.assertInvalid('1, 2')
self.assertInvalid('[007]')
self.assertInvalid('[.1]')
self.assertInvalid('[document.cookies]')
self.assertInvalid('[alert()]')
self.assertInvalid('[1+1]')
self.assertInvalid('[1;2]')

def testJsonMultiLine(self):
# The JSON example in Wikipedia.
self.assertValid("""
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
],
"newSubscription": false,
"companyName": null
}
""")

if __name__ == "__main__":
unittest.main()

Change log

r96 by neil.fraser on Dec 8, 2009   Diff
Adding a JSON validator (for use in future
status messages).
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 3105 bytes, 105 lines

File properties

svn:executable
*
Powered by Google Project Hosting