src/json.c

Sat, 11 Jan 2025 12:56:54 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 11 Jan 2025 12:56:54 +0100
changeset 1122
49ab92de9a13
parent 1121
7fd2672199d7
permissions
-rw-r--r--

add more escape sequences to unescape function

and change the name of token_start to more clearly express what it actually is
(the start of the currently parsed PART of the token)

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

mercurial