1+ """
2+ The MIT License (MIT)
3+
4+ Copyright (c) 2016 Daniele Linguaglossa <[email protected] > 5+
6+ Permission is hereby granted, free of charge, to any person obtaining a copy
7+ of this software and associated documentation files (the "Software"), to deal
8+ in the Software without restriction, including without limitation the rights
9+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ copies of the Software, and to permit persons to whom the Software is
11+ furnished to do so, subject to the following conditions:
12+
13+ The above copyright notice and this permission notice shall be included in all
14+ copies or substantial portions of the Software.
15+
16+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
19+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ SOFTWARE.
23+ """
24+ from gramfuzz .fields import *
25+ import gramfuzz
26+
27+ def generate_json (path ):
28+ grammar = gramfuzz .GramFuzzer ()
29+ grammar .load_grammar (path )
30+ for x in grammar .gen (cat = "json" , num = 10 , max_recursion = 10 ):
31+ if x not in ["{}" , "[]" ]:
32+ j = json .loads (x )
33+ del grammar
34+ return j
35+ return {"dummy" : 1 }
36+
37+ TOP_CAT = "json"
38+
39+ class RDef (Def ): cat = "json-def"
40+
41+ class RRef (Ref ): cat = "json-def"
42+
43+ Def ("json" ,
44+ RRef ("json-object" ) | RRef ("json-array" ),
45+ cat = "json" )
46+
47+ RDef ("json-array" ,
48+ And ("[" ,Join (RRef ("value" ), max = 3 , sep = "," ), "]" ) |
49+ And ('[' ,Join (RRef ("empty" ), max = 1 ), "]" ) |
50+ And ('[' ,Join (RRef ("json-array" ), max = 3 , sep = "," ), "]" )|
51+ And ('[' ,Join (RRef ("json-object" ), max = 3 , sep = "," ), "]" )
52+ )
53+
54+ RDef ("key-value" ,
55+
56+ RRef ("key" ) & ":" & RRef ("value" )
57+
58+ )
59+
60+ RDef ("key-array" ,
61+
62+ RRef ("key" ) & ":" & RRef ("json-array" )
63+
64+ )
65+
66+ RDef ("key-object" ,
67+
68+ RRef ("key" ) & ":" & RRef ("json-object" )
69+
70+ )
71+
72+ RDef ("json-object" ,
73+ And ("{" , Join (RRef ("key-value" ), max = 3 , sep = "," ), "}" ) |
74+ And ("{" , Join (RRef ("key-array" ), max = 3 , sep = "," ), "}" ) |
75+ And ("{" , Join (RRef ("key-object" ), max = 3 , sep = "," ), "}" ) |
76+ And ("{" , RRef ("empty" ), "}" )
77+
78+ )
79+
80+ RDef ("key" ,
81+ Q (String (charset = String .charset_alphanum , min = 5 , max = 10 ))
82+ )
83+
84+ RDef ("sep" ,
85+ ":"
86+ )
87+
88+ RDef ("value" ,
89+ Int | Float | RRef ("boolean" ) | RRef ("key" ) | UInt | UFloat | RRef ("null" )
90+ )
91+
92+ RDef ("empty" ,
93+
94+ ""
95+ )
96+
97+ RDef ("null" ,
98+ "null"
99+ )
100+
101+ RDef ("boolean" ,
102+ Or ("true" , "false" )
103+ )
0 commit comments