-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parsing.feature
More file actions
258 lines (250 loc) · 7.2 KB
/
json_parsing.feature
File metadata and controls
258 lines (250 loc) · 7.2 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
Feature: Parsing data from JSON
Scenario: Simple object data
Given an object model defined as:
"""
class SimpleObjectModel < SonJay::ObjectModel
properties do
property :id
property :name
property :published
property :featured
property :owner
end
end
"""
And JSON data defined as:
"""
json = <<-JSON
{
"id" : 55 ,
"name" : "Polygon" ,
"published" : true ,
"featured" : false ,
"owner" : null
}
JSON
"""
When the JSON is parsed to a model instance as:
"""
instance = SimpleObjectModel.parse_json( json )
"""
Then the instance attributes are as follows:
| id | name | published | featured | owner |
| 55 | "Polygon" | true | false | nil |
Scenario: Composite object data
Given an object model defined as:
"""
class ThingModel < SonJay::ObjectModel
properties do
property :name
property :details, :model => DetailModel
end
end
"""
And an object model defined as:
"""
class DetailModel < SonJay::ObjectModel
properties do
property :color
property :size
end
end
"""
And JSON data defined as:
"""
json = <<-JSON
{
"name" : "Cherry" ,
"details" :
{
"color" : "red" ,
"size" : "small"
}
}
JSON
"""
When the JSON is parsed to a model instance as:
"""
instance = ThingModel.parse_json( json )
"""
Then the instance attributes are as follows:
| name | details.color | details.size |
| "Cherry" | "red" | "small" |
Scenario: Object data with a value-array property
Given an object model defined as:
"""
class ContestantModel < SonJay::ObjectModel
properties do
property :name
property :scores, model: []
end
end
"""
And JSON data defined as:
"""
json = <<-JSON
{
"name" : "Pat" ,
"scores" : [ 9, 5, 7 ]
}
JSON
"""
When the JSON is parsed to a model instance as:
"""
instance = ContestantModel.parse_json( json )
"""
Then the instance attributes are as follows:
| name | scores[0] | scores[1] | scores[2] |
| "Pat" | 9 | 5 | 7 |
Scenario: Object data with a nested value-array property
Given an object model defined as:
"""
class TicTacToeModel < SonJay::ObjectModel
properties do
property :rows, model: [[]]
end
end
"""
And a model instance defined as:
"""
instance = TicTacToeModel.new
"""
And JSON data defined as:
"""
json = <<-JSON
{
"rows" : [
[ "X", "O", "X" ] ,
[ "O", "X", "X" ] ,
[ "X", "O", "O" ]
]
}
JSON
"""
When the JSON is parsed to a model instance as:
"""
instance = TicTacToeModel.parse_json( json )
"""
Then the instance attributes are as follows:
| rows[0][0] | rows[0][1] | rows[0][2] |
| 'X' | 'O' | 'X' |
| rows[1][0] | rows[1][1] | rows[1][2] |
| 'O' | 'X' | 'X' |
| rows[2][0] | rows[2][1] | rows[2][2] |
| 'X' | 'O' | 'O' |
Scenario: Object data with an object-array property
Given an object model defined as:
"""
class ListModel < SonJay::ObjectModel
properties do
property :name
property :items, :model => [ ItemModel ]
end
end
"""
And an object model defined as:
"""
class ItemModel < SonJay::ObjectModel
properties do
property :description
property :priority
end
end
"""
And JSON data defined as:
"""
json = <<-JSON
{
"name" : "Shopping" ,
"items" :
[
{
"description" : "Potato Chips" ,
"priority" : "Low"
} ,
{
"description" : "Ice Cream" ,
"priority" : "High"
}
]
}
JSON
"""
When the JSON is parsed to a model instance as:
"""
instance = ListModel.parse_json( json )
"""
Then the instance attributes are as follows:
| name | items[0].description | items[0].priority | items[1].description | items[1].priority |
| "Shopping" | "Potato Chips" | "Low" | "Ice Cream" | "High" |
Scenario: Object data with a nested object-array property
Given an object model defined as:
"""
class TableModel < SonJay::ObjectModel
properties do
property :rows, :model => [[ CellModel ]]
end
end
"""
And an object model defined as:
"""
class CellModel < SonJay::ObjectModel
properties do
property :is_head
property :value
end
end
"""
And JSON data defined as:
"""
json = <<-JSON
{
"rows" :
[
[ {"is_head": 1, "value": "Date"} , {"is_head": 1, "value": "Sightings"} ] ,
[ {"is_head": 1, "value": "Jan 1"} , {"is_head": 0, "value": 3} ] ,
[ {"is_head": 1, "value": "Jan 3"} , {"is_head": 0, "value": 2} ]
]
}
JSON
"""
When the JSON is parsed to a model instance as:
"""
instance = TableModel.parse_json( json )
"""
Then the instance attributes are as follows:
| rows[0][0].is_head | rows[0][0].value | rows[0][1].is_head | rows[0][1].value |
| 1 | "Date" | 1 | "Sightings" |
| rows[1][0].is_head | rows[1][0].value | rows[1][1].is_head | rows[1][1].value |
| 1 | "Jan 1" | 0 | 3 |
| rows[2][0].is_head | rows[2][0].value | rows[2][1].is_head | rows[2][1].value |
| 1 | "Jan 3" | 0 | 2 |
Scenario: Object data with extra properties
Given an object model defined as:
"""
class SimpleObjectModel < SonJay::ObjectModel
allow_extras
properties do
property :id
property :name
end
end
"""
And JSON data defined as:
"""
json = <<-JSON
{
"id" : 55 ,
"name" : "Polygon" ,
"published" : true ,
"featured" : false
}
JSON
"""
When the JSON is parsed to a model instance as:
"""
instance = SimpleObjectModel.parse_json( json )
"""
Then the instance attributes and indexed properties are as follows:
| id | name | ['id'] | ['name'] | ['published'] | ['featured'] |
| 55 | "Polygon" | 55 | "Polygon" | true | false |