src/json.c

changeset 1009
7650e722437e
parent 1008
3b69f025f083
child 1012
21884374edbb
--- a/src/json.c	Tue Dec 10 00:52:15 2024 +0100
+++ b/src/json.c	Tue Dec 10 21:40:39 2024 +0100
@@ -48,20 +48,6 @@
     }
 }
 
-
-static int token_isliteral(const char *content, size_t length) {
-    if (length == 4) {
-        if (!memcmp(content, "true", 4)) {
-            return 1;
-        } else if (!memcmp(content, "null", 4)) {
-            return 1;
-        }
-    } else if (length == 5 && !memcmp(content, "false", 5)) {
-        return 1;
-    }
-    return 0;
-}
-
 static int num_isexp(const char *content, size_t length, size_t pos) {
     if (pos >= length) {
         return 0;
@@ -123,7 +109,9 @@
     if (isstring) {
         ttype = CX_JSON_TOKEN_STRING;
     } else {
-        if (token_isliteral(str.ptr, str.length)) {
+        cxstring s = cx_strcast(str);
+        if (!cx_strcmp(s, CX_STR("true")) || !cx_strcmp(s, CX_STR("false"))
+            || !cx_strcmp(s, CX_STR("null"))) {
             ttype = CX_JSON_TOKEN_LITERAL;
         } else {
             ttype = token_numbertype(str.ptr, str.length);
@@ -383,7 +371,7 @@
     return v;
 }
 
-static int json_obj_add_entry(CxJson *json, char *name) {
+static int json_obj_add_entry(const CxJson *json, char *name) {
     CxJsonObjValue kv = {name, NULL};
     assert(json->vbuf_size > 0);
     CxJsonValue *parent = json->vbuf[json->vbuf_size - 1];
@@ -676,18 +664,17 @@
     cxFree(value->allocator, value);
 }
 
-CxJsonValue *cxJsonArrGet(CxJsonValue *value, size_t index) {
+CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index) {
     if (index >= value->value.array.array_size) {
         return &cx_json_value_nothing;
     }
     return value->value.array.array[index];
 }
 
-CxJsonValue *cxJsonObjGet(CxJsonValue *value, const char *name) {
-    CxJsonObject *obj = &(value->value.object);
+CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char *name) {
+    const CxJsonObject *obj = &(value->value.object);
     // TODO: think about sorting the object so that we can use binary search here
     for (size_t i = 0; i < obj->values_size; i++) {
-        // TODO: we might want to store names as cxmutstr
         if (0 == strcmp(name, obj->values[i].name)) {
             return obj->values[i].value;
         }

mercurial