ucx/json.c

Thu, 18 Dec 2025 17:50:15 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 18 Dec 2025 17:50:15 +0100
changeset 1016
ccde46662db7
parent 995
2f811eae2424
permissions
-rw-r--r--

update ucx

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"
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
30 #include "cx/kv_list.h"
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <string.h>
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <assert.h>
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <stdio.h>
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <inttypes.h>
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
36 #include <ctype.h>
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 /*
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 * RFC 8259
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 * https://tools.ietf.org/html/rfc8259
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 */
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 CxJsonValue cx_json_value_nothing = {.type = CX_JSON_NOTHING};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 static void token_destroy(CxJsonToken *token) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 if (token->allocated) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 cx_strfree(&token->content);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
48 token->allocated = false;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 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
53 if (pos >= length) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 int ok = 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 for (size_t i = pos; i < length; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 char c = content[i];
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
60 if (isdigit((unsigned char)c)) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 ok = 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 } else if (i == pos) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 if (!(c == '+' || c == '-')) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 return 0;
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 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 return ok;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 static CxJsonTokenType token_numbertype(const char *content, size_t length) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 if (length == 0) return CX_JSON_TOKEN_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
77 if (content[0] != '-' && !isdigit((unsigned char)content[0])) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 return CX_JSON_TOKEN_ERROR;
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 CxJsonTokenType type = CX_JSON_TOKEN_INTEGER;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 for (size_t i = 1; i < length; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 if (content[i] == '.') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 if (type == CX_JSON_TOKEN_NUMBER) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 return CX_JSON_TOKEN_ERROR; // more than one decimal separator
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 type = CX_JSON_TOKEN_NUMBER;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 } else if (content[i] == 'e' || content[i] == 'E') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 return num_isexp(content, length, i + 1) ? CX_JSON_TOKEN_NUMBER : CX_JSON_TOKEN_ERROR;
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
90 } else if (!isdigit((unsigned char)content[i])) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 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
92 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 return type;
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 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
99 cxmutstr str = cx_mutstrn(json->buffer.space + start, end - start);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 bool allocated = false;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 if (json->uncompleted.tokentype != CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 allocated = true;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 str = cx_strcat_m(json->uncompleted.content, 1, str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 if (str.ptr == NULL) { // LCOV_EXCL_START
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 return (CxJsonToken){CX_JSON_NO_TOKEN, false, {NULL, 0}};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 } // LCOV_EXCL_STOP
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 json->uncompleted = (CxJsonToken){0};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 CxJsonTokenType ttype;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 if (isstring) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 ttype = CX_JSON_TOKEN_STRING;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 cxstring s = cx_strcast(str);
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
114 if (!cx_strcmp(s, "true") || !cx_strcmp(s, "false")
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
115 || !cx_strcmp(s, "null")) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 ttype = CX_JSON_TOKEN_LITERAL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 ttype = token_numbertype(str.ptr, str.length);
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 if (ttype == CX_JSON_TOKEN_ERROR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 if (allocated) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 cx_strfree(&str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 return (CxJsonToken){CX_JSON_TOKEN_ERROR, false, {NULL, 0}};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 return (CxJsonToken){ttype, allocated, str};
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 static CxJsonTokenType char2ttype(char c) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 switch (c) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 case '[': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 return CX_JSON_TOKEN_BEGIN_ARRAY;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 case '{': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 return CX_JSON_TOKEN_BEGIN_OBJECT;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 case ']': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 return CX_JSON_TOKEN_END_ARRAY;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 case '}': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 return CX_JSON_TOKEN_END_OBJECT;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 case ':': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 return CX_JSON_TOKEN_NAME_SEPARATOR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 case ',': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 return CX_JSON_TOKEN_VALUE_SEPARATOR;
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 case '"': {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 return CX_JSON_TOKEN_STRING;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 default: {
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
154 if (isspace((unsigned char)c)) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 return CX_JSON_TOKEN_SPACE;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 }
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 return CX_JSON_NO_TOKEN;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 }
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 static enum cx_json_status token_parse_next(CxJson *json, CxJsonToken *result) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 // check if there is data in the buffer
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 if (cxBufferEof(&json->buffer)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 return json->uncompleted.tokentype == CX_JSON_NO_TOKEN ?
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 CX_JSON_NO_DATA : CX_JSON_INCOMPLETE_DATA;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 // current token type and start index
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 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
171 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
172
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
173 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
174 && json->uncompleted.content.ptr[json->uncompleted.content.length-1] == '\\';
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 for (size_t i = json->buffer.pos; i < json->buffer.size; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 char c = json->buffer.space[i];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 if (ttype != CX_JSON_TOKEN_STRING) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 // currently non-string token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 CxJsonTokenType ctype = char2ttype(c); // start of new token?
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 if (ttype == CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 if (ctype == CX_JSON_TOKEN_SPACE) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 json->buffer.pos++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 continue;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 } else if (ctype == CX_JSON_TOKEN_STRING) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 // begin string
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 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
188 token_part_start = i;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 } else if (ctype != CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 // single-char token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 json->buffer.pos = i + 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 *result = (CxJsonToken){ctype, false, {NULL, 0}};
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 return CX_JSON_NO_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 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
196 token_part_start = i;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 // finish token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 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
201 *result = token_create(json, false, token_part_start, i);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 if (result->tokentype == CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 if (result->tokentype == CX_JSON_TOKEN_ERROR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 return CX_JSON_FORMAT_ERROR_NUMBER;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 json->buffer.pos = i;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 return CX_JSON_NO_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 // 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
214 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
215 escape_end_of_string = false;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 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
218 *result = token_create(json, true, token_part_start, i + 1);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 if (result->tokentype == CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
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 json->buffer.pos = i + 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 return CX_JSON_NO_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 } 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
225 escape_end_of_string = true;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 }
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 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
231 if (ttype == CX_JSON_NO_TOKEN) {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
232 return CX_JSON_NO_DATA;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
233 } else {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 // 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
235 size_t uncompleted_len = json->buffer.size - token_part_start;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 if (json->uncompleted.tokentype == CX_JSON_NO_TOKEN) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 // current token is uncompleted
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 // save current token content
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 CxJsonToken uncompleted = {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 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
241 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
242 };
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 if (uncompleted.content.ptr == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
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 json->uncompleted = uncompleted;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 // previously we also had an uncompleted token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 // combine the uncompleted token with the current token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 assert(json->uncompleted.allocated);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 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
252 cx_strn(json->buffer.space + token_part_start, uncompleted_len));
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 if (str.ptr == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 json->uncompleted.content = str;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 // advance the buffer position - we saved the stuff in the uncompleted token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 json->buffer.pos += uncompleted_len;
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
260 return CX_JSON_INCOMPLETE_DATA;
440
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 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263
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 // 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
265 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
266 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
267 *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
268 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
269 } 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
270 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
271 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
272 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
273 } 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
274 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
275 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
276 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
277 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
278 } 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
279 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
280 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
281 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
282 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
283 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
284 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
285
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
286 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
287 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
288
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
289 // 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
290 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
291 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
292 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
293
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
294 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
295 // 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
296 // 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
297
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
298 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
299 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
300 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
301
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
302 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
303 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
304 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
305 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
306 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
307 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
308 // 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
309 // 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
310 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
311 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
312 } 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
313 // 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
314 // 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
315 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
316 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
317 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
318 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
319 && 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
320 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
321 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
322 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
323 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
324 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
325 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
326 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
327 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
328 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
329
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 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
331 // 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
332
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 cxmutstr result;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 result.length = 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 result.ptr = cxMalloc(a, str.length - 1);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 if (result.ptr == NULL) return result; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 bool u = false;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 for (size_t i = 1; i < str.length - 1; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 char c = str.ptr[i];
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 if (u) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 u = false;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 if (c == 'n') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 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
345 } 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
346 c = '"';
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 } else if (c == 't') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 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
349 } 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
350 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
351 } 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
352 c = '\\';
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
353 } 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
354 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
355 } 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
356 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
357 } 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
358 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
359 } 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
360 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
361 unsigned utf8len = unescape_unicode_string(
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
362 cx_strn(str.ptr + i - 1, str.length - 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
363 utf8buf
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
364 );
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
365 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
366 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
367 // 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
368 // 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
369 utf8len--;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
370 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
371 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
372 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
373 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
374 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
375 // 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
376 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
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 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
379 // 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
380 // most parsers throw an error here - we just ignore it
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
381 result.ptr[result.length++] = '\\'; // LCOV_EXCL_LINE
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 }
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
383
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 result.ptr[result.length++] = c;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 if (c == '\\') {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 u = true;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 result.ptr[result.length++] = c;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 result.ptr[result.length] = 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 return result;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
398 static cxmutstr escape_string(cxstring str, bool escape_slash) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
399 // 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
400 // 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
401 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
402
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
403 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
404 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
405 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
406 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
407 || (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
408
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
409 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
410 size_t capa = str.length + 32;
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
411 char *space = cxMallocDefault(capa);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
412 if (space == NULL) return cx_mutstrn(NULL, 0);
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
413 cxBufferInit(&buf, NULL, space, capa, CX_BUFFER_AUTO_EXTEND);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
414 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
415 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
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 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
418 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
419 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
420 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
421 } 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
422 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
423 } 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
424 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
425 } 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
426 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
427 } 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
428 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
429 } 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
430 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
431 } 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
432 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
433 } 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
434 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
435 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
436 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
437 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
438 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
439 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
440 } 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
441 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
442 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
443 }
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
444 cxmutstr ret;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
445 if (all_printable) {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
446 // don't copy the string when we don't need to escape anything
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
447 ret = cx_mutstrn((char*)str.ptr, str.length);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
448 } else {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
449 ret = cx_mutstrn(buf.space, buf.size);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
450 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
451 cxBufferDestroy(&buf);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
452 return ret;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
453 }
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
454
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
455 static CxJsonObject json_create_object_map(const CxAllocator *allocator) {
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
456 CxMap *map = cxKvListCreateAsMap(allocator, CX_STORE_POINTERS);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
457 if (map == NULL) return NULL; // LCOV_EXCL_LINE
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
458 cxSetCompareFunc(map, cxJsonCompare);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
459 cxSetDestructor(map, cxJsonValueFree);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
460 return map;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
461 }
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
462
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
463 static void json_free_object_map(CxJsonObject obj) {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
464 cxMapFree(obj);
471
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
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
467 static CxJsonValue* json_create_value(CxJson *json, CxJsonValueType type) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 CxJsonValue *v = cxCalloc(json->allocator, 1, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 if (v == NULL) return NULL; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 // initialize the value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 v->type = type;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 v->allocator = json->allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 if (type == CX_JSON_ARRAY) {
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
475 if (cx_array_init_a(json->allocator, v->array, 16)) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
476 goto create_json_value_exit_error; // LCOV_EXCL_LINE
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
477 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 } else if (type == CX_JSON_OBJECT) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
479 v->object = json_create_object_map(json->allocator);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
480 if (v->object == NULL) goto create_json_value_exit_error; // LCOV_EXCL_LINE
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 }
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 // add the new value to a possible parent
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
484 if (json->vbuf.size > 0) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
485 CxJsonValue *parent = json->vbuf.data[json->vbuf.size - 1];
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 assert(parent != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 if (parent->type == CX_JSON_ARRAY) {
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
488 if (cx_array_add_a(json->allocator, parent->array, &v)) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489 goto create_json_value_exit_error; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 } else if (parent->type == CX_JSON_OBJECT) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492 // the member was already created after parsing the name
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
493 // store the pointer of the uncompleted value in the map
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
494 assert(json->uncompleted_member_name.ptr != NULL);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
495 if (cxMapPut(parent->object, json->uncompleted_member_name, v)) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 goto create_json_value_exit_error; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497 }
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
498 cx_strfree_a(json->allocator, &json->uncompleted_member_name);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500 assert(false); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
501 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504 // 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
505 if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) {
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
506 if (json->vbuf.size >= json->vbuf.capacity) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
507 int alloc_error;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
508 if (json->vbuf.data == json->vbuf_internal) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
509 alloc_error = cx_array_copy_to_new(json->vbuf, json->vbuf.size+1);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
510 } else {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
511 alloc_error = cx_array_reserve(json->vbuf, json->vbuf.size+1);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
512 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
513 if (alloc_error) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
514 goto create_json_value_exit_error; // LCOV_EXCL_LINE
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
515 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
516 }
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
517 json->vbuf.data[json->vbuf.size] = v;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
518 json->vbuf.size++;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
520
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521 // 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
522 if (json->parsed == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 json->parsed = v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
524 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
525
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
526 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
527 // LCOV_EXCL_START
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528 create_json_value_exit_error:
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
529 cxJsonValueFree(v);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530 return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
531 // LCOV_EXCL_STOP
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
532 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
533
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
534 #define JP_STATE_VALUE_BEGIN 0
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
535 #define JP_STATE_VALUE_END 10
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
536 #define JP_STATE_VALUE_BEGIN_OBJ 1
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
537 #define JP_STATE_OBJ_SEP_OR_CLOSE 11
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
538 #define JP_STATE_VALUE_BEGIN_AR 2
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
539 #define JP_STATE_ARRAY_SEP_OR_CLOSE 12
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
540 #define JP_STATE_OBJ_NAME_OR_CLOSE 5
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
541 #define JP_STATE_OBJ_NAME 6
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
542 #define JP_STATE_OBJ_COLON 7
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
543
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
544 void cxJsonInit(CxJson *json, const CxAllocator *allocator) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
545 if (allocator == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
546 allocator = cxDefaultAllocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
547 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
548
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
549 memset(json, 0, sizeof(CxJson));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
550 json->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
551
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
552 cx_array_init_fixed(json->states, json->states_internal, 1);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
553 json->states.data[0] = JP_STATE_VALUE_BEGIN;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
554 cx_array_init_fixed(json->vbuf, json->vbuf_internal, 0);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
555 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
556
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
557 void cxJsonDestroy(CxJson *json) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
558 cxBufferDestroy(&json->buffer);
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
559 if (json->states.data != json->states_internal) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
560 cx_array_free(json->states);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
561 }
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
562 if (json->vbuf.data != json->vbuf_internal) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
563 cx_array_free(json->vbuf);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
564 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
565 cxJsonValueFree(json->parsed);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
566 json->parsed = NULL;
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
567 token_destroy(&json->uncompleted);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
568 cx_strfree_a(json->allocator, &json->uncompleted_member_name);
440
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
870
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
571 void cxJsonReset(CxJson *json) {
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
572 const CxAllocator *allocator = json->allocator;
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
573 cxJsonDestroy(json);
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
574 cxJsonInit(json, allocator);
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
575 }
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
576
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
577 int cxJsonFilln(CxJson *json, const char *buf, size_t size) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
578 if (cxBufferEof(&json->buffer)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
579 // reinitialize the buffer
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
580 cxBufferDestroy(&json->buffer);
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
581 if (buf == NULL) buf = ""; // buffer must not be initialized with NULL
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
582 cxBufferInit(&json->buffer, NULL, (char*) buf,
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
583 size, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
584 json->buffer.size = size;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
585 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
586 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
587 return size != cxBufferAppend(buf, 1, size, &json->buffer);
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 static void json_add_state(CxJson *json, int state) {
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
592 // we have guaranteed the necessary space
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
593 // therefore, we can safely add the state in the simplest way possible
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
594 json->states.data[json->states.size++] = state;
440
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
597 #define return_rec(code) \
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
598 token_destroy(&token); \
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
599 return code
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
600
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
601 static enum cx_json_status json_parse(CxJson *json) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
602 // Reserve a pointer for a possibly read value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
603 CxJsonValue *vbuf = NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
604
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
605 // grab the next token
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
606 CxJsonToken token;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
607 {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
608 enum cx_json_status ret = token_parse_next(json, &token);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
609 if (ret != CX_JSON_NO_ERROR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
610 return ret;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
611 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
612 }
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 // pop the current state
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
615 assert(json->states.size > 0);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
616 int state = json->states.data[--json->states.size];
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
617
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
618 // guarantee that at least two more states fit into the array
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
619 const size_t required_states_depth = json->states.size + 2;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
620 if (required_states_depth >= json->states.capacity) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
621 int alloc_error;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
622 if (json->states.data == json->states_internal) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
623 alloc_error = cx_array_copy_to_new(json->states, required_states_depth);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
624 } else {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
625 alloc_error = cx_array_reserve(json->states, required_states_depth);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
626 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
627 if (alloc_error) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
628 return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
629 }
440
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
632
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
633 // 0 JP_STATE_VALUE_BEGIN value begin
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
634 // 10 JP_STATE_VALUE_END expect value end
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
635 // 1 JP_STATE_VALUE_BEGIN_OBJ value begin (inside object)
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
636 // 11 JP_STATE_OBJ_SEP_OR_CLOSE object, expect separator, objclose
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
637 // 2 JP_STATE_VALUE_BEGIN_AR value begin (inside array)
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
638 // 12 JP_STATE_ARRAY_SEP_OR_CLOSE array, expect separator or arrayclose
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
639 // 5 JP_STATE_OBJ_NAME_OR_CLOSE object, expect name or objclose
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
640 // 6 JP_STATE_OBJ_NAME object, expect name
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
641 // 7 JP_STATE_OBJ_COLON object, expect ':'
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
642
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
643 if (state < 3) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
644 // push expected end state to the stack
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
645 json_add_state(json, 10 + state);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
646 switch (token.tokentype) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
647 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
648 if (json_create_value(json, CX_JSON_ARRAY) == NULL) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
649 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
650 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
651 json_add_state(json, JP_STATE_VALUE_BEGIN_AR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
652 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
653 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
654 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
655 if (json_create_value(json, CX_JSON_OBJECT) == NULL) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
656 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
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 json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
659 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
660 }
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
661 case CX_JSON_TOKEN_END_ARRAY: {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
662 if (state == JP_STATE_VALUE_BEGIN_AR) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
663 // discard the array from the value buffer
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
664 json->vbuf.size--;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
665 json->states.size--;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
666 return_rec(CX_JSON_NO_ERROR);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
667 } else {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
668 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
669 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
670 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
671 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
672 if ((vbuf = json_create_value(json, CX_JSON_STRING)) == NULL) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
673 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
674 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
675 cxmutstr str = unescape_string(json->allocator, token.content);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
676 if (str.ptr == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
677 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
678 }
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
679 vbuf->string = str;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
680 return_rec(CX_JSON_NO_ERROR);
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 case CX_JSON_TOKEN_INTEGER:
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
683 case CX_JSON_TOKEN_NUMBER: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
684 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
685 if (NULL == (vbuf = json_create_value(json, type))) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
686 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
687 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
688 if (type == CX_JSON_INTEGER) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
689 if (cx_strtoi64(token.content, &vbuf->integer, 10)) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
690 return_rec(CX_JSON_FORMAT_ERROR_NUMBER);
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 } else {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
693 if (cx_strtod(token.content, &vbuf->number)) {
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
694 // TODO: at the moment this is unreachable, because the tokenizer is already stricter than cx_strtod()
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
695 return_rec(CX_JSON_FORMAT_ERROR_NUMBER); // LCOV_EXCL_LINE
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
696 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
697 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
698 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
699 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
700 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
701 if ((vbuf = json_create_value(json, CX_JSON_LITERAL)) == NULL) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
702 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
703 }
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
704 if (0 == cx_strcmp(token.content, "true")) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
705 vbuf->literal = CX_JSON_TRUE;
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
706 } else if (0 == cx_strcmp(token.content, "false")) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
707 vbuf->literal = CX_JSON_FALSE;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
708 } else {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
709 vbuf->literal = CX_JSON_NULL;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
710 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
711 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
712 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
713 default: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
714 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
715 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
716 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
717 } else if (state == JP_STATE_ARRAY_SEP_OR_CLOSE) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
718 // expect ',' or ']'
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
719 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
720 json_add_state(json, JP_STATE_VALUE_BEGIN_AR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
721 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
722 } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
723 // discard the array from the value buffer
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
724 json->vbuf.size--;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
725 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
726 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
727 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
728 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
729 } 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
730 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
731 // discard the obj from the value buffer
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
732 json->vbuf.size--;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
733 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
734 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
735 // expect string
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
736 if (token.tokentype != CX_JSON_TOKEN_STRING) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
737 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
740 // add new entry
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
741 cxmutstr name = unescape_string(json->allocator, token.content);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
742 if (name.ptr == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
743 return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
744 }
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
745 assert(json->uncompleted_member_name.ptr == NULL);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
746 json->uncompleted_member_name = name;
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
747 assert(json->vbuf.size > 0);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
748
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
749 // next state
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
750 json_add_state(json, JP_STATE_OBJ_COLON);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
751 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
752 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
753 } else if (state == JP_STATE_OBJ_COLON) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
754 // expect ':'
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
755 if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
756 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
757 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
758 // next state
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
759 json_add_state(json, JP_STATE_VALUE_BEGIN_OBJ);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
760 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
761 } else if (state == JP_STATE_OBJ_SEP_OR_CLOSE) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
762 // expect ',' or '}'
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
763 if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
764 json_add_state(json, JP_STATE_OBJ_NAME);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
765 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
766 } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
767 // discard the obj from the value buffer
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
768 json->vbuf.size--;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
769 return_rec(CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
770 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
771 return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
772 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
773 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
774 // should be unreachable
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
775 assert(false);
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
776 return_rec(-1); // LCOV_EXCL_LINE
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
777 }
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
780 CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value) {
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
781 // initialize output value
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
782 *value = &cx_json_value_nothing;
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
783
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
784 // check if the buffer has been filled
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
785 if (json->buffer.space == NULL) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
786 return CX_JSON_NULL_DATA;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
787 }
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 // parse data
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
790 CxJsonStatus result;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
791 do {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
792 result = json_parse(json);
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
793 if (result == CX_JSON_NO_ERROR && json->states.size == 1) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
794 // final state reached
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
795 assert(json->states.data[0] == JP_STATE_VALUE_END);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
796 assert(json->vbuf.size == 0);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
797
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
798 // write output value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
799 *value = json->parsed;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
800 json->parsed = NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
801
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
802 // re-initialize state machine
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
803 json->states.data[0] = JP_STATE_VALUE_BEGIN;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
804
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
805 return CX_JSON_NO_ERROR;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
806 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
807 } while (result == CX_JSON_NO_ERROR);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
808
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
809 // the parser might think there is no data
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
810 // but when we did not reach the final state,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
811 // we know that there must be more to come
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
812 if (result == CX_JSON_NO_DATA && json->states.size > 1) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
813 return CX_JSON_INCOMPLETE_DATA;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
814 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
815
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
816 return result;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
817 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
818
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
819 CxJsonStatus cx_json_from_string(const CxAllocator *allocator,
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
820 cxstring str, CxJsonValue **value) {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
821 *value = &cx_json_value_nothing;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
822 CxJson parser;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
823 cxJsonInit(&parser, allocator);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
824 if (cxJsonFill(&parser, str)) {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
825 // LCOV_EXCL_START
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
826 cxJsonDestroy(&parser);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
827 return CX_JSON_BUFFER_ALLOC_FAILED;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
828 // LCOV_EXCL_STOP
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
829 }
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
830 CxJsonStatus status = cxJsonNext(&parser, value);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
831 // check if we consume the total string
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
832 CxJsonValue *chk_value = NULL;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
833 CxJsonStatus chk_status = CX_JSON_NO_DATA;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
834 if (status == CX_JSON_NO_ERROR) {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
835 chk_status = cxJsonNext(&parser, &chk_value);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
836 }
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
837 cxJsonDestroy(&parser);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
838 if (chk_status == CX_JSON_NO_DATA) {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
839 return status;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
840 } else {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
841 cxJsonValueFree(*value);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
842 // if chk_value is nothing, the free is harmless
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
843 cxJsonValueFree(chk_value);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
844 *value = &cx_json_value_nothing;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
845 return CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
846 }
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
847
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
848 }
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
849
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
850 void cxJsonValueFree(CxJsonValue *value) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
851 if (value == NULL || value->type == CX_JSON_NOTHING) return;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
852 switch (value->type) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
853 case CX_JSON_OBJECT: {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
854 json_free_object_map(value->object);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
855 break;
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 case CX_JSON_ARRAY: {
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
858 for (size_t i = 0; i < value->array.size; i++) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
859 cxJsonValueFree(value->array.data[i]);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
860 }
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
861 cx_array_free_a(value->allocator, value->array);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
862 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
863 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
864 case CX_JSON_STRING: {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
865 cxFree(value->allocator, value->string.ptr);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
866 break;
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 default: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
869 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
870 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
871 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
872 cxFree(value->allocator, value);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
873 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
874
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
875 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
876 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
877 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
878 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
879 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
880 v->type = CX_JSON_OBJECT;
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
881 v->object = json_create_object_map(allocator);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
882 if (v->object == NULL) { // LCOV_EXCL_START
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
883 cxFree(allocator, v);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
884 return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
885 // LCOV_EXCL_STOP
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 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
888 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
889
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
890 CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator, size_t capacity) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
891 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
892 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
893 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
894 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
895 v->type = CX_JSON_ARRAY;
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
896 if (capacity > 0) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
897 if (cx_array_init_a(allocator, v->array, capacity)) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
898 // LCOV_EXCL_START
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
899 cxFree(allocator, v);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
900 return NULL;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
901 // LCOV_EXCL_STOP
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
902 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
903 } else {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
904 v->array.data = NULL;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
905 v->array.size = v->array.capacity = 0;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
906 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
907 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
908 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
909
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
910 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
911 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
912 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
913 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
914 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
915 v->type = CX_JSON_NUMBER;
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
916 v->number = num;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
917 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
918 }
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 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
921 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
922 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
923 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
924 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
925 v->type = CX_JSON_INTEGER;
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
926 v->integer = num;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
927 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
928 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
929
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
930 CxJsonValue* cx_json_create_string(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
931 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
932 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
933 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
934 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
935 v->type = CX_JSON_STRING;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
936 cxmutstr s = cx_strdup_a(allocator, str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
937 if (s.ptr == NULL) { cxFree(allocator, v); return NULL; }
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
938 v->string = s;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
939 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
940 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
941
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
942 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
943 if (allocator == NULL) allocator = cxDefaultAllocator;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
944 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
945 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
946 v->allocator = allocator;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
947 v->type = CX_JSON_LITERAL;
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
948 v->literal = lit;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
949 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
950 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
951
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
952 // LCOV_EXCL_START
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
953 // 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
954 static void json_arr_free_temp(CxJsonValue** values, size_t count) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
955 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
956 if (values[i] == NULL) break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
957 cxJsonValueFree(values[i]);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
958 }
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
959 cxFreeDefault(values);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
960 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
961 // LCOV_EXCL_STOP
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
962
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
963 int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count) {
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
964 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
965 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
966 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
967 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
968 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
969 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
970 int ret = cxJsonArrAddValues(arr, values, count);
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
971 cxFreeDefault(values);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
972 return ret;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
973 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
974
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
975 int cxJsonArrAddIntegers(CxJsonValue* arr, const int64_t* num, size_t count) {
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
976 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
977 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
978 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
979 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
980 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
981 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
982 int ret = cxJsonArrAddValues(arr, values, count);
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
983 cxFreeDefault(values);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
984 return ret;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
985 }
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 int cxJsonArrAddStrings(CxJsonValue* arr, const char* const* str, size_t count) {
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
988 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
989 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
990 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
991 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
992 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
993 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
994 int ret = cxJsonArrAddValues(arr, values, count);
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
995 cxFreeDefault(values);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
996 return ret;
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
999 int cxJsonArrAddCxStrings(CxJsonValue* arr, const cxstring* str, size_t count) {
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
1000 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1001 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1002 for (size_t i = 0; i < count; i++) {
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1003 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
1004 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
1005 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1006 int ret = cxJsonArrAddValues(arr, values, count);
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
1007 cxFreeDefault(values);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1008 return ret;
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1011 int cxJsonArrAddLiterals(CxJsonValue* arr, const CxJsonLiteral* lit, size_t count) {
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
1012 CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1013 if (values == NULL) return -1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1014 for (size_t i = 0; i < count; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1015 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
1016 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
1017 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1018 int ret = cxJsonArrAddValues(arr, values, count);
629
0385a450c2a6 add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 471
diff changeset
1019 cxFreeDefault(values);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1020 return ret;
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1023 int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1024 assert(arr->type == CX_JSON_ARRAY);
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1025 return cx_array_add_array_a(arr->allocator, arr->array, val, count);
440
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
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1028 int cx_json_obj_put(CxJsonValue* obj, cxstring name, CxJsonValue* child) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1029 return cxMapPut(obj->object, name, child);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1030 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1031
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1032 CxJsonValue* cx_json_obj_put_obj(CxJsonValue* obj, cxstring name) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1033 CxJsonValue* v = cxJsonCreateObj(obj->allocator);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1034 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1035 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1036 return v;
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
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1039 CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name, size_t capacity) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1040 CxJsonValue* v = cxJsonCreateArr(obj->allocator, capacity);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1041 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1042 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1043 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1044 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1045
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1046 CxJsonValue* cx_json_obj_put_number(CxJsonValue* obj, cxstring name, double num) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1047 CxJsonValue* v = cxJsonCreateNumber(obj->allocator, num);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1048 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1049 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1050 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1051 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1052
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1053 CxJsonValue* cx_json_obj_put_integer(CxJsonValue* obj, cxstring name, int64_t num) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1054 CxJsonValue* v = cxJsonCreateInteger(obj->allocator, num);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1055 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1056 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1057 return v;
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
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1060 CxJsonValue* cx_json_obj_put_string(CxJsonValue* obj, cxstring name, cxstring str) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1061 CxJsonValue* v = cxJsonCreateString(obj->allocator, str);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1062 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1063 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1064 return v;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1065 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1066
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1067 CxJsonValue* cx_json_obj_put_literal(CxJsonValue* obj, cxstring name, CxJsonLiteral lit) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1068 CxJsonValue* v = cxJsonCreateLiteral(obj->allocator, lit);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1069 if (v == NULL) return NULL;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1070 if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL;}
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1071 return v;
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1074 CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index) {
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1075 if (index >= value->array.size) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1076 return &cx_json_value_nothing;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1077 }
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1078 return value->array.data[index];
440
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
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1081 CxJsonValue *cxJsonArrRemove(CxJsonValue *value, size_t index) {
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1082 if (index >= value->array.size) {
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1083 return NULL;
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1084 }
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1085 CxJsonValue *ret = value->array.data[index];
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1086 // TODO: replace with a low level cx_array_remove()
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1087 size_t count = value->array.size - index - 1;
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1088 if (count > 0) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1089 memmove(value->array.data + index, value->array.data + index + 1, count * sizeof(CxJsonValue*));
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1090 }
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1091 value->array.size--;
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1092 return ret;
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1093 }
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1094
870
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1095 char *cxJsonAsString(const CxJsonValue *value) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1096 return value->string.ptr;
870
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1097 }
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1098
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1099 cxstring cxJsonAsCxString(const CxJsonValue *value) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1100 return cx_strcast(value->string);
870
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1101 }
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1102
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1103 cxmutstr cxJsonAsCxMutStr(const CxJsonValue *value) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1104 return value->string;
870
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1105 }
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1106
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1107 double cxJsonAsDouble(const CxJsonValue *value) {
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1108 if (value->type == CX_JSON_INTEGER) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1109 return (double) value->integer;
870
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1110 } else {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1111 return value->number;
870
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1112 }
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1113 }
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1114
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1115 int64_t cxJsonAsInteger(const CxJsonValue *value) {
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1116 if (value->type == CX_JSON_INTEGER) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1117 return value->integer;
870
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1118 } else {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1119 return (int64_t) value->number;
870
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1120 }
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1121 }
e167cf006213 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 845
diff changeset
1122
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1123 CxIterator cxJsonArrIter(const CxJsonValue *value) {
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1124 return cx_array_iterator_ptr(value->array);
440
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
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1127 CxMapIterator cxJsonObjIter(const CxJsonValue *value) {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1128 return cxMapIterator(value->object);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1129 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1130
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1131 CxJsonValue *cx_json_obj_get(const CxJsonValue *value, cxstring name) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1132 CxJsonValue *v = cxMapGet(value->object, name);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1133 if (v == NULL) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1134 return &cx_json_value_nothing;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1135 } else {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1136 return v;
845
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1137 }
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1138 }
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1139
f3ab28ed22e5 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 629
diff changeset
1140 CxJsonValue *cx_json_obj_remove(CxJsonValue *value, cxstring name) {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1141 CxJsonValue *v = NULL;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1142 cxMapRemoveAndGet(value->object, name, &v);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1143 return v;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1144 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1145
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1146 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
1147 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
1148 false,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1149 6,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1150 false,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1151 4,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1152 false
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1153 };
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1154 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1155
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1156 CxJsonWriter cxJsonWriterPretty(bool use_spaces) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1157 return (CxJsonWriter) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1158 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
1159 6,
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1160 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
1161 4,
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1162 false
440
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 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1165
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1166 static int cx_json_writer_indent(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1167 void *target,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1168 cx_write_func wfunc,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1169 const CxJsonWriter *settings,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1170 unsigned int depth
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1171 ) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1172 if (depth == 0) return 0;
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 // determine the width and characters to use
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1175 const char* indent; // for 32 prepared chars
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1176 size_t width = depth;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1177 if (settings->indent_space) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1178 if (settings->indent == 0) return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1179 width *= settings->indent;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1180 indent = " ";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1181 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1182 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
1183 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1184
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1185 // calculate the number of write calls and write
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1186 size_t full = width / 32;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1187 size_t remaining = width % 32;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1188 for (size_t i = 0; i < full; i++) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1189 if (32 != wfunc(indent, 1, 32, target)) return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1190 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1191 if (remaining != wfunc(indent, 1, remaining, target)) return 1;
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 return 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1194 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1195
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1196
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1197 int cx_json_write_rec(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1198 void *target,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1199 const CxJsonValue *value,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1200 cx_write_func wfunc,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1201 const CxJsonWriter *settings,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1202 unsigned int depth
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 // keep track of written items
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1205 // the idea is to reduce the number of jumps for error checking
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1206 size_t actual = 0, expected = 0;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1207
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1208 // 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
1209 char numbuf[40];
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1210
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1211 // recursively write the values
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1212 switch (value->type) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1213 case CX_JSON_OBJECT: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1214 const char *begin_obj = "{\n";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1215 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1216 actual += wfunc(begin_obj, 1, 2, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1217 expected += 2;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1218 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1219 actual += wfunc(begin_obj, 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1220 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1221 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1222 depth++;
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1223 CxMapIterator member_iter = cxJsonObjIter(value);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1224 cx_foreach(const CxMapEntry *, member, member_iter) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1225 // possible indentation
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1226 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1227 if (cx_json_writer_indent(target, wfunc, settings, depth)) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1228 return 1; // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1229 }
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1232 // the name
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1233 actual += wfunc("\"", 1, 1, target);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1234 cxstring key = cx_strn(member->key->data, member->key->len);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1235 cxmutstr name = escape_string(key, settings->escape_slash);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1236 actual += wfunc(name.ptr, 1, name.length, target);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1237 actual += wfunc("\"", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1238 const char *obj_name_sep = ": ";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1239 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1240 actual += wfunc(obj_name_sep, 1, 2, target);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1241 expected += 4 + name.length;
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1242 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1243 actual += wfunc(obj_name_sep, 1, 1, target);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1244 expected += 3 + name.length;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1245 }
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1246 if (name.ptr != key.ptr) {
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1247 cx_strfree(&name);
440
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 value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1251 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
1252
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1253 // end of object-value
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1254 if (member_iter.index < member_iter.elem_count - 1) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1255 const char *obj_value_sep = ",\n";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1256 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1257 actual += wfunc(obj_value_sep, 1, 2, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1258 expected += 2;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1259 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1260 actual += wfunc(obj_value_sep, 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1261 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1262 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1263 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1264 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1265 actual += wfunc("\n", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1266 expected ++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1267 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1268 }
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 depth--;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1271 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1272 if (cx_json_writer_indent(target, wfunc, settings, depth)) return 1;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1273 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1274 actual += wfunc("}", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1275 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1276 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1277 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1278 case CX_JSON_ARRAY: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1279 actual += wfunc("[", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1280 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1281 CxIterator iter = cxJsonArrIter(value);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1282 cx_foreach(CxJsonValue*, element, iter) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1283 if (cx_json_write_rec(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1284 target, element,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1285 wfunc, settings, depth)
943
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1286 ) {
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1287 return 1; // LCOV_EXCL_LINE
9b5948aa5b90 update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 870
diff changeset
1288 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1289
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1290 if (iter.index < iter.elem_count - 1) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1291 const char *arr_value_sep = ", ";
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1292 if (settings->pretty) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1293 actual += wfunc(arr_value_sep, 1, 2, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1294 expected += 2;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1295 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1296 actual += wfunc(arr_value_sep, 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 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1299 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1300 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1301 actual += wfunc("]", 1, 1, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1302 expected++;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1303 break;
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 case CX_JSON_STRING: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1306 actual += wfunc("\"", 1, 1, target);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1307 cxmutstr str = escape_string(cx_strcast(value->string),
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1308 settings->escape_slash);
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1309 actual += wfunc(str.ptr, 1, str.length, target);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1310 actual += wfunc("\"", 1, 1, target);
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1311 expected += 2 + str.length;
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1312 if (str.ptr != value->string.ptr) {
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1313 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
1314 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1315 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1316 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1317 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
1318 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
1319 // 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
1320 // 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
1321 precision = 1 + (precision > 15 ? 30 : 2 * precision);
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1322 snprintf(numbuf, 40, "%.*g", precision, value->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
1323 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
1324 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
1325 // 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
1326 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
1327 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
1328 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
1329 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1330 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
1331 // 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
1332 // 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
1333 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
1334 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
1335 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1336 // 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
1337 // 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
1338 // 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
1339 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
1340 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
1341 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
1342 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
1343 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
1344 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
1345 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1346 // 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
1347 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
1348 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
1349 expected++;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1350 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1351 dot++;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1352 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1353 // 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
1354 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
1355 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
1356 // 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
1357 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
1358 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
1359 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
1360 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
1361 }
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(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
1363 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
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 } else {
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1366 // 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
1367 // 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
1368 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
1369 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
1370 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
1371 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
1372 }
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1373 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
1374 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
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("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
1377 expected++;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1378 exp++;
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1379 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
1380 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
1381 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
1382 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1383 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1384 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1385 case CX_JSON_INTEGER: {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1386 snprintf(numbuf, 32, "%" PRIi64, value->integer);
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1387 size_t len = strlen(numbuf);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1388 actual += wfunc(numbuf, 1, len, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1389 expected += len;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1390 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1391 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1392 case CX_JSON_LITERAL: {
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1393 if (value->literal == CX_JSON_TRUE) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1394 actual += wfunc("true", 1, 4, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1395 expected += 4;
992
f421aef8f865 remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 943
diff changeset
1396 } else if (value->literal == CX_JSON_FALSE) {
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1397 actual += wfunc("false", 1, 5, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1398 expected += 5;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1399 } else {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1400 actual += wfunc("null", 1, 4, target);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1401 expected += 4;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1402 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1403 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1404 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1405 case CX_JSON_NOTHING: {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1406 // deliberately supported as an empty string!
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1407 // users might want to just write the result
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1408 // of a get operation without testing the value
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1409 // and therefore this should not blow up
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1410 break;
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1411 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1412 default: assert(false); // LCOV_EXCL_LINE
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1413 }
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1414
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1415 return expected != actual;
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
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1418 int cxJsonWrite(
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1419 void *target,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1420 const CxJsonValue *value,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1421 cx_write_func wfunc,
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1422 const CxJsonWriter *settings
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1423 ) {
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1424 assert(target != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1425 assert(value != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1426 assert(wfunc != NULL);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1427
471
063a9f29098c ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 440
diff changeset
1428 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
1429 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
1430 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
1431 }
440
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1432 return cx_json_write_rec(target, value, wfunc, settings, 0);
7c4b9cba09ca update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1433 }
995
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1434
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1435 static cxmutstr cx_json_to_string(CxJsonValue *value, const CxAllocator *allocator, CxJsonWriter *writer) {
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1436 if (allocator == NULL) allocator = cxDefaultAllocator;
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1437 CxBuffer buffer;
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1438 if (cxBufferInit(&buffer, allocator, NULL, 128,
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1439 CX_BUFFER_AUTO_EXTEND | CX_BUFFER_DO_NOT_FREE)) {
995
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1440 return (cxmutstr){NULL, 0};
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1441 }
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1442 if (cx_json_write_rec(&buffer, value, cxBufferWriteFunc, writer, 0)
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1443 || cxBufferTerminate(&buffer)) {
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1444 // LCOV_EXCL_START
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1445 buffer.flags &= ~CX_BUFFER_DO_NOT_FREE;
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1446 cxBufferDestroy(&buffer);
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1447 return (cxmutstr){NULL, 0};
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1448 // LCOV_EXCL_STOP
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1449 } else {
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1450 cxmutstr str = cx_mutstrn(buffer.space, buffer.size);
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1451 cxBufferDestroy(&buffer);
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1452 return str;
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1453 }
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1454
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1455 }
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1456
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1457 cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value) {
995
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1458 CxJsonWriter writer = cxJsonWriterCompact();
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1459 return cx_json_to_string(value, allocator, &writer);
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1460 }
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1461
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1462 cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value) {
995
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1463 CxJsonWriter writer = cxJsonWriterPretty(true);
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1464 return cx_json_to_string(value, allocator, &writer);
2f811eae2424 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 992
diff changeset
1465 }
1016
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1466
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1467 int cxJsonCompare(const CxJsonValue *json, const CxJsonValue *other) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1468 if (json == other) return 0;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1469 if (json == NULL || other == NULL) return -1;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1470 if (json->type != other->type) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1471 if (!cxJsonIsNumber(json)) return -1;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1472 if (!cxJsonIsNumber(other)) return -1;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1473 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1474 switch (json->type) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1475 case CX_JSON_NOTHING:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1476 return 0;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1477 case CX_JSON_OBJECT:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1478 return cxMapCompare(json->object, other->object);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1479 case CX_JSON_ARRAY:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1480 if (json->array.size != other->array.size) return -1;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1481 for (size_t i = 0; i < json->array.size; i++) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1482 const int d = cxJsonCompare(json->array.data[i], other->array.data[i]);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1483 if (d != 0) return d;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1484 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1485 return 0;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1486 case CX_JSON_STRING:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1487 return cx_strcmp(json->string, other->string);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1488 case CX_JSON_INTEGER:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1489 if (other->type == CX_JSON_INTEGER) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1490 return cx_vcmp_int64(json->integer, other->integer);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1491 } else {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1492 return cx_vcmp_double(cxJsonAsDouble(json), other->number);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1493 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1494 case CX_JSON_NUMBER:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1495 return cx_vcmp_double(json->number, cxJsonAsDouble(other));
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1496 case CX_JSON_LITERAL:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1497 return json->literal == other->literal ? 0 : -1;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1498 default:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1499 // LCOV_EXCL_START
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1500 // unreachable
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1501 assert(false);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1502 return -1;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1503 // LCOV_EXCL_STOP
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1504 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1505 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1506
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1507 CxJsonValue* cxJsonClone(const CxJsonValue* value, const CxAllocator* allocator) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1508 return cx_json_clone_func(NULL, value, allocator, NULL);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1509 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1510
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1511 CxJsonValue* cx_json_clone_func(CxJsonValue* target, const CxJsonValue* source,
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1512 const CxAllocator* allocator, cx_attr_unused void *data) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1513 if (source == NULL || source->type == CX_JSON_NOTHING) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1514 return &cx_json_value_nothing;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1515 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1516 if (allocator == NULL) allocator = cxDefaultAllocator;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1517
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1518 #define return_value(v) { \
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1519 CxJsonValue *ret = v; \
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1520 if (target == NULL) { \
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1521 return ret; \
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1522 } else { \
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1523 *target = *ret; \
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1524 cxFree(allocator, ret); \
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1525 return target; \
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1526 } \
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1527 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1528
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1529 switch (source->type) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1530 case CX_JSON_OBJECT: {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1531 CxJsonValue *obj = cxJsonCreateObj(allocator);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1532 if (obj == NULL) return NULL; // LCOV_EXCL_LINE
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1533 if (cxMapClone(obj->object, source->object, cxJsonCloneFunc, allocator, NULL)) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1534 // LCOV_EXCL_START
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1535 cxJsonValueFree(obj);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1536 return NULL;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1537 // LCOV_EXCL_STOP
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1538 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1539 return_value(obj);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1540 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1541 case CX_JSON_ARRAY: {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1542 const size_t elem_count = source->array.size;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1543 CxJsonValue *arr = cxJsonCreateArr(allocator, elem_count);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1544 if (arr == NULL) return NULL; // LCOV_EXCL_LINE
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1545 arr->array.size = elem_count;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1546 for (size_t i = 0 ; i < elem_count ; i++) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1547 CxJsonValue *e = cx_json_clone_func(NULL, source->array.data[i], allocator, NULL);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1548 if (e == NULL) {
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1549 // LCOV_EXCL_START
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1550 cxJsonValueFree(arr);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1551 return NULL;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1552 // LCOV_EXCL_STOP
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1553 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1554 arr->array.data[i] = e;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1555 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1556 return_value(arr);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1557 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1558 case CX_JSON_STRING:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1559 return_value(cxJsonCreateString(allocator, source->string));
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1560 case CX_JSON_INTEGER:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1561 return_value(cxJsonCreateInteger(allocator, source->integer));
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1562 case CX_JSON_NUMBER:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1563 return_value(cxJsonCreateNumber(allocator, source->number));
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1564 case CX_JSON_LITERAL:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1565 return_value(cxJsonCreateLiteral(allocator, source->literal));
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1566 default:
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1567 // LCOV_EXCL_START
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1568 // unreachable
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1569 assert(false);
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1570 return NULL;
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1571 // LCOV_EXCL_STOP
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1572 }
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1573 #undef return_value
ccde46662db7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 995
diff changeset
1574 }

mercurial