Skip to content

Commit 62397b2

Browse files
committed
pack_json: new API to abstract JSON parser backend
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 2787f6a commit 62397b2

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

include/fluent-bit/flb_pack_json.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2015-2024 The Fluent Bit Authors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#ifndef FLB_PACK_JSON_H
21+
#define FLB_PACK_JSON_H
22+
23+
#include <fluent-bit/flb_pack.h>
24+
25+
#define FLB_PACK_JSON_BACKEND_JSMN 1
26+
#define FLB_PACK_JSON_BACKEND_YYJSON 2
27+
28+
29+
struct flb_pack_opts {
30+
/* which backend to use (default, jsmn, yyjson) */
31+
int backend;
32+
33+
/* optional: required only for JSMN in streaming mode */
34+
struct flb_pack_state *state;
35+
};
36+
37+
int flb_pack_json_ext(const char *json, size_t len,
38+
char **out_buf, size_t *out_size,
39+
int *out_root_type,
40+
struct flb_pack_opts *opts);
41+
42+
#endif
43+

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(src
1616
flb_hash_table.c
1717
flb_help.c
1818
flb_pack.c
19+
flb_pack_json.c
1920
flb_pack_gelf.c
2021
flb_sds.c
2122
flb_sds_list.c
@@ -398,6 +399,7 @@ set(FLB_DEPS
398399
ctraces-static
399400
mk_core
400401
jsmn
402+
yyjson
401403
${MSGPACK_LIBRARIES}
402404
mpack-static
403405
chunkio-static

src/flb_pack_json.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*-*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2015-2024 The Fluent Bit Authors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#include <fluent-bit.h>
21+
#include <fluent-bit/flb_pack_json.h>
22+
23+
int flb_pack_json_ext(const char *json, size_t len,
24+
char **out_buf, size_t *out_size,
25+
int *out_root_type,
26+
struct flb_pack_opts *opts)
27+
{
28+
int backend;
29+
struct flb_pack_state *state = NULL;
30+
31+
if (!opts) {
32+
backend = FLB_PACK_JSON_BACKEND_YYJSON;
33+
}
34+
else {
35+
backend = opts->backend;
36+
state = opts->state;
37+
}
38+
39+
if (backend != FLB_PACK_JSON_BACKEND_JSMN &&
40+
backend != FLB_PACK_JSON_BACKEND_YYJSON) {
41+
return -1;
42+
}
43+
44+
if (backend == FLB_PACK_JSON_BACKEND_JSMN) {
45+
/* jsmn */
46+
if (state) {
47+
/* state for incremental reads */
48+
return flb_pack_json_state(json, len, out_buf, out_size, state);
49+
}
50+
else {
51+
/* jsmn (no state) */
52+
return flb_pack_json(json, len, out_buf, out_size,
53+
out_root_type, NULL);
54+
}
55+
}
56+
else if (backend == FLB_PACK_JSON_BACKEND_YYJSON) {
57+
/* yyjson */
58+
return flb_pack_json_yyjson(json, len, out_buf, out_size,
59+
out_root_type, NULL);
60+
}
61+
62+
/* unknown backend */
63+
return -1;
64+
}

0 commit comments

Comments
 (0)