ucx/json.c

changeset 31
287484519844
parent 30
d33eaaec15da
--- a/ucx/json.c	Fri Dec 12 10:42:53 2025 +0100
+++ b/ucx/json.c	Fri Dec 19 17:22:03 2025 +0100
@@ -111,8 +111,8 @@
         ttype = CX_JSON_TOKEN_STRING;
     } else {
         cxstring s = cx_strcast(str);
-        if (!cx_strcmp(s, CX_STR("true")) || !cx_strcmp(s, CX_STR("false"))
-            || !cx_strcmp(s, CX_STR("null"))) {
+        if (!cx_strcmp(s, "true") || !cx_strcmp(s, "false")
+            || !cx_strcmp(s, "null")) {
             ttype = CX_JSON_TOKEN_LITERAL;
         } else {
             ttype = token_numbertype(str.ptr, str.length);
@@ -410,7 +410,7 @@
             size_t capa = str.length + 32;
             char *space = cxMallocDefault(capa);
             if (space == NULL) return cx_mutstrn(NULL, 0);
-            cxBufferInit(&buf, space, capa, NULL, CX_BUFFER_AUTO_EXTEND);
+            cxBufferInit(&buf, NULL, space, capa, CX_BUFFER_AUTO_EXTEND);
             cxBufferWrite(str.ptr, 1, i, &buf);
             all_printable = false;
         }
@@ -453,10 +453,10 @@
 }
 
 static CxJsonObject json_create_object_map(const CxAllocator *allocator) {
-    // TODO: we might want to add a comparator that is sorting the elements by their key
-    CxMap *map = cxKvListCreateAsMap(allocator, NULL, CX_STORE_POINTERS);
+    CxMap *map = cxKvListCreateAsMap(allocator, CX_STORE_POINTERS);
     if (map == NULL) return NULL; // LCOV_EXCL_LINE
-    cxDefineDestructor(map, cxJsonValueFree);
+    cxSetCompareFunc(map, cxJsonCompare);
+    cxSetDestructor(map, cxJsonValueFree);
     return map;
 }
 
@@ -472,20 +472,20 @@
     v->type = type;
     v->allocator = json->allocator;
     if (type == CX_JSON_ARRAY) {
-        cx_array_initialize_a(json->allocator, v->array.data, 16);
-        if (v->array.data == NULL) goto create_json_value_exit_error; // LCOV_EXCL_LINE
+        if (cx_array_init_a(json->allocator, v->array, 16)) {
+            goto create_json_value_exit_error; // LCOV_EXCL_LINE
+        }
     } else if (type == CX_JSON_OBJECT) {
         v->object = json_create_object_map(json->allocator);
         if (v->object == NULL) goto create_json_value_exit_error; // LCOV_EXCL_LINE
     }
 
     // add the new value to a possible parent
-    if (json->vbuf_size > 0) {
-        CxJsonValue *parent = json->vbuf[json->vbuf_size - 1];
+    if (json->vbuf.size > 0) {
+        CxJsonValue *parent = json->vbuf.data[json->vbuf.size - 1];
         assert(parent != NULL);
         if (parent->type == CX_JSON_ARRAY) {
-            CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL);
-            if (cx_array_simple_add_a(&value_realloc, parent->array.data, v)) {
+            if (cx_array_add_a(json->allocator, parent->array, v)) {
                 goto create_json_value_exit_error; // LCOV_EXCL_LINE
             }
         } else if (parent->type == CX_JSON_OBJECT) {
@@ -503,10 +503,19 @@
 
     // add the new value to the stack, if it is an array or object
     if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) {
-        CxArrayReallocator vbuf_realloc = cx_array_reallocator(NULL, json->vbuf_internal);
-        if (cx_array_simple_add_a(&vbuf_realloc, json->vbuf, v)) {
-            goto create_json_value_exit_error; // LCOV_EXCL_LINE
+        if (json->vbuf.size >= json->vbuf.capacity) {
+            int alloc_error;
+            if (json->vbuf.data == json->vbuf_internal) {
+                alloc_error = cx_array_copy_to_new(json->vbuf, json->vbuf.size+1);
+            } else {
+                alloc_error = cx_array_reserve(json->vbuf, json->vbuf.size+1);
+            }
+            if (alloc_error) {
+                goto create_json_value_exit_error; // LCOV_EXCL_LINE
+            }
         }
+        json->vbuf.data[json->vbuf.size] = v;
+        json->vbuf.size++;
     }
 
     // if currently no value is parsed, this is now the value of interest
@@ -540,22 +549,18 @@
     memset(json, 0, sizeof(CxJson));
     json->allocator = allocator;
 
-    json->states = json->states_internal;
-    json->states_capacity = cx_nmemb(json->states_internal);
-    json->states[0] = JP_STATE_VALUE_BEGIN;
-    json->states_size = 1;
-
-    json->vbuf = json->vbuf_internal;
-    json->vbuf_capacity = cx_nmemb(json->vbuf_internal);
+    cx_array_init_fixed(json->states, json->states_internal, 1);
+    json->states.data[0] = JP_STATE_VALUE_BEGIN;
+    cx_array_init_fixed(json->vbuf, json->vbuf_internal, 0);
 }
 
 void cxJsonDestroy(CxJson *json) {
     cxBufferDestroy(&json->buffer);
-    if (json->states != json->states_internal) {
-        cxFreeDefault(json->states);
+    if (json->states.data != json->states_internal) {
+        cx_array_free(json->states);
     }
-    if (json->vbuf != json->vbuf_internal) {
-        cxFreeDefault(json->vbuf);
+    if (json->vbuf.data != json->vbuf_internal) {
+        cx_array_free(json->vbuf);
     }
     cxJsonValueFree(json->parsed);
     json->parsed = NULL;
@@ -574,8 +579,8 @@
         // reinitialize the buffer
         cxBufferDestroy(&json->buffer);
         if (buf == NULL) buf = ""; // buffer must not be initialized with NULL
-        cxBufferInit(&json->buffer, (char*) buf, size,
-            NULL, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE);
+        cxBufferInit(&json->buffer, NULL, (char*) buf,
+                     size, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE);
         json->buffer.size = size;
         return 0;
     } else {
@@ -584,9 +589,9 @@
 }
 
 static void json_add_state(CxJson *json, int state) {
-    // we have guaranteed the necessary space with cx_array_simple_reserve()
+    // we have guaranteed the necessary space
     // therefore, we can safely add the state in the simplest way possible
-    json->states[json->states_size++] = state;
+    json->states.data[json->states.size++] = state;
 }
 
 #define return_rec(code) \
@@ -607,13 +612,21 @@
     }
 
     // pop the current state
-    assert(json->states_size > 0);
-    int state = json->states[--json->states_size];
+    assert(json->states.size > 0);
+    int state = json->states.data[--json->states.size];
 
-    // guarantee that at least two more states fit on the stack
-    CxArrayReallocator state_realloc = cx_array_reallocator(NULL, json->states_internal);
-    if (cx_array_simple_reserve_a(&state_realloc, json->states, 2)) {
-        return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
+    // guarantee that at least two more states fit into the array
+    const size_t required_states_depth = json->states.size + 2;
+    if (required_states_depth >= json->states.capacity) {
+        int alloc_error;
+        if (json->states.data == json->states_internal) {
+            alloc_error = cx_array_copy_to_new(json->states, required_states_depth);
+        } else {
+            alloc_error = cx_array_reserve(json->states, required_states_depth);
+        }
+        if (alloc_error) {
+            return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
+        }
     }
 
 
@@ -645,6 +658,16 @@
                 json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE);
                 return_rec(CX_JSON_NO_ERROR);
             }
+            case CX_JSON_TOKEN_END_ARRAY: {
+                if (state == JP_STATE_VALUE_BEGIN_AR) {
+                    // discard the array from the value buffer
+                    json->vbuf.size--;
+                    json->states.size--;
+                    return_rec(CX_JSON_NO_ERROR);
+                } else {
+                    return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
+                }
+            }
             case CX_JSON_TOKEN_STRING: {
                 if ((vbuf = json_create_value(json, CX_JSON_STRING)) == NULL) {
                     return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
@@ -678,9 +701,9 @@
                 if ((vbuf = json_create_value(json, CX_JSON_LITERAL)) == NULL) {
                     return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
-                if (0 == cx_strcmp(cx_strcast(token.content), cx_str("true"))) {
+                if (0 == cx_strcmp(token.content, "true")) {
                     vbuf->literal = CX_JSON_TRUE;
-                } else if (0 == cx_strcmp(cx_strcast(token.content), cx_str("false"))) {
+                } else if (0 == cx_strcmp(token.content, "false")) {
                     vbuf->literal = CX_JSON_FALSE;
                 } else {
                     vbuf->literal = CX_JSON_NULL;
@@ -698,7 +721,7 @@
             return_rec(CX_JSON_NO_ERROR);
         } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) {
             // discard the array from the value buffer
-            json->vbuf_size--;
+            json->vbuf.size--;
             return_rec(CX_JSON_NO_ERROR);
         } else {
             return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
@@ -706,7 +729,7 @@
     } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) {
         if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
             // discard the obj from the value buffer
-            json->vbuf_size--;
+            json->vbuf.size--;
             return_rec(CX_JSON_NO_ERROR);
         } else {
             // expect string
@@ -721,7 +744,7 @@
             }
             assert(json->uncompleted_member_name.ptr == NULL);
             json->uncompleted_member_name = name;
-            assert(json->vbuf_size > 0);
+            assert(json->vbuf.size > 0);
 
             // next state
             json_add_state(json, JP_STATE_OBJ_COLON);
@@ -742,7 +765,7 @@
             return_rec(CX_JSON_NO_ERROR);
         } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
             // discard the obj from the value buffer
-            json->vbuf_size--;
+            json->vbuf.size--;
             return_rec(CX_JSON_NO_ERROR);
         } else {
             return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
@@ -767,17 +790,17 @@
     CxJsonStatus result;
     do {
         result = json_parse(json);
-        if (result == CX_JSON_NO_ERROR && json->states_size == 1) {
+        if (result == CX_JSON_NO_ERROR && json->states.size == 1) {
             // final state reached
-            assert(json->states[0] == JP_STATE_VALUE_END);
-            assert(json->vbuf_size == 0);
+            assert(json->states.data[0] == JP_STATE_VALUE_END);
+            assert(json->vbuf.size == 0);
 
             // write output value
             *value = json->parsed;
             json->parsed = NULL;
 
             // re-initialize state machine
-            json->states[0] = JP_STATE_VALUE_BEGIN;
+            json->states.data[0] = JP_STATE_VALUE_BEGIN;
 
             return CX_JSON_NO_ERROR;
         }
@@ -786,7 +809,7 @@
     // the parser might think there is no data
     // but when we did not reach the final state,
     // we know that there must be more to come
-    if (result == CX_JSON_NO_DATA && json->states_size > 1) {
+    if (result == CX_JSON_NO_DATA && json->states.size > 1) {
         return CX_JSON_INCOMPLETE_DATA;
     }
 
@@ -832,11 +855,10 @@
             break;
         }
         case CX_JSON_ARRAY: {
-            CxJsonArray array = value->array;
-            for (size_t i = 0; i < array.data_size; i++) {
-                cxJsonValueFree(array.data[i]);
+            for (size_t i = 0; i < value->array.size; i++) {
+                cxJsonValueFree(value->array.data[i]);
             }
-            cxFree(value->allocator, array.data);
+            cx_array_free_a(value->allocator, value->array);
             break;
         }
         case CX_JSON_STRING: {
@@ -865,14 +887,23 @@
     return v;
 }
 
-CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator) {
+CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator, size_t capacity) {
     if (allocator == NULL) allocator = cxDefaultAllocator;
     CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
     if (v == NULL) return NULL;
     v->allocator = allocator;
     v->type = CX_JSON_ARRAY;
-    cx_array_initialize_a(allocator, v->array.data, 16);
-    if (v->array.data == NULL) { cxFree(allocator, v); return NULL; }
+    if (capacity > 0) {
+        if (cx_array_init_a(allocator, v->array, capacity)) {
+            // LCOV_EXCL_START
+            cxFree(allocator, v);
+            return NULL;
+            // LCOV_EXCL_STOP
+        }
+    } else {
+        v->array.data = NULL;
+        v->array.size = v->array.capacity = 0;
+    }
     return v;
 }
 
@@ -990,13 +1021,8 @@
 }
 
 int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count) {
-    CxArrayReallocator value_realloc = cx_array_reallocator(arr->allocator, NULL);
     assert(arr->type == CX_JSON_ARRAY);
-    return cx_array_simple_copy_a(&value_realloc,
-            arr->array.data,
-            arr->array.data_size,
-            val, count
-    );
+    return cx_array_add_array_a(arr->allocator, arr->array, val, count);
 }
 
 int cx_json_obj_put(CxJsonValue* obj, cxstring name, CxJsonValue* child) {
@@ -1010,8 +1036,8 @@
     return v;
 }
 
-CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name) {
-    CxJsonValue* v = cxJsonCreateArr(obj->allocator);
+CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name, size_t capacity) {
+    CxJsonValue* v = cxJsonCreateArr(obj->allocator, capacity);
     if (v == NULL) return NULL;
     if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
     return v;
@@ -1046,23 +1072,18 @@
 }
 
 CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index) {
-    if (index >= value->array.data_size) {
+    if (index >= value->array.size) {
         return &cx_json_value_nothing;
     }
     return value->array.data[index];
 }
 
 CxJsonValue *cxJsonArrRemove(CxJsonValue *value, size_t index) {
-    if (index >= value->array.data_size) {
+    if (index >= value->array.size) {
         return NULL;
     }
     CxJsonValue *ret = value->array.data[index];
-    // TODO: replace with a low level cx_array_remove()
-    size_t count = value->array.data_size - index - 1;
-    if (count > 0) {
-        memmove(value->array.data + index, value->array.data + index + 1, count * sizeof(CxJsonValue*));
-    }
-    value->array.data_size--;
+    cx_array_remove(value->array, index);
     return ret;
 }
 
@@ -1095,11 +1116,7 @@
 }
 
 CxIterator cxJsonArrIter(const CxJsonValue *value) {
-    return cxIteratorPtr(
-        value->array.data,
-        value->array.data_size,
-        true // arrays need to keep order
-    );
+    return cx_array_iterator_ptr(value->array);
 }
 
 CxMapIterator cxJsonObjIter(const CxJsonValue *value) {
@@ -1409,3 +1426,144 @@
     }
     return cx_json_write_rec(target, value, wfunc, settings, 0);
 }
+
+static cxmutstr cx_json_to_string(CxJsonValue *value, const CxAllocator *allocator, CxJsonWriter *writer) {
+    if (allocator == NULL) allocator = cxDefaultAllocator;
+    CxBuffer buffer;
+    if (cxBufferInit(&buffer, allocator, NULL, 128,
+                     CX_BUFFER_AUTO_EXTEND | CX_BUFFER_DO_NOT_FREE)) {
+        return (cxmutstr){NULL, 0};
+    }
+    if (cx_json_write_rec(&buffer, value, cxBufferWriteFunc, writer, 0)
+            || cxBufferTerminate(&buffer)) {
+        // LCOV_EXCL_START
+        buffer.flags &= ~CX_BUFFER_DO_NOT_FREE;
+        cxBufferDestroy(&buffer);
+        return (cxmutstr){NULL, 0};
+        // LCOV_EXCL_STOP
+    } else {
+        cxmutstr str = cx_mutstrn(buffer.space, buffer.size);
+        cxBufferDestroy(&buffer);
+        return str;
+    }
+
+}
+
+cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value) {
+    CxJsonWriter writer = cxJsonWriterCompact();
+    return cx_json_to_string(value, allocator, &writer);
+}
+
+cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value) {
+    CxJsonWriter writer = cxJsonWriterPretty(true);
+    return cx_json_to_string(value, allocator, &writer);
+}
+
+int cxJsonCompare(const CxJsonValue *json, const CxJsonValue *other) {
+    if (json == other) return 0;
+    if (json == NULL || other == NULL) return -1;
+    if (json->type != other->type) {
+        if (!cxJsonIsNumber(json)) return -1;
+        if (!cxJsonIsNumber(other)) return -1;
+    }
+    switch (json->type) {
+        case CX_JSON_NOTHING:
+            return 0;
+        case CX_JSON_OBJECT:
+            return cxMapCompare(json->object, other->object);
+        case CX_JSON_ARRAY:
+            if (json->array.size != other->array.size) return -1;
+            for (size_t i = 0; i < json->array.size; i++) {
+                const int d = cxJsonCompare(json->array.data[i], other->array.data[i]);
+                if (d != 0) return d;
+            }
+            return 0;
+        case CX_JSON_STRING:
+            return cx_strcmp(json->string, other->string);
+        case CX_JSON_INTEGER:
+            if (other->type == CX_JSON_INTEGER) {
+                return cx_vcmp_int64(json->integer, other->integer);
+            } else {
+                return cx_vcmp_double(cxJsonAsDouble(json), other->number);
+            }
+        case CX_JSON_NUMBER:
+            return cx_vcmp_double(json->number, cxJsonAsDouble(other));
+        case CX_JSON_LITERAL:
+            return json->literal == other->literal ? 0 : -1;
+        default:
+            // LCOV_EXCL_START
+            // unreachable
+            assert(false);
+            return -1;
+            // LCOV_EXCL_STOP
+    }
+}
+
+CxJsonValue* cxJsonClone(const CxJsonValue* value, const CxAllocator* allocator) {
+    return cx_json_clone_func(NULL, value, allocator, NULL);
+}
+
+CxJsonValue* cx_json_clone_func(CxJsonValue* target, const CxJsonValue* source,
+        const CxAllocator* allocator, cx_attr_unused void *data) {
+    if (source == NULL || source->type == CX_JSON_NOTHING) {
+        return &cx_json_value_nothing;
+    }
+    if (allocator == NULL) allocator = cxDefaultAllocator;
+
+#define return_value(v) { \
+        CxJsonValue *ret = v; \
+        if (target == NULL) { \
+            return ret; \
+        } else { \
+            *target = *ret; \
+            cxFree(allocator, ret); \
+            return target; \
+        } \
+    }
+
+    switch (source->type) {
+        case CX_JSON_OBJECT: {
+            CxJsonValue *obj = cxJsonCreateObj(allocator);
+            if (obj == NULL) return NULL; // LCOV_EXCL_LINE
+            if (cxMapClone(obj->object, source->object, cxJsonCloneFunc, allocator, NULL)) {
+                // LCOV_EXCL_START
+                cxJsonValueFree(obj);
+                return NULL;
+                // LCOV_EXCL_STOP
+            }
+            return_value(obj);
+        }
+        case CX_JSON_ARRAY: {
+            const size_t elem_count = source->array.size;
+            CxJsonValue *arr = cxJsonCreateArr(allocator, elem_count);
+            if (arr == NULL) return NULL; // LCOV_EXCL_LINE
+            arr->array.size = elem_count;
+            for (size_t i = 0 ; i < elem_count ; i++) {
+                CxJsonValue *e = cx_json_clone_func(NULL, source->array.data[i], allocator, NULL);
+                if (e == NULL) {
+                    // LCOV_EXCL_START
+                    cxJsonValueFree(arr);
+                    return NULL;
+                    // LCOV_EXCL_STOP
+                }
+                arr->array.data[i] = e;
+            }
+            return_value(arr);
+        }
+        case CX_JSON_STRING:
+            return_value(cxJsonCreateString(allocator, source->string));
+        case CX_JSON_INTEGER:
+            return_value(cxJsonCreateInteger(allocator, source->integer));
+        case CX_JSON_NUMBER:
+            return_value(cxJsonCreateNumber(allocator, source->number));
+        case CX_JSON_LITERAL:
+            return_value(cxJsonCreateLiteral(allocator, source->literal));
+        default:
+            // LCOV_EXCL_START
+            // unreachable
+            assert(false);
+            return NULL;
+            // LCOV_EXCL_STOP
+    }
+#undef return_value
+}

mercurial