ucx/json.c

2 weeks ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 05 Mar 2025 21:35:36 +0100 (2 weeks ago)
changeset 485
c584149b22f0
parent 471
063a9f29098c
permissions
-rw-r--r--

move ui_customwidget_create to separate file (GTK)

440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "cx/json.h"
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <assert.h>
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <stdio.h>
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <inttypes.h>
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 /*
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 * RFC 8259
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 * https://tools.ietf.org/html/rfc8259
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 */
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 static CxJsonValue cx_json_value_nothing = {.type = CX_JSON_NOTHING};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 static int json_cmp_objvalue(const void *l, const void *r) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 const CxJsonObjValue *left = l;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 const CxJsonObjValue *right = r;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 return cx_strcmp(cx_strcast(left->name), cx_strcast(right->name));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 static CxJsonObjValue *json_find_objvalue(const CxJsonValue *obj, cxstring name) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 assert(obj->type == CX_JSON_OBJECT);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 CxJsonObjValue kv_dummy;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 kv_dummy.name = cx_mutstrn((char*) name.ptr, name.length);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 size_t index = cx_array_binary_search(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 obj->value.object.values,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 obj->value.object.values_size,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 sizeof(CxJsonObjValue),
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 &kv_dummy,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 json_cmp_objvalue
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 );
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 if (index == obj->value.object.values_size) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 return &obj->value.object.values[index];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 static int json_add_objvalue(CxJsonValue *objv, CxJsonObjValue member) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 assert(objv->type == CX_JSON_OBJECT);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 const CxAllocator * const al = objv->allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 CxJsonObject *obj = &(objv->value.object);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 // determine the index where we need to insert the new member
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 size_t index = cx_array_binary_search_sup(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 obj->values,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 obj->values_size,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 sizeof(CxJsonObjValue),
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 &member, json_cmp_objvalue
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 );
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 // is the name already present?
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 if (index < obj->values_size && 0 == json_cmp_objvalue(&member, &obj->values[index])) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 // free the original value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 cx_strfree_a(al, &obj->values[index].name);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 cxJsonValueFree(obj->values[index].value);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 // replace the item
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 obj->values[index] = member;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 // nothing more to do
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 // determine the old capacity and reserve for one more element
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 CxArrayReallocator arealloc = cx_array_reallocator(al, NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 size_t oldcap = obj->values_capacity;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 if (cx_array_simple_reserve_a(&arealloc, obj->values, 1)) return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 // check the new capacity, if we need to realloc the index array
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 size_t newcap = obj->values_capacity;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 if (newcap > oldcap) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 if (cxReallocateArray(al, &obj->indices, newcap, sizeof(size_t))) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 // check if append or insert
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 if (index < obj->values_size) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 // move the other elements
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 memmove(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 &obj->values[index+1],
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 &obj->values[index],
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 (obj->values_size - index) * sizeof(CxJsonObjValue)
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 );
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 // increase indices for the moved elements
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 for (size_t i = 0; i < obj->values_size ; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 if (obj->indices[i] >= index) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 obj->indices[i]++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 // insert the element and set the index
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 obj->values[index] = member;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 obj->indices[obj->values_size] = index;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 obj->values_size++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 static void token_destroy(CxJsonToken *token) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 if (token->allocated) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 cx_strfree(&token->content);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
135 static bool json_isdigit(char c) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
136 // TODO: remove once UCX has public API for this
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
137 return c >= '0' && c <= '9';
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
138 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
139
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
140 static bool json_isspace(char c) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
141 // TODO: remove once UCX has public API for this
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
142 return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f';
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
143 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
144
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 static int num_isexp(const char *content, size_t length, size_t pos) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 if (pos >= length) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 int ok = 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 for (size_t i = pos; i < length; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 char c = content[i];
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
153 if (json_isdigit(c)) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 ok = 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 } else if (i == pos) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 if (!(c == '+' || c == '-')) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 return ok;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 static CxJsonTokenType token_numbertype(const char *content, size_t length) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 if (length == 0) return CX_JSON_TOKEN_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
170 if (content[0] != '-' && !json_isdigit(content[0])) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 return CX_JSON_TOKEN_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 CxJsonTokenType type = CX_JSON_TOKEN_INTEGER;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 for (size_t i = 1; i < length; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 if (content[i] == '.') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 if (type == CX_JSON_TOKEN_NUMBER) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 return CX_JSON_TOKEN_ERROR; // more than one decimal separator
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 type = CX_JSON_TOKEN_NUMBER;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 } else if (content[i] == 'e' || content[i] == 'E') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 return num_isexp(content, length, i + 1) ? CX_JSON_TOKEN_NUMBER : CX_JSON_TOKEN_ERROR;
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
183 } else if (!json_isdigit(content[i])) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 return CX_JSON_TOKEN_ERROR; // char is not a digit, decimal separator or exponent sep
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 return type;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 static CxJsonToken token_create(CxJson *json, bool isstring, size_t start, size_t end) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 cxmutstr str = cx_mutstrn(json->buffer.space + start, end - start);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 bool allocated = false;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 if (json->uncompleted.tokentype != CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 allocated = true;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 str = cx_strcat_m(json->uncompleted.content, 1, str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 if (str.ptr == NULL) { // LCOV_EXCL_START
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 return (CxJsonToken){CX_JSON_NO_TOKEN, false, {NULL, 0}};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 } // LCOV_EXCL_STOP
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 json->uncompleted = (CxJsonToken){0};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 CxJsonTokenType ttype;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 if (isstring) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 ttype = CX_JSON_TOKEN_STRING;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 cxstring s = cx_strcast(str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 if (!cx_strcmp(s, CX_STR("true")) || !cx_strcmp(s, CX_STR("false"))
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 || !cx_strcmp(s, CX_STR("null"))) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 ttype = CX_JSON_TOKEN_LITERAL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 ttype = token_numbertype(str.ptr, str.length);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 if (ttype == CX_JSON_TOKEN_ERROR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 if (allocated) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 cx_strfree(&str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 return (CxJsonToken){CX_JSON_TOKEN_ERROR, false, {NULL, 0}};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 return (CxJsonToken){ttype, allocated, str};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 static CxJsonTokenType char2ttype(char c) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 switch (c) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 case '[': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 return CX_JSON_TOKEN_BEGIN_ARRAY;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 case '{': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 return CX_JSON_TOKEN_BEGIN_OBJECT;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 case ']': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 return CX_JSON_TOKEN_END_ARRAY;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 case '}': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 return CX_JSON_TOKEN_END_OBJECT;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 case ':': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 return CX_JSON_TOKEN_NAME_SEPARATOR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 case ',': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 return CX_JSON_TOKEN_VALUE_SEPARATOR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 case '"': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 return CX_JSON_TOKEN_STRING;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 default: {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
247 if (json_isspace(c)) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 return CX_JSON_TOKEN_SPACE;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 return CX_JSON_NO_TOKEN;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 static enum cx_json_status token_parse_next(CxJson *json, CxJsonToken *result) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 // check if there is data in the buffer
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 if (cxBufferEof(&json->buffer)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 return json->uncompleted.tokentype == CX_JSON_NO_TOKEN ?
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 CX_JSON_NO_DATA : CX_JSON_INCOMPLETE_DATA;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 // current token type and start index
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 CxJsonTokenType ttype = json->uncompleted.tokentype;
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
264 size_t token_part_start = json->buffer.pos;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
265
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
266 bool escape_end_of_string = ttype == CX_JSON_TOKEN_STRING
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
267 && json->uncompleted.content.ptr[json->uncompleted.content.length-1] == '\\';
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 for (size_t i = json->buffer.pos; i < json->buffer.size; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 char c = json->buffer.space[i];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 if (ttype != CX_JSON_TOKEN_STRING) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 // currently non-string token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 CxJsonTokenType ctype = char2ttype(c); // start of new token?
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 if (ttype == CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 if (ctype == CX_JSON_TOKEN_SPACE) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 json->buffer.pos++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 continue;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 } else if (ctype == CX_JSON_TOKEN_STRING) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 // begin string
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 ttype = CX_JSON_TOKEN_STRING;
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
281 token_part_start = i;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 } else if (ctype != CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 // single-char token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 json->buffer.pos = i + 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 *result = (CxJsonToken){ctype, false, {NULL, 0}};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 return CX_JSON_NO_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 ttype = CX_JSON_TOKEN_LITERAL; // number or literal
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
289 token_part_start = i;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 // finish token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 if (ctype != CX_JSON_NO_TOKEN) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
294 *result = token_create(json, false, token_part_start, i);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 if (result->tokentype == CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 if (result->tokentype == CX_JSON_TOKEN_ERROR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 return CX_JSON_FORMAT_ERROR_NUMBER;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 json->buffer.pos = i;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 return CX_JSON_NO_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 // currently inside a string
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
307 if (escape_end_of_string) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
308 escape_end_of_string = false;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 if (c == '"') {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
311 *result = token_create(json, true, token_part_start, i + 1);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 if (result->tokentype == CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 json->buffer.pos = i + 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 return CX_JSON_NO_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 } else if (c == '\\') {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
318 escape_end_of_string = true;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 if (ttype != CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 // uncompleted token
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
326 size_t uncompleted_len = json->buffer.size - token_part_start;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 if (json->uncompleted.tokentype == CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 // current token is uncompleted
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 // save current token content
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 CxJsonToken uncompleted = {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 ttype, true,
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
332 cx_strdup(cx_strn(json->buffer.space + token_part_start, uncompleted_len))
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 };
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 if (uncompleted.content.ptr == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 json->uncompleted = uncompleted;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 // previously we also had an uncompleted token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 // combine the uncompleted token with the current token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 assert(json->uncompleted.allocated);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 cxmutstr str = cx_strcat_m(json->uncompleted.content, 1,
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
343 cx_strn(json->buffer.space + token_part_start, uncompleted_len));
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 if (str.ptr == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 json->uncompleted.content = str;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 // advance the buffer position - we saved the stuff in the uncompleted token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 json->buffer.pos += uncompleted_len;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 return CX_JSON_INCOMPLETE_DATA;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
356 // converts a Unicode codepoint to utf8
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
357 static unsigned codepoint_to_utf8(uint32_t codepoint, char *output_buf) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
358 if (codepoint <= 0x7F) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
359 *output_buf = (char)codepoint;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
360 return 1;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
361 } else if (codepoint <= 0x7FF) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
362 output_buf[0] = (char)(0xC0 | ((codepoint >> 6) & 0x1F));
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
363 output_buf[1] = (char)(0x80 | (codepoint & 0x3F));
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
364 return 2;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
365 } else if (codepoint <= 0xFFFF) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
366 output_buf[0] = (char)(0xE0 | ((codepoint >> 12) & 0x0F));
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
367 output_buf[1] = (char)(0x80 | ((codepoint >> 6) & 0x3F));
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
368 output_buf[2] = (char)(0x80 | (codepoint & 0x3F));
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
369 return 3;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
370 } else if (codepoint <= 0x10FFFF) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
371 output_buf[0] = (char)(0xF0 | ((codepoint >> 18) & 0x07));
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
372 output_buf[1] = (char)(0x80 | ((codepoint >> 12) & 0x3F));
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
373 output_buf[2] = (char)(0x80 | ((codepoint >> 6) & 0x3F));
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
374 output_buf[3] = (char)(0x80 | (codepoint & 0x3F));
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
375 return 4;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
376 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
377
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
378 return 0; // LCOV_EXCL_LINE
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
379 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
380
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
381 // converts a utf16 surrogate pair to utf8
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
382 static inline uint32_t utf16pair_to_codepoint(uint16_t c0, uint16_t c1) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
383 return ((c0 - 0xD800) << 10) + (c1 - 0xDC00) + 0x10000;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
384 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
385
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
386 static unsigned unescape_unicode_string(cxstring str, char *utf8buf) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
387 // str is supposed to start with "\uXXXX" or "\uXXXX\uXXXX"
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
388 // remaining bytes in the string are ignored (str may be larger!)
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
389
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
390 if (str.length < 6 || str.ptr[0] != '\\' || str.ptr[1] != 'u') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
391 return 0;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
392 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
393
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
394 unsigned utf8len = 0;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
395 cxstring ustr1 = { str.ptr + 2, 4};
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
396 uint16_t utf16a, utf16b;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
397 if (!cx_strtou16_lc(ustr1, &utf16a, 16, "")) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
398 uint32_t codepoint;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
399 if (utf16a < 0xD800 || utf16a > 0xE000) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
400 // character is in the Basic Multilingual Plane
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
401 // and encoded as a single utf16 char
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
402 codepoint = utf16a;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
403 utf8len = codepoint_to_utf8(codepoint, utf8buf);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
404 } else if (utf16a >= 0xD800 && utf16a <= 0xDBFF) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
405 // character is encoded as a surrogate pair
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
406 // get next 6 bytes
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
407 if (str.length >= 12) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
408 if (str.ptr[6] == '\\' && str.ptr[7] == 'u') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
409 cxstring ustr2 = { str.ptr+8, 4 };
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
410 if (!cx_strtou16_lc(ustr2, &utf16b, 16, "")
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
411 && utf16b >= 0xDC00 && utf16b <= 0xDFFF) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
412 codepoint = utf16pair_to_codepoint(utf16a, utf16b);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
413 utf8len = codepoint_to_utf8(codepoint, utf8buf);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
414 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
415 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
416 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
417 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
418 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
419 return utf8len;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
420 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
421
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 static cxmutstr unescape_string(const CxAllocator *a, cxmutstr str) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
423 // note: this function expects that str contains the enclosing quotes!
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
424
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 cxmutstr result;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 result.length = 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 result.ptr = cxMalloc(a, str.length - 1);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 if (result.ptr == NULL) return result; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 bool u = false;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 for (size_t i = 1; i < str.length - 1; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 char c = str.ptr[i];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 if (u) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 u = false;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 if (c == 'n') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 c = '\n';
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
437 } else if (c == '"') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
438 c = '"';
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 } else if (c == 't') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 c = '\t';
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
441 } else if (c == 'r') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
442 c = '\r';
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
443 } else if (c == '\\') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
444 c = '\\';
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
445 } else if (c == '/') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
446 c = '/'; // always unescape, we don't need settings here
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
447 } else if (c == 'f') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
448 c = '\f';
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
449 } else if (c == 'b') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
450 c = '\b';
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
451 } else if (c == 'u') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
452 char utf8buf[4];
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
453 unsigned utf8len = unescape_unicode_string(
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
454 cx_strn(str.ptr + i - 1, str.length + 1 - i),
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
455 utf8buf
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
456 );
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
457 if(utf8len > 0) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
458 i += utf8len < 4 ? 4 : 10;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
459 // add all bytes from utf8buf except the last char
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
460 // to the result (last char will be added below)
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
461 utf8len--;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
462 c = utf8buf[utf8len];
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
463 for (unsigned x = 0; x < utf8len; x++) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
464 result.ptr[result.length++] = utf8buf[x];
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
465 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
466 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
467 // decoding failed, ignore the entire sequence
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
468 result.ptr[result.length++] = '\\';
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
469 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
470 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
471 // TODO: discuss the behavior for unrecognized escape sequences
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
472 // most parsers throw an error here - we just ignore it
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
473 result.ptr[result.length++] = '\\';
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 }
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
475
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476 result.ptr[result.length++] = c;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 if (c == '\\') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479 u = true;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 result.ptr[result.length++] = c;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 result.ptr[result.length] = 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 return result;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
490 static cxmutstr escape_string(cxmutstr str, bool escape_slash) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
491 // note: this function produces the string without enclosing quotes
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
492 // the reason is that we don't want to allocate memory just for that
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
493 CxBuffer buf = {0};
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
494
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
495 bool all_printable = true;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
496 for (size_t i = 0; i < str.length; i++) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
497 unsigned char c = str.ptr[i];
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
498 bool escape = c < 0x20 || c == '\\' || c == '"'
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
499 || (escape_slash && c == '/');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
500
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
501 if (all_printable && escape) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
502 size_t capa = str.length + 32;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
503 char *space = malloc(capa);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
504 if (space == NULL) return cx_mutstrn(NULL, 0);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
505 cxBufferInit(&buf, space, capa, NULL, CX_BUFFER_AUTO_EXTEND);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
506 cxBufferWrite(str.ptr, 1, i, &buf);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
507 all_printable = false;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
508 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
509 if (escape) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
510 cxBufferPut(&buf, '\\');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
511 if (c == '\"') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
512 cxBufferPut(&buf, '\"');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
513 } else if (c == '\n') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
514 cxBufferPut(&buf, 'n');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
515 } else if (c == '\t') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
516 cxBufferPut(&buf, 't');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
517 } else if (c == '\r') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
518 cxBufferPut(&buf, 'r');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
519 } else if (c == '\\') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
520 cxBufferPut(&buf, '\\');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
521 } else if (c == '/') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
522 cxBufferPut(&buf, '/');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
523 } else if (c == '\f') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
524 cxBufferPut(&buf, 'f');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
525 } else if (c == '\b') {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
526 cxBufferPut(&buf, 'b');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
527 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
528 char code[6];
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
529 snprintf(code, sizeof(code), "u%04x", (unsigned int) c);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
530 cxBufferPutString(&buf, code);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
531 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
532 } else if (!all_printable) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
533 cxBufferPut(&buf, c);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
534 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
535 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
536 if (!all_printable) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
537 str = cx_mutstrn(buf.space, buf.size);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
538 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
539 cxBufferDestroy(&buf);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
540 return str;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
541 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
542
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
543 static CxJsonValue* json_create_value(CxJson *json, CxJsonValueType type) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
544 CxJsonValue *v = cxCalloc(json->allocator, 1, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
545 if (v == NULL) return NULL; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
546
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
547 // initialize the value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
548 v->type = type;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
549 v->allocator = json->allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
550 if (type == CX_JSON_ARRAY) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
551 cx_array_initialize_a(json->allocator, v->value.array.array, 16);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
552 if (v->value.array.array == NULL) goto create_json_value_exit_error; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
553 } else if (type == CX_JSON_OBJECT) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
554 cx_array_initialize_a(json->allocator, v->value.object.values, 16);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
555 v->value.object.indices = cxCalloc(json->allocator, 16, sizeof(size_t));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
556 if (v->value.object.values == NULL ||
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
557 v->value.object.indices == NULL)
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
558 goto create_json_value_exit_error; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
559 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
560
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
561 // add the new value to a possible parent
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
562 if (json->vbuf_size > 0) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
563 CxJsonValue *parent = json->vbuf[json->vbuf_size - 1];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
564 assert(parent != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
565 if (parent->type == CX_JSON_ARRAY) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
566 CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
567 if (cx_array_simple_add_a(&value_realloc, parent->value.array.array, v)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
568 goto create_json_value_exit_error; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
569 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
570 } else if (parent->type == CX_JSON_OBJECT) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
571 // the member was already created after parsing the name
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
572 assert(json->uncompleted_member.name.ptr != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
573 json->uncompleted_member.value = v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
574 if (json_add_objvalue(parent, json->uncompleted_member)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
575 goto create_json_value_exit_error; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
576 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
577 json->uncompleted_member.name = (cxmutstr) {NULL, 0};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
578 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
579 assert(false); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
580 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
581 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
582
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
583 // add the new value to the stack, if it is an array or object
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
584 if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
585 CxArrayReallocator vbuf_realloc = cx_array_reallocator(NULL, json->vbuf_internal);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
586 if (cx_array_simple_add_a(&vbuf_realloc, json->vbuf, v)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
587 goto create_json_value_exit_error; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
588 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
589 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
590
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
591 // if currently no value is parsed, this is now the value of interest
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
592 if (json->parsed == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
593 json->parsed = v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
594 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
595
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
596 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
597 // LCOV_EXCL_START
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
598 create_json_value_exit_error:
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
599 cxJsonValueFree(v);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
600 return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
601 // LCOV_EXCL_STOP
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
602 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
603
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
604 #define JP_STATE_VALUE_BEGIN 0
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
605 #define JP_STATE_VALUE_END 10
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
606 #define JP_STATE_VALUE_BEGIN_OBJ 1
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
607 #define JP_STATE_OBJ_SEP_OR_CLOSE 11
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
608 #define JP_STATE_VALUE_BEGIN_AR 2
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
609 #define JP_STATE_ARRAY_SEP_OR_CLOSE 12
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
610 #define JP_STATE_OBJ_NAME_OR_CLOSE 5
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
611 #define JP_STATE_OBJ_NAME 6
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
612 #define JP_STATE_OBJ_COLON 7
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
613
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
614 void cxJsonInit(CxJson *json, const CxAllocator *allocator) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
615 if (allocator == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
616 allocator = cxDefaultAllocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
617 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
618
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
619 memset(json, 0, sizeof(CxJson));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
620 json->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
621
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
622 json->states = json->states_internal;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
623 json->states_capacity = cx_nmemb(json->states_internal);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
624 json->states[0] = JP_STATE_VALUE_BEGIN;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
625 json->states_size = 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
626
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
627 json->vbuf = json->vbuf_internal;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
628 json->vbuf_capacity = cx_nmemb(json->vbuf_internal);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
629 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
630
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
631 void cxJsonDestroy(CxJson *json) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
632 cxBufferDestroy(&json->buffer);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
633 if (json->states != json->states_internal) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
634 free(json->states);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
635 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
636 if (json->vbuf != json->vbuf_internal) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
637 free(json->vbuf);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
638 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
639 cxJsonValueFree(json->parsed);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
640 json->parsed = NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
641 if (json->uncompleted_member.name.ptr != NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
642 cx_strfree_a(json->allocator, &json->uncompleted_member.name);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
643 json->uncompleted_member = (CxJsonObjValue){{NULL, 0}, NULL};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
644 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
645 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
646
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
647 int cxJsonFilln(CxJson *json, const char *buf, size_t size) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
648 if (cxBufferEof(&json->buffer)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
649 // reinitialize the buffer
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
650 cxBufferDestroy(&json->buffer);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
651 cxBufferInit(&json->buffer, (char*) buf, size,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
652 NULL, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
653 json->buffer.size = size;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
654 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
655 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
656 return size != cxBufferAppend(buf, 1, size, &json->buffer);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
657 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
658 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
659
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
660 static void json_add_state(CxJson *json, int state) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
661 // we have guaranteed the necessary space with cx_array_simple_reserve()
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
662 // therefore, we can safely add the state in the simplest way possible
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
663 json->states[json->states_size++] = state;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
664 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
665
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
666 #define return_rec(code) \
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
667 token_destroy(&token); \
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
668 return code
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
669
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
670 static enum cx_json_status json_parse(CxJson *json) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
671 // Reserve a pointer for a possibly read value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
672 CxJsonValue *vbuf = NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
673
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
674 // grab the next token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
675 CxJsonToken token;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
676 {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
677 enum cx_json_status ret = token_parse_next(json, &token);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
678 if (ret != CX_JSON_NO_ERROR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
679 return ret;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
680 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
681 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
682
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
683 // pop the current state
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
684 assert(json->states_size > 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
685 int state = json->states[--json->states_size];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
686
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
687 // guarantee that at least two more states fit on the stack
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
688 CxArrayReallocator state_realloc = cx_array_reallocator(NULL, json->states_internal);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
689 if (cx_array_simple_reserve_a(&state_realloc, json->states, 2)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
690 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
691 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
692
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
693
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
694 // 0 JP_STATE_VALUE_BEGIN value begin
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
695 // 10 JP_STATE_VALUE_END expect value end
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
696 // 1 JP_STATE_VALUE_BEGIN_OBJ value begin (inside object)
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
697 // 11 JP_STATE_OBJ_SEP_OR_CLOSE object, expect separator, objclose
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
698 // 2 JP_STATE_VALUE_BEGIN_AR value begin (inside array)
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
699 // 12 JP_STATE_ARRAY_SEP_OR_CLOSE array, expect separator or arrayclose
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
700 // 5 JP_STATE_OBJ_NAME_OR_CLOSE object, expect name or objclose
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
701 // 6 JP_STATE_OBJ_NAME object, expect name
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
702 // 7 JP_STATE_OBJ_COLON object, expect ':'
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
703
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
704 if (state < 3) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
705 // push expected end state to the stack
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
706 json_add_state(json, 10 + state);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
707 switch (token.tokentype) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
708 case CX_JSON_TOKEN_BEGIN_ARRAY: {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
709 if (json_create_value(json, CX_JSON_ARRAY) == NULL) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
710 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
711 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
712 json_add_state(json, JP_STATE_VALUE_BEGIN_AR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
713 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
714 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
715 case CX_JSON_TOKEN_BEGIN_OBJECT: {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
716 if (json_create_value(json, CX_JSON_OBJECT) == NULL) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
717 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
718 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
719 json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
720 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
721 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
722 case CX_JSON_TOKEN_STRING: {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
723 if ((vbuf = json_create_value(json, CX_JSON_STRING)) == NULL) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
724 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
725 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
726 cxmutstr str = unescape_string(json->allocator, token.content);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
727 if (str.ptr == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
728 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
729 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
730 vbuf->value.string = str;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
731 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
732 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
733 case CX_JSON_TOKEN_INTEGER:
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
734 case CX_JSON_TOKEN_NUMBER: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
735 int type = token.tokentype == CX_JSON_TOKEN_INTEGER ? CX_JSON_INTEGER : CX_JSON_NUMBER;
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
736 if (NULL == (vbuf = json_create_value(json, type))) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
737 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
738 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
739 if (type == CX_JSON_INTEGER) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
740 if (cx_strtoi64(token.content, &vbuf->value.integer, 10)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
741 return_rec(CX_JSON_FORMAT_ERROR_NUMBER);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
742 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
743 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
744 if (cx_strtod(token.content, &vbuf->value.number)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
745 return_rec(CX_JSON_FORMAT_ERROR_NUMBER);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
746 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
747 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
748 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
749 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
750 case CX_JSON_TOKEN_LITERAL: {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
751 if ((vbuf = json_create_value(json, CX_JSON_LITERAL)) == NULL) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
752 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
753 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
754 if (0 == cx_strcmp(cx_strcast(token.content), cx_str("true"))) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
755 vbuf->value.literal = CX_JSON_TRUE;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
756 } else if (0 == cx_strcmp(cx_strcast(token.content), cx_str("false"))) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
757 vbuf->value.literal = CX_JSON_FALSE;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
758 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
759 vbuf->value.literal = CX_JSON_NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
760 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
761 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
762 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
763 default: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
764 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
765 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
766 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
767 } else if (state == JP_STATE_ARRAY_SEP_OR_CLOSE) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
768 // expect ',' or ']'
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
769 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
770 json_add_state(json, JP_STATE_VALUE_BEGIN_AR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
771 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
772 } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
773 // discard the array from the value buffer
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
774 json->vbuf_size--;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
775 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
776 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
777 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
778 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
779 } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
780 if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
781 // discard the obj from the value buffer
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
782 json->vbuf_size--;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
783 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
784 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
785 // expect string
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
786 if (token.tokentype != CX_JSON_TOKEN_STRING) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
787 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
788 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
789
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
790 // add new entry
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
791 cxmutstr name = unescape_string(json->allocator, token.content);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
792 if (name.ptr == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
793 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
794 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
795 assert(json->uncompleted_member.name.ptr == NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
796 json->uncompleted_member.name = name;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
797 assert(json->vbuf_size > 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
798
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
799 // next state
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
800 json_add_state(json, JP_STATE_OBJ_COLON);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
801 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
802 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
803 } else if (state == JP_STATE_OBJ_COLON) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
804 // expect ':'
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
805 if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
806 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
807 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
808 // next state
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
809 json_add_state(json, JP_STATE_VALUE_BEGIN_OBJ);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
810 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
811 } else if (state == JP_STATE_OBJ_SEP_OR_CLOSE) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
812 // expect ',' or '}'
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
813 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
814 json_add_state(json, JP_STATE_OBJ_NAME);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
815 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
816 } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
817 // discard the obj from the value buffer
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
818 json->vbuf_size--;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
819 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
820 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
821 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
822 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
823 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
824 // should be unreachable
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
825 assert(false);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
826 return_rec(-1);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
827 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
828 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
829
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
830 CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
831 // check if buffer has been filled
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
832 if (json->buffer.space == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
833 return CX_JSON_NULL_DATA;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
834 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
835
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
836 // initialize output value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
837 *value = &cx_json_value_nothing;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
838
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
839 // parse data
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
840 CxJsonStatus result;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
841 do {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
842 result = json_parse(json);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
843 if (result == CX_JSON_NO_ERROR && json->states_size == 1) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
844 // final state reached
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
845 assert(json->states[0] == JP_STATE_VALUE_END);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
846 assert(json->vbuf_size == 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
847
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
848 // write output value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
849 *value = json->parsed;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
850 json->parsed = NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
851
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
852 // re-initialize state machine
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
853 json->states[0] = JP_STATE_VALUE_BEGIN;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
854
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
855 return CX_JSON_NO_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
856 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
857 } while (result == CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
858
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
859 // the parser might think there is no data
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
860 // but when we did not reach the final state,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
861 // we know that there must be more to come
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
862 if (result == CX_JSON_NO_DATA && json->states_size > 1) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
863 return CX_JSON_INCOMPLETE_DATA;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
864 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
865
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
866 return result;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
867 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
868
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
869 void cxJsonValueFree(CxJsonValue *value) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
870 if (value == NULL || value->type == CX_JSON_NOTHING) return;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
871 switch (value->type) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
872 case CX_JSON_OBJECT: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
873 CxJsonObject obj = value->value.object;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
874 for (size_t i = 0; i < obj.values_size; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
875 cxJsonValueFree(obj.values[i].value);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
876 cx_strfree_a(value->allocator, &obj.values[i].name);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
877 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
878 cxFree(value->allocator, obj.values);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
879 cxFree(value->allocator, obj.indices);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
880 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
881 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
882 case CX_JSON_ARRAY: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
883 CxJsonArray array = value->value.array;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
884 for (size_t i = 0; i < array.array_size; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
885 cxJsonValueFree(array.array[i]);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
886 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
887 cxFree(value->allocator, array.array);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
888 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
889 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
890 case CX_JSON_STRING: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
891 cxFree(value->allocator, value->value.string.ptr);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
892 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
893 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
894 default: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
895 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
896 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
897 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
898 cxFree(value->allocator, value);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
899 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
900
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
901 CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
902 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
903 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
904 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
905 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
906 v->type = CX_JSON_OBJECT;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
907 cx_array_initialize_a(allocator, v->value.object.values, 16);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
908 if (v->value.object.values == NULL) { // LCOV_EXCL_START
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
909 cxFree(allocator, v);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
910 return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
911 // LCOV_EXCL_STOP
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
912 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
913 v->value.object.indices = cxCalloc(allocator, 16, sizeof(size_t));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
914 if (v->value.object.indices == NULL) { // LCOV_EXCL_START
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
915 cxFree(allocator, v->value.object.values);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
916 cxFree(allocator, v);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
917 return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
918 // LCOV_EXCL_STOP
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
919 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
920 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
921 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
922
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
923 CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
924 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
925 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
926 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
927 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
928 v->type = CX_JSON_ARRAY;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
929 cx_array_initialize_a(allocator, v->value.array.array, 16);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
930 if (v->value.array.array == NULL) { cxFree(allocator, v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
931 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
932 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
933
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
934 CxJsonValue* cxJsonCreateNumber(const CxAllocator* allocator, double num) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
935 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
936 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
937 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
938 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
939 v->type = CX_JSON_NUMBER;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
940 v->value.number = num;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
941 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
942 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
943
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
944 CxJsonValue* cxJsonCreateInteger(const CxAllocator* allocator, int64_t num) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
945 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
946 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
947 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
948 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
949 v->type = CX_JSON_INTEGER;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
950 v->value.integer = num;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
951 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
952 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
953
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
954 CxJsonValue* cxJsonCreateString(const CxAllocator* allocator, const char* str) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
955 return cxJsonCreateCxString(allocator, cx_str(str));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
956 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
957
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
958 CxJsonValue* cxJsonCreateCxString(const CxAllocator* allocator, cxstring str) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
959 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
960 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
961 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
962 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
963 v->type = CX_JSON_STRING;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
964 cxmutstr s = cx_strdup_a(allocator, str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
965 if (s.ptr == NULL) { cxFree(allocator, v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
966 v->value.string = s;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
967 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
968 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
969
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
970 CxJsonValue* cxJsonCreateLiteral(const CxAllocator* allocator, CxJsonLiteral lit) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
971 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
972 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
973 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
974 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
975 v->type = CX_JSON_LITERAL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
976 v->value.literal = lit;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
977 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
978 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
979
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
980 // LCOV_EXCL_START
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
981 // never called as long as malloc() does not return NULL
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
982 static void json_arr_free_temp(CxJsonValue** values, size_t count) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
983 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
984 if (values[i] == NULL) break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
985 cxJsonValueFree(values[i]);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
986 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
987 free(values);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
988 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
989 // LCOV_EXCL_STOP
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
990
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
991 int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
992 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
993 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
994 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
995 values[i] = cxJsonCreateNumber(arr->allocator, num[i]);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
996 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
997 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
998 int ret = cxJsonArrAddValues(arr, values, count);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
999 free(values);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1000 return ret;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1001 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1002
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1003 int cxJsonArrAddIntegers(CxJsonValue* arr, const int64_t* num, size_t count) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1004 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1005 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1006 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1007 values[i] = cxJsonCreateInteger(arr->allocator, num[i]);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1008 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1009 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1010 int ret = cxJsonArrAddValues(arr, values, count);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1011 free(values);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1012 return ret;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1013 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1014
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1015 int cxJsonArrAddStrings(CxJsonValue* arr, const char* const* str, size_t count) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1016 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1017 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1018 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1019 values[i] = cxJsonCreateString(arr->allocator, str[i]);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1020 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1021 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1022 int ret = cxJsonArrAddValues(arr, values, count);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1023 free(values);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1024 return ret;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1025 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1026
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1027 int cxJsonArrAddCxStrings(CxJsonValue* arr, const cxstring* str, size_t count) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1028 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1029 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1030 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1031 values[i] = cxJsonCreateCxString(arr->allocator, str[i]);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1032 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1033 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1034 int ret = cxJsonArrAddValues(arr, values, count);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1035 free(values);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1036 return ret;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1037 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1038
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1039 int cxJsonArrAddLiterals(CxJsonValue* arr, const CxJsonLiteral* lit, size_t count) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1040 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1041 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1042 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1043 values[i] = cxJsonCreateLiteral(arr->allocator, lit[i]);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1044 if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1045 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1046 int ret = cxJsonArrAddValues(arr, values, count);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1047 free(values);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1048 return ret;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1049 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1050
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1051 int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1052 CxArrayReallocator value_realloc = cx_array_reallocator(arr->allocator, NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1053 assert(arr->type == CX_JSON_ARRAY);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1054 return cx_array_simple_copy_a(&value_realloc,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1055 arr->value.array.array,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1056 arr->value.array.array_size,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1057 val, count
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1058 );
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1059 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1060
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1061 int cxJsonObjPut(CxJsonValue* obj, cxstring name, CxJsonValue* child) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1062 cxmutstr k = cx_strdup_a(obj->allocator, name);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1063 if (k.ptr == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1064 CxJsonObjValue kv = {k, child};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1065 if (json_add_objvalue(obj, kv)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1066 cx_strfree_a(obj->allocator, &k);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1067 return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1068 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1069 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1070 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1071 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1072
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1073 CxJsonValue* cxJsonObjPutObj(CxJsonValue* obj, cxstring name) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1074 CxJsonValue* v = cxJsonCreateObj(obj->allocator);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1075 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1076 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1077 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1078 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1079
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1080 CxJsonValue* cxJsonObjPutArr(CxJsonValue* obj, cxstring name) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1081 CxJsonValue* v = cxJsonCreateArr(obj->allocator);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1082 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1083 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1084 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1085 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1086
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1087 CxJsonValue* cxJsonObjPutNumber(CxJsonValue* obj, cxstring name, double num) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1088 CxJsonValue* v = cxJsonCreateNumber(obj->allocator, num);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1089 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1090 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1091 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1092 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1093
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1094 CxJsonValue* cxJsonObjPutInteger(CxJsonValue* obj, cxstring name, int64_t num) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1095 CxJsonValue* v = cxJsonCreateInteger(obj->allocator, num);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1096 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1097 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1098 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1099 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1100
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1101 CxJsonValue* cxJsonObjPutString(CxJsonValue* obj, cxstring name, const char* str) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1102 CxJsonValue* v = cxJsonCreateString(obj->allocator, str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1103 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1104 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1105 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1106 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1107
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1108 CxJsonValue* cxJsonObjPutCxString(CxJsonValue* obj, cxstring name, cxstring str) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1109 CxJsonValue* v = cxJsonCreateCxString(obj->allocator, str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1110 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1111 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1112 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1113 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1114
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1115 CxJsonValue* cxJsonObjPutLiteral(CxJsonValue* obj, cxstring name, CxJsonLiteral lit) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1116 CxJsonValue* v = cxJsonCreateLiteral(obj->allocator, lit);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1117 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1118 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL;}
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1119 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1120 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1121
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1122 CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1123 if (index >= value->value.array.array_size) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1124 return &cx_json_value_nothing;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1125 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1126 return value->value.array.array[index];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1127 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1128
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1129 CxIterator cxJsonArrIter(const CxJsonValue *value) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1130 return cxIteratorPtr(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1131 value->value.array.array,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1132 value->value.array.array_size
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1133 );
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1134 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1135
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1136 CxIterator cxJsonObjIter(const CxJsonValue *value) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1137 return cxIterator(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1138 value->value.object.values,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1139 sizeof(CxJsonObjValue),
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1140 value->value.object.values_size
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1141 );
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1142 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1143
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1144 CxJsonValue *cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1145 CxJsonObjValue *member = json_find_objvalue(value, name);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1146 if (member == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1147 return &cx_json_value_nothing;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1148 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1149 return member->value;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1150 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1151 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1152
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1153 CxJsonWriter cxJsonWriterCompact(void) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1154 return (CxJsonWriter) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1155 false,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1156 true,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1157 6,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1158 false,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1159 4,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1160 false
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1161 };
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1162 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1163
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1164 CxJsonWriter cxJsonWriterPretty(bool use_spaces) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1165 return (CxJsonWriter) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1166 true,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1167 true,
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1168 6,
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1169 use_spaces,
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1170 4,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1171 false
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1172 };
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1173 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1174
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1175 static int cx_json_writer_indent(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1176 void *target,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1177 cx_write_func wfunc,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1178 const CxJsonWriter *settings,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1179 unsigned int depth
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1180 ) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1181 if (depth == 0) return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1182
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1183 // determine the width and characters to use
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1184 const char* indent; // for 32 prepared chars
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1185 size_t width = depth;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1186 if (settings->indent_space) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1187 if (settings->indent == 0) return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1188 width *= settings->indent;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1189 indent = " ";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1190 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1191 indent = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1192 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1193
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1194 // calculate the number of write calls and write
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1195 size_t full = width / 32;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1196 size_t remaining = width % 32;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1197 for (size_t i = 0; i < full; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1198 if (32 != wfunc(indent, 1, 32, target)) return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1199 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1200 if (remaining != wfunc(indent, 1, remaining, target)) return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1201
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1202 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1203 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1204
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1205
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1206 int cx_json_write_rec(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1207 void *target,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1208 const CxJsonValue *value,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1209 cx_write_func wfunc,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1210 const CxJsonWriter *settings,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1211 unsigned int depth
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1212 ) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1213 // keep track of written items
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1214 // the idea is to reduce the number of jumps for error checking
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1215 size_t actual = 0, expected = 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1216
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1217 // small buffer for number to string conversions
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1218 char numbuf[40];
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1219
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1220 // recursively write the values
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1221 switch (value->type) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1222 case CX_JSON_OBJECT: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1223 const char *begin_obj = "{\n";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1224 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1225 actual += wfunc(begin_obj, 1, 2, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1226 expected += 2;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1227 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1228 actual += wfunc(begin_obj, 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1229 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1230 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1231 depth++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1232 size_t elem_count = value->value.object.values_size;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1233 for (size_t look_idx = 0; look_idx < elem_count; look_idx++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1234 // get the member either via index array or directly
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1235 size_t elem_idx = settings->sort_members
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1236 ? look_idx
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1237 : value->value.object.indices[look_idx];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1238 CxJsonObjValue *member = &value->value.object.values[elem_idx];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1239 if (settings->sort_members) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1240 depth++;depth--;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1241 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1242
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1243 // possible indentation
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1244 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1245 if (cx_json_writer_indent(target, wfunc, settings, depth)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1246 return 1; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1247 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1248 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1249
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1250 // the name
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1251 actual += wfunc("\"", 1, 1, target);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1252 cxmutstr name = escape_string(member->name, settings->escape_slash);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1253 actual += wfunc(name.ptr, 1, name.length, target);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1254 if (name.ptr != member->name.ptr) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1255 cx_strfree(&name);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1256 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1257 actual += wfunc("\"", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1258 const char *obj_name_sep = ": ";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1259 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1260 actual += wfunc(obj_name_sep, 1, 2, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1261 expected += 4 + member->name.length;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1262 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1263 actual += wfunc(obj_name_sep, 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1264 expected += 3 + member->name.length;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1265 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1266
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1267 // the value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1268 if (cx_json_write_rec(target, member->value, wfunc, settings, depth)) return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1269
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1270 // end of object-value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1271 if (look_idx < elem_count - 1) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1272 const char *obj_value_sep = ",\n";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1273 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1274 actual += wfunc(obj_value_sep, 1, 2, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1275 expected += 2;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1276 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1277 actual += wfunc(obj_value_sep, 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1278 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1279 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1280 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1281 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1282 actual += wfunc("\n", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1283 expected ++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1284 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1285 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1286 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1287 depth--;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1288 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1289 if (cx_json_writer_indent(target, wfunc, settings, depth)) return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1290 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1291 actual += wfunc("}", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1292 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1293 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1294 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1295 case CX_JSON_ARRAY: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1296 actual += wfunc("[", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1297 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1298 CxIterator iter = cxJsonArrIter(value);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1299 cx_foreach(CxJsonValue*, element, iter) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1300 if (cx_json_write_rec(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1301 target, element,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1302 wfunc, settings, depth)
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1303 ) return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1304
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1305 if (iter.index < iter.elem_count - 1) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1306 const char *arr_value_sep = ", ";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1307 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1308 actual += wfunc(arr_value_sep, 1, 2, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1309 expected += 2;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1310 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1311 actual += wfunc(arr_value_sep, 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1312 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1313 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1314 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1315 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1316 actual += wfunc("]", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1317 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1318 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1319 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1320 case CX_JSON_STRING: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1321 actual += wfunc("\"", 1, 1, target);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1322 cxmutstr str = escape_string(value->value.string, settings->escape_slash);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1323 actual += wfunc(str.ptr, 1, str.length, target);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1324 if (str.ptr != value->value.string.ptr) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1325 cx_strfree(&str);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1326 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1327 actual += wfunc("\"", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1328 expected += 2 + value->value.string.length;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1329 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1330 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1331 case CX_JSON_NUMBER: {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1332 int precision = settings->frac_max_digits;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1333 // because of the way how %g is defined, we need to
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1334 // double the precision and truncate ourselves
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1335 precision = 1 + (precision > 15 ? 30 : 2 * precision);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1336 snprintf(numbuf, 40, "%.*g", precision, value->value.number);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1337 char *dot, *exp;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1338 unsigned char max_digits;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1339 // find the decimal separator and hope that it's one of . or ,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1340 dot = strchr(numbuf, '.');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1341 if (dot == NULL) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1342 dot = strchr(numbuf, ',');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1343 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1344 if (dot == NULL) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1345 // no decimal separator found
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1346 // output everything until a possible exponent
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1347 max_digits = 30;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1348 dot = numbuf;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1349 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1350 // found a decimal separator
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1351 // output everything until the separator
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1352 // and set max digits to what the settings say
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1353 size_t len = dot - numbuf;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1354 actual += wfunc(numbuf, 1, len, target);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1355 expected += len;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1356 max_digits = settings->frac_max_digits;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1357 if (max_digits > 15) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1358 max_digits = 15;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1359 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1360 // locale independent separator
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1361 if (max_digits > 0) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1362 actual += wfunc(".", 1, 1, target);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1363 expected++;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1364 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1365 dot++;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1366 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1367 // find the exponent
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1368 exp = strchr(dot, 'e');
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1369 if (exp == NULL) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1370 // no exponent - output the rest
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1371 if (max_digits > 0) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1372 size_t len = strlen(dot);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1373 if (len > max_digits) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1374 len = max_digits;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1375 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1376 actual += wfunc(dot, 1, len, target);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1377 expected += len;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1378 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1379 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1380 // exponent found - truncate the frac digits
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1381 // and then output the rest
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1382 if (max_digits > 0) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1383 size_t len = exp - dot - 1;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1384 if (len > max_digits) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1385 len = max_digits;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1386 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1387 actual += wfunc(dot, 1, len, target);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1388 expected += len;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1389 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1390 actual += wfunc("e", 1, 1, target);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1391 expected++;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1392 exp++;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1393 size_t len = strlen(exp);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1394 actual += wfunc(exp, 1, len, target);
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1395 expected += len;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1396 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1397 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1398 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1399 case CX_JSON_INTEGER: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1400 snprintf(numbuf, 32, "%" PRIi64, value->value.integer);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1401 size_t len = strlen(numbuf);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1402 actual += wfunc(numbuf, 1, len, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1403 expected += len;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1404 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1405 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1406 case CX_JSON_LITERAL: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1407 if (value->value.literal == CX_JSON_TRUE) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1408 actual += wfunc("true", 1, 4, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1409 expected += 4;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1410 } else if (value->value.literal == CX_JSON_FALSE) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1411 actual += wfunc("false", 1, 5, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1412 expected += 5;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1413 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1414 actual += wfunc("null", 1, 4, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1415 expected += 4;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1416 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1417 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1418 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1419 case CX_JSON_NOTHING: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1420 // deliberately supported as an empty string!
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1421 // users might want to just write the result
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1422 // of a get operation without testing the value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1423 // and therefore this should not blow up
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1424 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1425 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1426 default: assert(false); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1427 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1428
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1429 return expected != actual;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1430 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1431
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1432 int cxJsonWrite(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1433 void *target,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1434 const CxJsonValue *value,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1435 cx_write_func wfunc,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1436 const CxJsonWriter *settings
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1437 ) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1438 assert(target != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1439 assert(value != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1440 assert(wfunc != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1441
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1442 CxJsonWriter writer_default = cxJsonWriterCompact();
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1443 if (settings == NULL) {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1444 settings = &writer_default;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1445 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1446 return cx_json_write_rec(target, value, wfunc, settings, 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1447 }

mercurial