src/cx/json.h

changeset 1000
1aecddf7e209
parent 996
333155f234c4
child 1002
1483c47063a8
--- a/src/cx/json.h	Thu Dec 05 01:51:47 2024 +0100
+++ b/src/cx/json.h	Thu Dec 05 01:54:12 2024 +0100
@@ -77,20 +77,8 @@
     CX_JSON_FALSE
 };
 
-enum cx_json_reader_type {
-    CX_JSON_READER_OBJECT_BEGIN,
-    CX_JSON_READER_OBJECT_END,
-    CX_JSON_READER_ARRAY_BEGIN,
-    CX_JSON_READER_ARRAY_END,
-    CX_JSON_READER_STRING,
-    CX_JSON_READER_INTEGER,
-    CX_JSON_READER_NUMBER,
-    CX_JSON_READER_LITERAL
-};
-
 typedef enum cx_json_token_type CxJsonTokenType;
 typedef enum cx_json_value_type CxJsonValueType;
-typedef enum cx_json_reader_type CxJsonReaderType;
 
 typedef struct cx_json_s CxJson;
 typedef struct cx_json_token_s CxJsonToken;
@@ -106,47 +94,6 @@
 
 typedef struct cx_json_obj_value_s CxJsonObjValue;
 
-struct cx_json_token_s {
-    CxJsonTokenType tokentype;
-    const char *content;
-    size_t length;
-    size_t alloc;
-};
-
-struct cx_json_s {
-    const CxAllocator *allocator;
-    const char *buffer;
-    size_t size;
-    size_t pos;
-
-    CxJsonToken uncompleted;
-    int tokenizer_escape;
-
-    int *states;
-    size_t nstates;
-    size_t states_alloc;
-    int states_internal[8];
-
-    CxJsonToken reader_token;
-    CxJsonReaderType reader_type;
-    int value_ready;
-    char *value_name;
-    size_t value_name_len;
-    char *value_str;
-    size_t value_str_len;
-    int64_t value_int;
-    double value_double;
-
-    CxJsonValue **readvalue_stack;
-    unsigned readvalue_nelm;
-    unsigned readvalue_alloc;
-    CxJsonValue *read_value;
-    int readvalue_initialized;
-
-    unsigned reader_array_alloc;
-
-    int error;
-};
 
 struct cx_json_array_s {
     CX_ARRAY_DECLARE(CxJsonValue*, array);
@@ -174,9 +121,54 @@
     } value;
 };
 
+struct cx_json_token_s {
+    CxJsonTokenType tokentype;
+    const char *content;
+    size_t length;
+    size_t alloc;
+};
 
-cx_attr_nonnull_arg(2)
-void cxJsonInit(const CxAllocator *allocator, CxJson *json);
+struct cx_json_s {
+    const CxAllocator *allocator;
+    const char *buffer;
+    size_t size;
+    size_t pos;
+
+    CxJsonToken uncompleted;
+
+    /**
+     * A pointer to an intermediate state of the currently parsed value.
+     *
+     * Never access this value manually.
+     */
+    CxJsonValue *parsed;
+
+    /**
+     * State stack.
+     */
+    CX_ARRAY_DECLARE_SIZED(int, states, unsigned);
+
+    /**
+     * Value buffer stack.
+     */
+    CX_ARRAY_DECLARE_SIZED(CxJsonValue*, vbuf, unsigned);
+
+    /**
+     * Internally reserved memory for the state stack.
+     */
+    int states_internal[8];
+
+    /**
+     * Internally reserved memory for the value buffer stack.
+     */
+    CxJsonValue* vbuf_internal[8];
+
+    int error; // TODO: currently unused
+    bool tokenizer_escape; // TODO: check if it can be replaced with look-behind
+};
+
+cx_attr_nonnull_arg(1)
+void cxJsonInit(CxJson *json, const CxAllocator *allocator);
 
 cx_attr_nonnull
 void cxJsonDestroy(CxJson *json);
@@ -250,7 +242,6 @@
 
 void cxJsonValueFree(CxJsonValue *value);
 
-// TODO: if the CxJsonValue was a returned value, we could reference cxJsonValueFree() as deallocator
 cx_attr_nonnull
 int cxJsonNext(CxJson *json, CxJsonValue **value);
 

mercurial