-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.c
200 lines (176 loc) · 5.72 KB
/
path.c
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
/*
* Copyright (c) 2012 Rogerz Zhang <[email protected]>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <string.h>
#include <assert.h>
#include <jansson.h>
#include "jansson_private.h"
json_t *json_path_get(const json_t *json, const char *path)
{
static const char root_chr = '$', array_open = '[';
static const char *path_delims = ".[", *array_close = "]";
const json_t *cursor;
char *token, *buf, *peek, *endptr, delim = '\0';
const char *expect;
if (!json || !path || path[0] != root_chr)
return NULL;
else
buf = jsonp_strdup(path);
peek = buf + 1;
cursor = json;
token = NULL;
expect = path_delims;
while (peek && *peek && cursor)
{
char *last_peek = peek;
peek = strpbrk(peek, expect);
if (peek) {
if (!token && peek != last_peek)
goto fail;
delim = *peek;
*peek++ = '\0';
} else if (expect != path_delims || !token) {
goto fail;
}
if (expect == path_delims) {
if (token) {
cursor = json_object_get(cursor, token);
}
expect = (delim == array_open ? array_close : path_delims);
token = peek;
} else if (expect == array_close) {
size_t index = strtol(token, &endptr, 0);
if (*endptr)
goto fail;
cursor = json_array_get(cursor, index);
token = NULL;
expect = path_delims;
} else {
goto fail;
}
}
jsonp_free(buf);
return (json_t *)cursor;
fail:
jsonp_free(buf);
return NULL;
}
int json_path_set_new(json_t *json, const char *path, json_t *value, size_t flags, json_error_t *error)
{
static const char root_chr = '$', array_open = '[', object_delim = '.';
static const char * const path_delims = ".[", *array_close = "]";
json_t *cursor, *parent = NULL;
char *token, *buf = NULL, *peek, delim = '\0';
const char *expect;
int index_saved = -1;
jsonp_error_init(error, "<path>");
if (!json || !path || flags || !value) {
jsonp_error_set(error, -1, -1, 0, "invalid argument");
goto fail;
} else {
buf = jsonp_strdup(path);
}
if (buf[0] != root_chr) {
jsonp_error_set(error, -1, -1, 0, "path should start with $");
goto fail;
}
peek = buf + 1;
cursor = json;
token = NULL;
expect = path_delims;
while (peek && *peek && cursor)
{
char *last_peek = peek;
peek = strpbrk(last_peek, expect);
if (peek) {
if (!token && peek != last_peek) {
jsonp_error_set(error, -1, -1, last_peek - buf, "unexpected trailing chars");
goto fail;
}
delim = *peek;
*peek++ = '\0';
} else { // end of path
if (expect == path_delims) {
break;
} else {
jsonp_error_set(error, -1, -1, peek - buf, "missing ']'?");
goto fail;
}
}
if (expect == path_delims) {
if (token) {
if (token[0] == '\0') {
jsonp_error_set(error, -1, -1, peek - buf, "empty token");
goto fail;
}
parent = cursor;
cursor = json_object_get(parent, token);
if (!cursor) {
if (!json_is_object(parent)) {
jsonp_error_set(error, -1, -1, peek - buf, "object expected");
goto fail;
}
if (delim == object_delim) {
cursor = json_object();
json_object_set_new(parent, token, cursor);
} else {
jsonp_error_set(error, -1, -1, peek - buf, "new array is not allowed");
goto fail;
}
}
}
expect = (delim == array_open ? array_close : path_delims);
token = peek;
} else if (expect == array_close) {
char *endptr;
size_t index;
parent = cursor;
if (!json_is_array(parent)) {
jsonp_error_set(error, -1, -1, peek - buf, "array expected");
goto fail;
}
index = strtol(token, &endptr, 0);
if (*endptr) {
jsonp_error_set(error, -1, -1, peek - buf, "invalid array index");
goto fail;
}
cursor = json_array_get(parent, index);
if (!cursor) {
jsonp_error_set(error, -1, -1, peek - buf, "array index out of bound");
goto fail;
}
index_saved = index;
token = NULL;
expect = path_delims;
} else {
assert(1);
jsonp_error_set(error, -1, -1, peek - buf, "kidding me");
goto fail;
}
}
if (token) {
if (json_is_object(cursor)) {
json_object_set(cursor, token, value);
} else {
jsonp_error_set(error, -1, -1, peek - buf, "object expected");
goto fail;
}
cursor = json_object_get(cursor, token);
} else if (index_saved != -1 && json_is_array(parent)) {
json_array_set(parent, index_saved, value);
cursor = json_array_get(parent, index_saved);
} else {
jsonp_error_set(error, -1, -1, peek - buf, "invalid path");
goto fail;
}
json_decref(value);
jsonp_free(buf);
return 0;
fail:
json_decref(value);
jsonp_free(buf);
return -1;
}