summaryrefslogtreecommitdiff
path: root/src/json.c
diff options
context:
space:
mode:
authorAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2024-04-12 12:18:03 +0200
committerAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2024-04-12 12:18:03 +0200
commit1dd3e1c8d7deb0fa6fe6208b2dd8d236d1f3fc2e (patch)
tree92c27ac0a3bf86bfe5f00214e691c3c388dcab27 /src/json.c
parent46bc58545b95ee1cba1292dcaeb46c60533e0184 (diff)
libjson: new api
a lot... changed... Signed-off-by: Anna (navi) Figueiredo Gomes <navi@vlhl.dev>
Diffstat (limited to 'src/json.c')
-rw-r--r--src/json.c73
1 files changed, 49 insertions, 24 deletions
diff --git a/src/json.c b/src/json.c
index cf71a90..5d8fb61 100644
--- a/src/json.c
+++ b/src/json.c
@@ -11,20 +11,39 @@
#include <signal.h>
-struct json *json_parse(const char *str) {
- return json_parse_len(str, strlen(str));
-}
-
struct json *json_new(void) {
struct json *json = calloc(1, sizeof(*json));
if (!json)
return NULL;
- json->type = JSON_INVALID;
+ json->type = JSON_NULL;
+ return json;
+}
+
+struct json *json_dup(const struct json *src) {
+ struct json *json = json_new();
+ switch (src->type) {
+ case JSON_OBJECT:
+ case JSON_ARRAY:
+ for (size_t i = 0; i < src->children->nitems; i++) {
+ add_children(json, json_dup(src->children->items[i]));
+ }
+ break;
+ case JSON_STRING:
+ json->string = strdup(src->string);
+ break;
+ case JSON_NUMBER:
+ json->num = src->num;
+ break;
+ default:
+ break;
+ }
+ json->type = src->type;
return json;
}
void json_detach(struct json *json) {
- if (!json || !json->parent) return;
+ if (!json || !json->parent)
+ return;
struct json_children *siblings = json->parent->children;
if (json->index != --siblings->nitems) {
@@ -66,35 +85,35 @@ void json_clear(struct json *json) {
break;
}
free(json->key);
- json->type = JSON_INVALID;
+ json->type = JSON_NULL;
}
-struct json *json_new_array(void) {
+struct json *json_object(void) {
struct json *json = json_new();
- if (!json)
- return NULL;
- json->type = JSON_ARRAY;
+ json->type = JSON_OBJECT;
init_children(json);
return json;
}
-struct json *json_new_bool(bool boolean) {
+struct json *json_array(void) {
struct json *json = json_new();
if (!json)
return NULL;
- json->type = boolean ? JSON_TRUE : JSON_FALSE;
+ json->type = JSON_ARRAY;
+ init_children(json);
return json;
}
-struct json *json_new_null(void) {
+struct json *json_bool(bool boolean) {
struct json *json = json_new();
if (!json)
return NULL;
- json->type = JSON_NULL;
+ json->type = JSON_BOOL;
+ json->boolean = boolean;
return json;
}
-struct json *json_new_number(double num) {
+struct json *json_number(double num) {
struct json *json = json_new();
if (!json)
return NULL;
@@ -103,24 +122,30 @@ struct json *json_new_number(double num) {
return json;
}
-struct json *json_new_string(void) {
+struct json *json_string(const char *string) {
struct json *json = json_new();
if (!json)
return NULL;
json->type = JSON_STRING;
- json->string = NULL;
+ json->string = strdup(string);
return json;
}
-struct json *json_new_string_copy(const char *string) {
+struct json *json_string_from(char *string) {
struct json *json = json_new();
if (!json)
return NULL;
json->type = JSON_STRING;
- json->string = strdup(string);
+ json->string = string;
return json;
}
+size_t json_len(const struct json *json) {
+ if (!json_check_type(json, JSON_OBJECT | JSON_ARRAY))
+ return 0;
+ return json->children->nitems;
+}
+
enum json_parse_result parse_value(struct json **json_out, struct raw_json *raw, size_t depth) {
skip_ws(raw);
struct json *json = NULL;
@@ -144,17 +169,17 @@ enum json_parse_result parse_value(struct json **json_out, struct raw_json *raw,
}
struct json *json_parse(const char *str) {
- return json_parse_len(strlen(str), str);
+ return json_parse_sized(strlen(str), str);
}
-struct json *json_parse_len(size_t size, const char str[size]) {
- if (!str || size == 0)
+struct json *json_parse_sized(size_t len, const char str[len]) {
+ if (!str || len == 0)
return NULL;
struct json *json = NULL;
struct raw_json raw = {
.index = 0,
.data = str,
- .size = size
+ .size = len
};
if (parse_value(&json, &raw, 1) != JSON_PARSE_OK) {
return NULL;