aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-07-22 08:43:23 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-07-22 08:50:30 +0200
commite02bfc1854effe0bb55ea6d4320e2b1902eaeae3 (patch)
tree9c5402d2881a83d820dd1a7f1059fae1558cf875
parent413cb3b0c5c70d60eee212c72afed9b5c659b6d8 (diff)
feat(json): Boolean parsing
-rw-r--r--README.md2
-rw-r--r--json.c9
-rw-r--r--json.h1
-rw-r--r--test_json.c38
4 files changed, 39 insertions, 11 deletions
diff --git a/README.md b/README.md
index 008e72e..6bc3aea 100644
--- a/README.md
+++ b/README.md
@@ -12,6 +12,8 @@
- File structure
- Static functions for private?
- JSON float
+- JSON: remove pointer and use bitflag
+- JSON: tagged union for memory
## Resources
diff --git a/json.c b/json.c
index 8a542f5..529dc3a 100644
--- a/json.c
+++ b/json.c
@@ -6,6 +6,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
+#include <string.h>
#include "json.h"
#include "string.h"
@@ -190,6 +191,14 @@ parse_any_result parse_any(Arena *a, byte *src, uptr start_pos) {
parse_object_result object_result = parse_object(a, src, result.pos);
result.value->object = object_result.object;
result.pos = object_result.pos;
+ } else if (memcmp(src + result.pos, "true", 4) == 0) {
+ result.value->boolean = new(a, int, 1);
+ *result.value->boolean = 1;
+ result.pos = start_pos + 4;
+ } else if (memcmp(src + result.pos, "false", 5) == 0) {
+ result.value->boolean = new(a, int, 1);
+ *result.value->boolean = 0;
+ result.pos = start_pos + 5;
}
return result;
diff --git a/json.h b/json.h
index 5d5d703..824c5c6 100644
--- a/json.h
+++ b/json.h
@@ -10,6 +10,7 @@ typedef struct {
size len;
} JSONObject;
typedef struct {
+ int *boolean;
int *integer;
Vec *array; // Vec<JSON>
Str *string;
diff --git a/test_json.c b/test_json.c
index b268685..65891fd 100644
--- a/test_json.c
+++ b/test_json.c
@@ -7,7 +7,7 @@ int test_json_integer() {
JSON json_int = JSON_parse(&perm, "123");
t_assert(*json_int.integer == 123);
- t_assert_equal(Arena_get_used(perm), 36);
+ t_assert_equal(Arena_get_used(perm), 44);
return 0;
}
@@ -29,7 +29,7 @@ int test_json_int_array() {
t_assert(ints[1] == 2);
t_assert(ints[2] == 3);
t_assert(sum == 6);
- t_assert_equal(Arena_get_used(perm), 384);
+ t_assert_equal(Arena_get_used(perm), 464);
return 0;
}
@@ -40,7 +40,7 @@ int test_json_empty_array() {
JSON json_empty_array = JSON_parse(&perm, "[]");
t_assert(json_empty_array.array->len == 0);
- t_assert_equal(Arena_get_used(perm), 136);
+ t_assert_equal(Arena_get_used(perm), 160);
return 0;
}
@@ -50,7 +50,7 @@ int test_json_nested_array() {
JSON doc = JSON_parse(&perm, "[[1,\"abc\"]]");
- t_assert_equal(Arena_get_used(perm), 363);
+ t_assert_equal(Arena_get_used(perm), 427);
JSON sub_array = *Vec_get(doc.array, JSON, 0);
JSON val_1 = *Vec_get(sub_array.array, JSON, 0);
t_assert(*val_1.integer == 1);
@@ -58,7 +58,7 @@ int test_json_nested_array() {
Str str = *val_2.string;
Str expected = S("abc");
t_assert(Str_equals(str, expected));
- t_assert_equal(Arena_get_used(perm), 363);
+ t_assert_equal(Arena_get_used(perm), 427);
return 0;
}
@@ -68,9 +68,9 @@ int test_json_string() {
JSON js = JSON_parse(&perm, "\"abc\"");
- t_assert_equal(Arena_get_used(perm), 51);
+ t_assert_equal(Arena_get_used(perm), 59);
t_assert(Str_equals(*js.string, S("abc")));
- t_assert_equal(Arena_get_used(perm), 51);
+ t_assert_equal(Arena_get_used(perm), 59);
return 0;
}
@@ -81,7 +81,7 @@ int test_json_empty_object() {
JSON doc = JSON_parse(&perm, "{}");
t_assert_equal(doc.object->len, 0);
- t_assert_equal(Arena_get_used(perm), 296);
+ t_assert_equal(Arena_get_used(perm), 320);
return 0;
}
@@ -91,11 +91,27 @@ int test_json_object() {
JSON doc = JSON_parse(&perm, "{\"key\": \"value\"}");
- t_assert_equal(Arena_get_used(perm), 373);
+ t_assert_equal(Arena_get_used(perm), 405);
t_assert_equal(doc.object->len, 1);
t_assert(Str_equals(*Vec_get(&doc.object->keys, Str, 0), S("key")));
- t_assert(Str_equals(*JSONObject_get(doc.object, S("key"))->string,
- S("value"))) t_assert_equal(Arena_get_used(perm), 373);
+ JSON value = *JSONObject_get(doc.object, S("key"));
+ t_assert(Str_equals(*value.string, S("value")));
+ t_assert_equal(Arena_get_used(perm), 405);
+
+ return 0;
+}
+
+int test_json_boolean() {
+ Arena perm = Arena_new(128e3);
+
+ JSON doc = JSON_parse(&perm, "{\"a\": true, \"b\": false}");
+
+ t_assert_equal(Arena_get_used(perm), 460);
+ JSON a = *JSONObject_get(doc.object, S("a"));
+ t_assert(*a.boolean == 1);
+ JSON b = *JSONObject_get(doc.object, S("b"));
+ t_assert(*b.boolean == 0);
+ t_assert_equal(Arena_get_used(perm), 460);
return 0;
}