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
|
#include "jsonpath.h"
#include <ctype.h>
/* NB: These macros evaluate their argument multiple times. */
#define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
/* isalpha() is locale-specific. This simply matches [A-Za-z] . */
#define isextended(c) ((unsigned char)(c) > 127)
/* Note that Unicode characters are allowed in identifiers. */
#define identifier_start(c) (isletter(c) || (c) == '_' || (c) == '$' || isextended(c))
#define identifier_char(c) (identifier_start(c) || isdigit(c))
#define integer_start(c) (isdigit(c) || (c) == '+' || (c) == '-')
/*
* Note that skip_spaces differs from skip_whitespace in json.c
* in that this function treats '\f' and '\v' as whitespace.
* This is because JSON does not accept these characters as
* whitespace, but since this is JSONPath,
* we can do whatever we want here :-)
*/
static void skip_spaces(const char **sp)
{
const char *s = *sp;
while (isspace(*s))
s++;
*sp = s;
}
static jp_element *mkElement(jp_element_type type, bool rd)
{
jp_element *elem = palloc0(sizeof(*elem));
elem->type = type;
elem->recursive_descent = rd;
return elem;
}
static jp_element *mkRoot(void)
{
jp_element *elem = mkElement(JP_ROOT, false);
return elem;
}
static jp_element *mkWildcard(bool rd)
{
jp_element *elem = mkElement(JP_WILDCARD, rd);
return elem;
}
static jp_element *mkIndexSubscript(int index, bool rd)
{
jp_element *elem = mkElement(JP_INDEX_SUBSCRIPT, rd);
elem->data.index = index;
return elem;
}
static jp_element *mkKeySubscript(char *key, size_t length, bool rd)
{
jp_element *elem = mkElement(JP_KEY_SUBSCRIPT, rd);
elem->data.key.ptr = key;
elem->data.key.length = length;
return elem;
}
char *jp_show(JSONPath *jp)
{
StringInfoData string[1];
ListCell *cell;
jp_element *elem;
bool rd;
char *tmp;
initStringInfo(string);
foreach(cell, jp) {
elem = lfirst(cell);
rd = elem->recursive_descent;
switch (elem->type) {
case JP_ROOT:
appendStringInfoChar(string, '$');
break;
case JP_WILDCARD:
appendStringInfoString(string, rd ? "..[*]" : "[*]");
break;
case JP_INDEX_SUBSCRIPT:
appendStringInfo(string, "%s[%ld]", rd ? ".." : "", elem->data.index);
break;
case JP_KEY_SUBSCRIPT:
tmp = json_encode_string(elem->data.key.ptr, elem->data.key.length, '"');
Assert(tmp != NULL);
appendStringInfo(string, "%s[%s]", rd ? ".." : "", tmp);
pfree(tmp);
break;
default:
Assert(false);
}
}
return string->data;
}
JSONPath *jp_parse(const char *pattern)
{
JSONPath *jp = NIL;
const char *s = pattern;
const char *p;
bool recursive_descent = false;
bool bracket = false;
const char *err_msg = NULL;
long index;
char *key;
size_t key_length;
skip_spaces(&s);
/* pattern may not be empty */
if (!*s)
return NULL;
jp = lappend(jp, mkRoot());
if (*s == '$') {
s++;
goto begin_element;
} else if (*s != '.') {
goto dot_subscript; // implicit '.' at beginning
}
begin_element:
skip_spaces(&s);
recursive_descent = false;
bracket = false;
if (*s == '\0')
goto end;
if (s[0] == '.' && s[1] == '.') {
recursive_descent = true;
s += 2;
goto dot_subscript;
}
if (s[0] == '.') {
s++;
goto dot_subscript;
}
if (s[0] == '[') {
s++;
goto bracket_subscript;
}
goto failed;
next_element:
if (bracket) {
skip_spaces(&s);
if (*s != ']')
goto failed;
s++;
}
goto begin_element;
dot_subscript:
skip_spaces(&s);
if (*s == '*')
goto wildcard;
if (integer_start(*s))
goto integer;
if (identifier_start(*s))
goto identifier;
if (*s == '"' || *s == '\'')
goto string;
if (*s == '[') {
s++;
goto bracket_subscript;
}
goto failed;
bracket_subscript:
skip_spaces(&s);
bracket = true;
if (*s == '*')
goto wildcard;
if (integer_start(*s))
goto integer;
if (identifier_start(*s)) {
err_msg = "Identifiers may not be bracketed. This syntax is reserved for future use.";
goto failed;
}
if (*s == '"' || *s == '\'')
goto string;
goto failed;
wildcard:
s++;
jp = lappend(jp, mkWildcard(recursive_descent));
goto next_element;
integer:
p = s;
errno = 0;
index = strtol(s, (char**)&p, 10);
if (p <= s || errno != 0)
goto failed;
s = p;
jp = lappend(jp, mkIndexSubscript(index, recursive_descent));
goto next_element;
identifier:
p = s;
while (identifier_char(*p))
p++;
key = pnstrdup(s, p - s);
key_length = p - s;
s = p;
jp = lappend(jp, mkKeySubscript(key, key_length, recursive_descent));
goto next_element;
string:
key = json_decode_string(&s, &key_length, false);
if (!key)
goto failed;
jp = lappend(jp, mkKeySubscript(key, key_length, recursive_descent));
goto next_element;
end:
return jp;
failed:
return NULL;
}
List *jp_match(JSONPath *jp, json_node *json)
{
}
|