-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcJsonPath.pkg
More file actions
263 lines (223 loc) · 8.19 KB
/
cJsonPath.pkg
File metadata and controls
263 lines (223 loc) · 8.19 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
259
260
261
262
263
//==============================================================================
// cJsonPath.pkg
//
// A class which will allow you to get either object handles or values from JSON
// objects using their path.
//
// Author: Mike Peat
// Date: 06/02/2020
//
// Usage:
// To return a JSON object at a path:
// Get JsonAtPath of oJsonPathObject hoSourceJsonObject, path to hoVar
// or:
// Move (JsonAtPath(oJsonPathObject, hoSourceJsonObject, path)) to hoVar
//
// To return a value at a path:
// Get ValueAtPath of oJsonPathObject hoSourceJsonObject path to sVar
// or
// Move (ValueAtPath(oJsonPathObject, hoSourceJson, path)) to sVar
//
// Path notation:
//
// A string with JSON object names, separated by dots "." and array indices
// in square brackets: "foo.bar.baz[0].bill[1][0]"
//
// Sample JSON:
//
// {
// "foo": {
// "bar": {
// "baz": [
// 66.123,
// {
// "jim": "jack"
// },
// false,
// {
// "bob": 42
// },
// {
// "kim": "possible"
// },
// [55, 1, 19, {
// "Mork": [
// [35, 67, 88, 100, [21, 33, 45, "Tim"]]
// ]
// }]
// ]
// }
// }
// }
//
// Examples:
// Move (JsonAtPath(oJPath, hoJson, "foo.bar.baz[5][3].Mork[0][4][3]")) to hoObj
//
// Get ValueAtPath of oJPath hJson "foo.bar.baz[5][3].Mork[0][4][3]" to sVal
// (sVal = "Tim")
//
// In the first example the JSON object would be returned; if you then performed
// Move (JsonValue(hoObj)) to sVal you would get the same result as the second
// example - i.e. "Tim".
//
// NOTE: JSON is case-sensitive, so your path-strings must exactly match the
// object names ("foo" is *not* "Foo") in the JSON file.
//
// Licence (MIT):
//
// Copyright 2020, Mike Peat, Unicorn InterGlobal Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//==============================================================================
// Added CountAtPath function to return the count of elements in an array at the
// passed path.
//
// 2024-05-08 - MJP - Added test to see if we were passed a JSON array rather
// than an object and if so call ArrayHandle rather than
// ObjectHandle, so should now cope with arrays as the top
// level "object".
//==============================================================================
//
Use UI
Use cJsonObject.pkg
Class cJsonPath is a cObject
Function ArrayHandle Handle hoJson String sPath Returns Handle
Integer iLSBPos iRSBPos iIndex
Handle hoMemb hoObj
If not hoJson ;
Function_Return 0
If (sPath = "") Begin
Send Destroy of hoJson
Function_Return 0
End
Move (Pos("[", sPath)) to iLSBPos
Move (Pos("]", sPath)) to iRSBPos
If (not(iLSBPos) or ;
not(iRSBPos) or ;
(iLSBPos > iRSBPos) or ;
(iLSBPos <> 1)) Begin
Send Destroy of hoJson
Function_Return 0
End
Move (Mid(sPath, (iRSBPos - iLSBPos - 1), (iLSBPos + 1))) to iIndex
If (MemberCount(hoJson) <= iIndex) Begin
Send Destroy of hoJson
Function_Return 0
End
Get MemberByIndex of hoJson iIndex to hoMemb
If (Length(Trim(sPath)) = iRSBPos) Begin // It ends here
Send Destroy of hoJson
Function_Return hoMemb
End
Move (Right(sPath, (Length(sPath) - iRSBPos))) to sPath
If (Left(sPath, 1) = ".") ;
Move (Replace(".", sPath, "")) to sPath
Get ObjectHandle hoMemb sPath to hoObj
Send Destroy of hoJson
Function_Return hoObj
End_Function
Function ObjectHandle Handle hoJson String sPath Returns Handle
Integer iLSBPos iDotPos iPos
String sMemb
Handle hoMemb hoObj
If not hoJson ;
Function_Return 0
If (sPath = "") Begin
Send Destroy of hoJson
Function_Return 0
End
If (Left(sPath, 1) = "[") ;
Function_Return (ArrayHandle(Self, hoJson, sPath))
Move (Pos(".", sPath)) to iDotPos
Move (Pos("[", sPath)) to iLSBPos
If (iLSBPos and iDotPos) ;
Move (iDotPos min iLSBPos) to iPos
Else If (iDotPos) ;
Move iDotPos to iPos
Else ;
Move iLSBPos to iPos
If not iPos Begin // We are there!
If (HasMember(hoJson, sPath)) Begin
Get Member of hoJson sPath to hoMemb
Send Destroy of hoJson
Function_Return hoMemb
End
Else Begin
Send Destroy of hoJson
Function_Return 0
End
End
Move (Left(sPath, (iPos - 1))) to sMemb
If (HasMember(hoJson, sMemb)) Begin
Get Member of hoJson sMemb to hoMemb
Send Destroy of hoJson
Move (Right(sPath, (Length(sPath) - iPos + 1))) to sPath
If (Left(sPath, 1) = ".") ;
Move (Replace(".", sPath, "")) to sPath
Get ObjectHandle hoMemb sPath to hoObj
Function_Return hoObj
End
Send Destroy of hoJson
Function_Return 0
End_Function
Function JsonAtPath Handle hoJson String sPath Returns Handle
Handle hoObj
UChar[] ucaJson
Boolean bOK
If (not(hoJson) or (sPath = "")) ;
Function_Return 0
// Do this to "copy" hoJson to hoObj, so hoJson does not get destroyed
// in the process
Get StringifyUtf8 of hoJson to ucaJson
Get CreateNamed (RefClass(cJsonObject)) "CopyJson" to hoObj
Get ParseUtf8 of hoObj ucaJson to bOK
If (JsonType(hoObj) = jsonTypeArray) ;
Function_Return (ArrayHandle(Self, hoObj, sPath))
Else If (JsonType(hoObj) = jsonTypeObject) ;
Function_Return (ObjectHandle(Self, hoObj, sPath))
Else ;
Function_Return 0
End_Function
Function ValueAtPath Handle hoJson String sPath Returns String
Handle hoObj
String sVal
Integer iType
Get JsonAtPath hoJson sPath to hoObj
If hoObj Begin
Get JsonType of hoObj to iType
If ((iType <> jsonTypeObject) and ;
(iType <> jsonTypeArray) and ;
(iType <> jsonTypeNull)) ;
Move (JsonValue(hoObj)) to sVal
Send Destroy of hoObj
End
Function_Return sVal
End_Function
Function CountAtPath Handle hoJson String sPath Returns Integer
Integer iCount
Get JsonAtPath hoJson sPath to hoJson
If not hoJson ;
Function_Return 0
If (JsonType(hoJson) = jsonTypeArray) ;
Get MemberCount of hoJson to iCount
Send Destroy of hoJson
Function_Return iCount
End_Function
End_Class