src/cx/json.h

changeset 1002
1483c47063a8
parent 1000
1aecddf7e209
child 1007
81b2986d2b04
--- a/src/cx/json.h	Sat Dec 07 23:59:54 2024 +0100
+++ b/src/cx/json.h	Sun Dec 08 00:13:38 2024 +0100
@@ -123,9 +123,8 @@
 
 struct cx_json_token_s {
     CxJsonTokenType tokentype;
-    const char *content;
-    size_t length;
-    size_t alloc;
+    bool allocated;
+    cxmutstr content;
 };
 
 struct cx_json_s {
@@ -167,6 +166,60 @@
     bool tokenizer_escape; // TODO: check if it can be replaced with look-behind
 };
 
+/**
+ * Status codes for the json interface.
+ */
+enum cx_json_status {
+    /**
+     * Everything is fine.
+     */
+    CX_JSON_NO_ERROR,
+    /**
+     * The input buffer does not contain more data.
+     */
+    CX_JSON_NO_DATA,
+    /**
+     * The input ends unexpectedly.
+     *
+     * Refill the buffer with cxJsonFill() to complete the json data.
+     */
+    CX_JSON_INCOMPLETE_DATA,
+    /**
+     * Not used as a status and never returned by any function.
+     *
+     * You can use this enumerator to check for all "good" status results
+     * by checking if the status is less than \c CX_JSON_OK.
+     *
+     * A "good" status means, that you can refill data and continue parsing.
+     */
+    CX_JSON_OK,
+    /**
+     * Input buffer is \c NULL.
+     */
+    CX_JSON_NULL_INPUT,
+    /**
+     * Allocating memory for the internal buffer failed.
+     */
+    CX_JSON_BUFFER_ALLOC_FAILED,
+    /**
+     * Allocating memory for a json value failed.
+     */
+    CX_JSON_VALUE_ALLOC_FAILED,
+    /**
+     * A number value is incorrectly formatted.
+     */
+    CX_JSON_FORMAT_ERROR_NUMBER,
+    /**
+     * The tokenizer found something unexpected.
+     */
+    CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN
+};
+
+/**
+ * Typedef for the json status enum.
+ */
+typedef enum cx_json_status CxJsonStatus;
+
 cx_attr_nonnull_arg(1)
 void cxJsonInit(CxJson *json, const CxAllocator *allocator);
 
@@ -243,7 +296,7 @@
 void cxJsonValueFree(CxJsonValue *value);
 
 cx_attr_nonnull
-int cxJsonNext(CxJson *json, CxJsonValue **value);
+CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value);
 
 cx_attr_nonnull
 static inline bool cxJsonIsObject(CxJsonValue *value) {

mercurial