Debian Codegraph @

lib/proc/proc_json.c

raw symbols SCIP
1 /*
2 Copyright (C) 2026 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "../grn_proc.h"
20 #include "../grn_db.h"
21 #include "../grn_expr.h"
22 #include "../grn_store.h"
23
24 #include <groonga/plugin.h>
25
26 typedef struct {
27 grn_obj *json;
28 grn_obj json_buffer;
29 grn_json_path *json_path;
30 } grn_json_extract_data;
31
32 static void
33 grn_json_extract_data_free(grn_ctx *ctx, grn_json_extract_data *data)
34 {
35 if (!data) {
36 return;
37 }
38
39 GRN_OBJ_FIN(ctx, &(data->json_buffer));
40 if (data->json_path) {
41 grn_json_path_close(ctx, data->json_path);
42 }
43
44 GRN_PLUGIN_FREE(ctx, data);
45 }
46
47 static grn_obj *
48 func_json_extract_init(grn_ctx *ctx,
49 int n_args,
50 grn_obj **args,
51 grn_user_data *user_data)
52 {
53 if (n_args != 2) {
54 GRN_PLUGIN_ERROR(ctx,
55 GRN_INVALID_ARGUMENT,
56 "json_extract(): wrong number of arguments (%d for 2)",
57 n_args);
58 return NULL;
59 }
60
61 grn_json_extract_data *data =
62 GRN_PLUGIN_CALLOC(ctx, sizeof(grn_json_extract_data));
63 if (!data) {
64 GRN_PLUGIN_ERROR(ctx,
65 GRN_INVALID_ARGUMENT,
66 "json_extract(): failed to allocate internal data");
67 return NULL;
68 }
69 user_data->ptr = data;
70
71 GRN_JSON_INIT(&(data->json_buffer), 0);
72 grn_obj *json = args[0];
73 if (json->header.domain == GRN_DB_JSON) {
74 data->json = json;
75 } else if (grn_obj_is_text_family_bulk(ctx, json)) {
76 grn_rc rc = grn_obj_cast(ctx, json, &(data->json_buffer), false);
77 if (rc != GRN_SUCCESS) {
78 GRN_PLUGIN_ERROR(ctx,
79 GRN_INVALID_ARGUMENT,
80 "json_extract(): failed to parse JSON: <%.*s>",
81 (int)GRN_TEXT_LEN(json),
82 GRN_TEXT_VALUE(json));
83 return NULL;
84 }
85 data->json = &(data->json_buffer);
86 } else {
87 grn_obj inspected;
88 GRN_TEXT_INIT(&inspected, 0);
89 grn_inspect(ctx, &inspected, json);
90 GRN_PLUGIN_ERROR(ctx,
91 GRN_INVALID_ARGUMENT,
92 "json_extract(): the first argument must be "
93 "a parsed JSON or JSON string: <%.*s>",
94 (int)GRN_TEXT_LEN(&inspected),
95 GRN_TEXT_VALUE(&inspected));
96 GRN_OBJ_FIN(ctx, &inspected);
97 return NULL;
98 }
99
100 grn_obj *json_path = args[1];
101 if (!grn_obj_is_text_family_bulk(ctx, json_path)) {
102 grn_obj inspected;
103 GRN_TEXT_INIT(&inspected, 0);
104 grn_inspect(ctx, &inspected, json_path);
105 GRN_PLUGIN_ERROR(ctx,
106 GRN_INVALID_ARGUMENT,
107 "json_extract(): the second argument must be "
108 "a JSONPath string: <%.*s>",
109 (int)GRN_TEXT_LEN(&inspected),
110 GRN_TEXT_VALUE(&inspected));
111 GRN_OBJ_FIN(ctx, &inspected);
112 return NULL;
113 }
114 data->json_path =
115 grn_json_path_open(ctx, GRN_TEXT_VALUE(json_path), GRN_TEXT_LEN(json_path));
116 if (!data->json_path) {
117 GRN_PLUGIN_ERROR(ctx,
118 GRN_INVALID_ARGUMENT,
119 "json_extract(): failed to parse JSONPath: <%.*s>",
120 (int)GRN_TEXT_LEN(json_path),
121 GRN_TEXT_VALUE(json_path));
122 return NULL;
123 }
124
125 return NULL;
126 }
127
128 static grn_obj *
129 func_json_extract_next(grn_ctx *ctx,
130 int n_args,
131 grn_obj **args,
132 grn_user_data *user_data)
133 {
134 grn_json_extract_data *data = user_data->ptr;
135
136 if (ctx->rc != GRN_SUCCESS) {
137 return NULL;
138 }
139
140 grn_obj *values =
141 grn_plugin_proc_alloc(ctx, user_data, GRN_DB_VOID, GRN_OBJ_VECTOR);
142 if (!values) {
143 return NULL;
144 }
145 grn_rc rc = grn_json_extract(ctx, data->json, data->json_path, values);
146 if (rc != GRN_SUCCESS) {
147 GRN_PLUGIN_ERROR(ctx,
148 rc,
149 "json_extract(): "
150 "failed to extract values: %s",
151 ctx->errbuf);
152 return NULL;
153 }
154
155 return values;
156 }
157
158 static grn_obj *
159 func_json_extract_fin(grn_ctx *ctx,
160 int n_args,
161 grn_obj **args,
162 grn_user_data *user_data)
163 {
164 grn_json_extract_data *data = user_data->ptr;
165
166 grn_json_extract_data_free(ctx, data);
167
168 return NULL;
169 }
170
171 void
172 grn_proc_init_json_extract(grn_ctx *ctx)
173 {
174 grn_proc_create(ctx,
175 "json_extract",
176 -1,
177 GRN_PROC_FUNCTION,
178 func_json_extract_init,
179 func_json_extract_next,
180 func_json_extract_fin,
181 0,
182 NULL);
183 }