src/json.c

Fri, 03 Jan 2025 19:18:00 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 03 Jan 2025 19:18:00 +0100
changeset 1082
46cdc8689fc4
parent 1079
4e1872151fb6
child 1083
cf54e413793c
permissions
-rw-r--r--

implement index array to preserve order of json object members

relates to #526 and resolves #462

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"
1067
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
30 #include "cx/compare.h"
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 1037
diff changeset
31
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <string.h>
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 #include <ctype.h>
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
34 #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
35 #include <stdio.h>
1014
56eb7da4f3e1 fix number parser not detecting integers out of range
Mike Becker <universe@uap-core.de>
parents: 1012
diff changeset
36 #include <errno.h>
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
37 #include <inttypes.h>
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 /*
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 * RFC 8259
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 * 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
42 */
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43
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
44 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
45
1067
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
46 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
47 const CxJsonObjValue *left = l;
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
48 const CxJsonObjValue *right = r;
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
49 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
50 }
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
51
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
52 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
53 assert(obj->type == CX_JSON_OBJECT);
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
54 CxJsonObjValue kv_dummy;
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
55 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
56 size_t index = cx_array_binary_search(
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
57 obj->value.object.values,
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
58 obj->value.object.values_size,
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
59 sizeof(CxJsonObjValue),
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
60 &kv_dummy,
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
61 json_cmp_objvalue
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
62 );
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
63 if (index == obj->value.object.values_size) {
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
64 return NULL;
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
65 } else {
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
66 return &obj->value.object.values[index];
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
67 }
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
68 }
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
69
1082
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
70 static int json_add_objvalue(CxJsonValue *objv, CxJsonObjValue member) {
1067
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
71 assert(obj->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
72 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
73 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
74
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
75 // 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
76 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
77 obj->values,
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
78 obj->values_size,
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
79 sizeof(CxJsonObjValue),
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
80 &member, json_cmp_objvalue
1067
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
81 );
1082
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
82
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
83 // 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
84 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
85 // 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
86 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
87 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
88 // replace the item
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
89 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
90
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
91 // 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
92 return 0;
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
93 }
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
94
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
95 // 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
96 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
97 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
98 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
99
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
100 // 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
101 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
102 if (newcap > oldcap) {
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
103 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
104 return 1;
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 }
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
107
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
108 // 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
109 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
110 // 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
111 memmove(
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
112 &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
113 &obj->values[index],
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
114 (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
115 );
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
116 // 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
117 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
118 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
119 obj->indices[i]++;
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 }
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
123
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
124 // 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
125 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
126 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
127 obj->values_size++;
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
128
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
129 return 0;
1067
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
130 }
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
131
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
132 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
133 if (token->allocated) {
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
134 cx_strfree(&token->content);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
135 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
136 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 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
139 if (pos >= length) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 return 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 int ok = 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 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
145 char c = content[i];
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 if (isdigit(c)) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 ok = 1;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 } else if (i == pos) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 if (!(c == '+' || c == '-')) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 return 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 return 0;
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 }
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 return ok;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 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
161 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
162
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 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
164 return CX_JSON_TOKEN_ERROR;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 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
168 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
169 if (content[i] == '.') {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 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
171 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
172 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 type = CX_JSON_TOKEN_NUMBER;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 } 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
175 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
176 } else if (!isdigit(content[i])) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 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
178 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 }
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 return type;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
184 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
185 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
186 bool allocated = false;
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
187 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
188 allocated = true;
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
189 str = cx_strcat_m(json->uncompleted.content, 1, str);
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
190 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
191 return (CxJsonToken){CX_JSON_NO_TOKEN, false, {NULL, 0}};
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
192 } // LCOV_EXCL_STOP
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
193 }
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
194 json->uncompleted = (CxJsonToken){0};
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
195 CxJsonTokenType ttype;
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
196 if (isstring) {
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
197 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
198 } else {
1009
7650e722437e minor cleanup
Mike Becker <universe@uap-core.de>
parents: 1008
diff changeset
199 cxstring s = cx_strcast(str);
7650e722437e minor cleanup
Mike Becker <universe@uap-core.de>
parents: 1008
diff changeset
200 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
201 || !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
202 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
203 } else {
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
204 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
205 }
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
206 }
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
207 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
208 if (allocated) {
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
209 cx_strfree(&str);
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
210 }
1020
e78e65405c56 fix wrong "variant" of zero/NULL/false
Mike Becker <universe@uap-core.de>
parents: 1014
diff changeset
211 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
212 }
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
213 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
214 }
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
215
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 static CxJsonTokenType char2ttype(char c) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 switch (c) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 case '[': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 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
220 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 case '{': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 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
223 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 case ']': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 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
226 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 case '}': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 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
229 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 case ':': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 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
232 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 case ',': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 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
235 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 case '"': {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 return CX_JSON_TOKEN_STRING;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 default: {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 if (isspace(c)) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 return CX_JSON_TOKEN_SPACE;
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 }
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 return CX_JSON_NO_TOKEN;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
248 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
249 // 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
250 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
251 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
252 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
253 }
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
254
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 // current token type and start index
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
256 CxJsonTokenType ttype = json->uncompleted.tokentype;
1007
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
257 size_t token_start = json->buffer.pos;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258
1007
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
259 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
260 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
261 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
262 // currently non-string token
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 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
264 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
265 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
266 json->buffer.pos++;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 continue;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268 } 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
269 // begin string
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 ttype = CX_JSON_TOKEN_STRING;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 token_start = i;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 } 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
273 // 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
274 json->buffer.pos = i + 1;
1020
e78e65405c56 fix wrong "variant" of zero/NULL/false
Mike Becker <universe@uap-core.de>
parents: 1014
diff changeset
275 *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
276 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
277 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278 ttype = CX_JSON_TOKEN_LITERAL; // number or literal
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 token_start = i;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 // finish token
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 if (ctype != CX_JSON_NO_TOKEN) {
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
284 *result = token_create(json, false, token_start, i);
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
285 if (result->tokentype == CX_JSON_NO_TOKEN) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
286 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
287 }
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
288 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
289 return CX_JSON_FORMAT_ERROR_NUMBER;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
290 }
1007
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
291 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
292 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
293 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296 // currently inside a string
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
297 if (json->tokenizer_escape) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
298 json->tokenizer_escape = false;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
299 } else {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300 if (c == '"') {
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
301 *result = token_create(json, true, token_start, i + 1);
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
302 if (result->tokentype == CX_JSON_NO_TOKEN) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
303 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
304 }
1007
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1002
diff changeset
305 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
306 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
307 } else if (c == '\\') {
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
308 json->tokenizer_escape = true;
937
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 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
313
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
314 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
315 // uncompleted token
1008
3b69f025f083 json: enable multiple subsequent fills - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
316 size_t uncompleted_len = json->buffer.size - token_start;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
317 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
318 // 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
319 // save current token content
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
320 CxJsonToken uncompleted = {
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
321 ttype, true,
1008
3b69f025f083 json: enable multiple subsequent fills - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
322 cx_strdup(cx_strn(json->buffer.space + token_start, uncompleted_len))
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
323 };
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
324 if (uncompleted.content.ptr == NULL) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
325 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
326 }
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
327 json->uncompleted = uncompleted;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
328 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
329 // 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
330 // 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
331 assert(json->uncompleted.allocated);
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
332 cxmutstr str = cx_strcat_m(json->uncompleted.content, 1,
1008
3b69f025f083 json: enable multiple subsequent fills - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
333 cx_strn(json->buffer.space + token_start, uncompleted_len));
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
334 if (str.ptr == NULL) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
335 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
336 }
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
337 json->uncompleted.content = str;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
338 }
1008
3b69f025f083 json: enable multiple subsequent fills - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
339 // 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
340 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
341 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
342
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
343 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
344 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
345
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
346 static cxmutstr unescape_string(const CxAllocator *a, cxmutstr str) {
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
347 // TODO: support more escape sequences
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
348 // we know that the unescaped string will be shorter by at least 2 chars
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
349 cxmutstr result;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
350 result.length = 0;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
351 result.ptr = cxMalloc(a, str.length - 1);
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
352 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
353
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
354 bool u = false;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
355 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
356 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
357 if (u) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
358 u = false;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
359 if (c == 'n') {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
360 c = '\n';
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
361 } else if (c == 't') {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
362 c = '\t';
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
363 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
364 result.ptr[result.length++] = c;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
365 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
366 if (c == '\\') {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
367 u = true;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
368 } else {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
369 result.ptr[result.length++] = c;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
370 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
371 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
372 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
373 result.ptr[result.length] = 0;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
374
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
375 return result;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
376 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
377
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
378 static CxJsonValue* create_json_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
379 CxJsonValue *v = cxCalloc(json->allocator, 1, sizeof(CxJsonValue));
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
380 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
381
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
382 // 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
383 v->type = type;
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
384 v->allocator = json->allocator;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
385 if (type == CX_JSON_ARRAY) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
386 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
387 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
388 } else if (type == CX_JSON_OBJECT) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
389 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
390 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
391 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
392 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
393 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
394 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
395
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
396 // 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
397 if (json->vbuf_size > 0) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
398 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
399 assert(parent != NULL);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
400 if (parent->type == CX_JSON_ARRAY) {
1067
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
401 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
402 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
403 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
404 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
405 } 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
406 // 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
407 assert(json->uncompleted_member.name.ptr != NULL);
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
408 json->uncompleted_member.value = v;
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
409 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
410 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
411 }
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
412 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
413 } else {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
414 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
415 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
416 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
417
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
418 // 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
419 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
420 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
421 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
422 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
423 }
940
bbf41b9c2658 fix memory leak in json reader when handling incomplete tokens
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 938
diff changeset
424 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
425
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
426 // 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
427 if (json->parsed == NULL) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
428 json->parsed = v;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
429 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
430
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
431 return v;
1067
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
432 // LCOV_EXCL_START
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
433 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
434 cxJsonValueFree(v);
1067
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
435 return NULL;
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
436 // LCOV_EXCL_STOP
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
437 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
438
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
439 #define JP_STATE_VALUE_BEGIN 0
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
440 #define JP_STATE_VALUE_END 10
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
441 #define JP_STATE_VALUE_BEGIN_OBJ 1
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
442 #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
443 #define JP_STATE_VALUE_BEGIN_AR 2
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
444 #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
445 #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
446 #define JP_STATE_OBJ_NAME 6
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
447 #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
448
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
449 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
450 if (allocator == NULL) {
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
451 allocator = cxDefaultAllocator;
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
452 }
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
453
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
454 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
455 json->allocator = allocator;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
456
946
b428424c0214 avoid state buffer allocation for JSON with reasonable nesting depth
Mike Becker <universe@uap-core.de>
parents: 944
diff changeset
457 json->states = json->states_internal;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
458 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
459 json->states[0] = JP_STATE_VALUE_BEGIN;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
460 json->states_size = 1;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
461
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
462 json->vbuf = json->vbuf_internal;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
463 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
464 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
465
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
466 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
467 cxBufferDestroy(&json->buffer);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
468 if (json->states != json->states_internal) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
469 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
470 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
471 if (json->vbuf != json->vbuf_internal) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
472 free(json->vbuf);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
473 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
474 cxJsonValueFree(json->parsed);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
475 json->parsed = NULL;
1075
0cc4b63a0ae0 fix cxJsonDestroy() not freeing uncompleted object member names
Mike Becker <universe@uap-core.de>
parents: 1072
diff changeset
476 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
477 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
478 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
479 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
480 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
481
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
482 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
483 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
484 // 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
485 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
486 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
487 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
488 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
489 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
490 } 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
491 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
492 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
493 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
494
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
495 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
496 // 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
497 // 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
498 json->states[json->states_size++] = state;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
499 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
500
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
501 #define return_rec(code) \
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
502 token_destroy(&token); \
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
503 return code
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
504
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
505 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
506 // 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
507 CxJsonValue *vbuf = NULL;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
508
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
509 // 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
510 CxJsonToken token;
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
511 {
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
512 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
513 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
514 return ret;
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
515 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
516 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
517
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
518 // pop the current state
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
519 assert(json->states_size > 0);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
520 int state = json->states[--json->states_size];
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
521
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
522 // 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
523 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
524 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
525 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
526 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
527
1000
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 // 0 JP_STATE_VALUE_BEGIN value begin
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
530 // 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
531 // 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
532 // 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
533 // 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
534 // 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
535 // 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
536 // 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
537 // 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
538
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
539 if (state < 3) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
540 // push expected end state to the stack
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
541 json_add_state(json, 10 + state);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
542 switch (token.tokentype) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
543 case CX_JSON_TOKEN_BEGIN_ARRAY: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
544 if (create_json_value(json, CX_JSON_ARRAY) == NULL) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
545 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
546 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
547 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
548 return_rec(CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
549 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
550 case CX_JSON_TOKEN_BEGIN_OBJECT: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
551 if (create_json_value(json, CX_JSON_OBJECT) == NULL) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
552 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
553 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
554 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
555 return_rec(CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
556 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
557 case CX_JSON_TOKEN_STRING: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
558 if ((vbuf = create_json_value(json, CX_JSON_STRING)) == NULL) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
559 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
560 }
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
561 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
562 if (str.ptr == NULL) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
563 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
564 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
565 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
566 return_rec(CX_JSON_NO_ERROR);
1000
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 case CX_JSON_TOKEN_INTEGER:
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
569 case CX_JSON_TOKEN_NUMBER: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
570 int type = token.tokentype == CX_JSON_TOKEN_INTEGER ? CX_JSON_INTEGER : CX_JSON_NUMBER;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
571 if (NULL == (vbuf = create_json_value(json, type))) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
572 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
573 }
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
574 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
575 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
576 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
577 }
0a7c1bb2372d avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents: 1057
diff changeset
578 } 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
579 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
580 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
581 }
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
582 }
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
583 return_rec(CX_JSON_NO_ERROR);
1000
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 case CX_JSON_TOKEN_LITERAL: {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
586 if ((vbuf = create_json_value(json, CX_JSON_LITERAL)) == NULL) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
587 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
588 }
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
589 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
590 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
591 } 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
592 vbuf->value.literal = CX_JSON_FALSE;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
593 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
594 vbuf->value.literal = CX_JSON_NULL;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
595 }
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
596 return_rec(CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
597 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
598 default: {
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
599 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
600 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
601 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
602 } 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
603 // expect ',' or ']'
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
604 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
605 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
606 return_rec(CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
607 } 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
608 // discard the array from the value buffer
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
609 json->vbuf_size--;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
610 return_rec(CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
611 } else {
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
612 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
613 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
614 } 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
615 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
616 // discard the obj from the value buffer
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
617 json->vbuf_size--;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
618 return_rec(CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
619 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
620 // expect string
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
621 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
622 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
623 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
624
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
625 // add new entry
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
626 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
627 if (name.ptr == NULL) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
628 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
629 }
1067
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
630 assert(json->uncompleted_member.name.ptr == NULL);
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
631 json->uncompleted_member.name = name;
1037
83620ba72cc1 generify cxJsonObjGet() - fixes #525
Mike Becker <universe@uap-core.de>
parents: 1033
diff changeset
632 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
633
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
634 // next state
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
635 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
636 return_rec(CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
637 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
638 } else if (state == JP_STATE_OBJ_COLON) {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
639 // expect ':'
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
640 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
641 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
642 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
643 // next state
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
644 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
645 return_rec(CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
646 } 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
647 // expect ',' or '}'
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
648 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
649 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
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 } 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
652 // discard the obj from the value buffer
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
653 json->vbuf_size--;
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
654 return_rec(CX_JSON_NO_ERROR);
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
655 } else {
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
656 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
657 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
658 } else {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
659 // should be unreachable
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
660 assert(false);
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
661 return_rec(-1);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
662 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
663 }
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
664
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
665 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
666 // 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
667 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
668 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
669 }
0a7c1bb2372d avoid copying the filled data in the json parser when possible - fixes #530
Mike Becker <universe@uap-core.de>
parents: 1057
diff changeset
670
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
671 // initialize output value
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
672 *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
673
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
674 // parse data
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
675 CxJsonStatus result;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
676 do {
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
677 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
678 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
679 // final state reached
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
680 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
681 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
682
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
683 // write output value
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
684 *value = json->parsed;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
685 json->parsed = NULL;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
686
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
687 // re-initialize state machine
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
688 json->states[0] = JP_STATE_VALUE_BEGIN;
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
689
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
690 return CX_JSON_NO_ERROR;
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
691 }
1002
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
692 } 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
693
1483c47063a8 add status codes to json parser - relates to #431
Mike Becker <universe@uap-core.de>
parents: 1000
diff changeset
694 // 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
695 // 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
696 // 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
697 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
698 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
699 }
1000
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
700
1aecddf7e209 simplify how the json parser works
Mike Becker <universe@uap-core.de>
parents: 996
diff changeset
701 return result;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
702 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
703
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
704 void cxJsonValueFree(CxJsonValue *value) {
1012
21884374edbb add documentation - resolves #431
Mike Becker <universe@uap-core.de>
parents: 1009
diff changeset
705 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
706 switch (value->type) {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
707 case CX_JSON_OBJECT: {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
708 CxJsonObject obj = value->value.object;
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
709 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
710 cxJsonValueFree(obj.values[i].value);
1037
83620ba72cc1 generify cxJsonObjGet() - fixes #525
Mike Becker <universe@uap-core.de>
parents: 1033
diff changeset
711 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
712 }
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
713 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
714 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
715 break;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
716 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
717 case CX_JSON_ARRAY: {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
718 CxJsonArray array = value->value.array;
967
a58f602ed2fe simplify parsing of array and object elements
Mike Becker <universe@uap-core.de>
parents: 965
diff changeset
719 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
720 cxJsonValueFree(array.array[i]);
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
721 }
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
722 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
723 break;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
724 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
725 case CX_JSON_STRING: {
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
726 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
727 break;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
728 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
729 default: {
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
730 break;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
731 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
732 }
996
333155f234c4 add support for allocators to the json parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 976
diff changeset
733 cxFree(value->allocator, value);
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
734 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
735
1054
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
736 CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator) {
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
737 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
738 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
739 v->allocator = allocator;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
740 v->type = CX_JSON_OBJECT;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
741 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
742 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
743 cxFree(allocator, v);
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
744 return NULL;
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
745 // LCOV_EXCL_STOP
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
746 }
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
747 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
748 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
749 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
750 cxFree(allocator, v);
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
751 return NULL;
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
752 // LCOV_EXCL_STOP
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
753 }
1054
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
754 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
755 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
756
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
757 CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator) {
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
758 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
759 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
760 v->allocator = allocator;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
761 v->type = CX_JSON_ARRAY;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
762 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
763 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
764 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
765 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
766
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
767 CxJsonValue* cxJsonCreateNumber(const CxAllocator* allocator, double num) {
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
768 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
769 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
770 v->allocator = allocator;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
771 v->type = CX_JSON_NUMBER;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
772 v->value.number = num;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
773 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
774 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
775
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
776 CxJsonValue* cxJsonCreateInteger(const CxAllocator* allocator, int64_t num) {
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
777 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
778 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
779 v->allocator = allocator;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
780 v->type = CX_JSON_INTEGER;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
781 v->value.integer = num;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
782 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
783 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
784
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
785 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
786 return cxJsonCreateCxString(allocator, cx_str(str));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
787 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
788
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
789 CxJsonValue* cxJsonCreateCxString(const CxAllocator* allocator, cxstring str) {
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
790 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
791 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
792 v->allocator = allocator;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
793 v->type = CX_JSON_STRING;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
794 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
795 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
796 v->value.string = s;
1054
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
797 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
798 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
799
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
800 CxJsonValue* cxJsonCreateLiteral(const CxAllocator* allocator, CxJsonLiteral lit) {
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
801 CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
802 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
803 v->allocator = allocator;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
804 v->type = CX_JSON_LITERAL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
805 v->value.literal = lit;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
806 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
807 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
808
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
809 // LCOV_EXCL_START
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
810 // never called as long as malloc() does not return NULL
1054
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
811 static void cx_json_arr_free_temp(CxJsonValue** values, size_t count) {
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
812 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
813 if (values[i] == NULL) break;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
814 cxJsonValueFree(values[i]);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
815 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
816 free(values);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
817 }
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1060
diff changeset
818 // LCOV_EXCL_STOP
1054
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
819
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
820 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
821 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
822 if (values == NULL) return -1;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
823 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
824 values[i] = cxJsonCreateNumber(arr->allocator, num[i]);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
825 if (values[i] == NULL) { cx_json_arr_free_temp(values, count); return -1; }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
826 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
827 int ret = cxJsonArrAddValues(arr, values, count);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
828 free(values);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
829 return ret;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
830 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
831
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
832 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
833 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
834 if (values == NULL) return -1;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
835 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
836 values[i] = cxJsonCreateInteger(arr->allocator, num[i]);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
837 if (values[i] == NULL) { cx_json_arr_free_temp(values, count); return -1; }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
838 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
839 int ret = cxJsonArrAddValues(arr, values, count);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
840 free(values);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
841 return ret;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
842 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
843
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
844 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
845 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
846 if (values == NULL) return -1;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
847 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
848 values[i] = cxJsonCreateString(arr->allocator, str[i]);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
849 if (values[i] == NULL) { cx_json_arr_free_temp(values, count); return -1; }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
850 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
851 int ret = cxJsonArrAddValues(arr, values, count);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
852 free(values);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
853 return ret;
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 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
857 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
858 if (values == NULL) return -1;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
859 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
860 values[i] = cxJsonCreateCxString(arr->allocator, str[i]);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
861 if (values[i] == NULL) { cx_json_arr_free_temp(values, count); return -1; }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
862 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
863 int ret = cxJsonArrAddValues(arr, values, count);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
864 free(values);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
865 return ret;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
866 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
867
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
868 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
869 CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
870 if (values == NULL) return -1;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
871 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
872 values[i] = cxJsonCreateLiteral(arr->allocator, lit[i]);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
873 if (values[i] == NULL) { cx_json_arr_free_temp(values, count); return -1; }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
874 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
875 int ret = cxJsonArrAddValues(arr, values, count);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
876 free(values);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
877 return ret;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
878 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
879
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
880 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
881 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
882 assert(arr->type == CX_JSON_ARRAY);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
883 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
884 arr->value.array.array,
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
885 arr->value.array.array_size,
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
886 val, count
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
887 );
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
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
890 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
891 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
892 if (k.ptr == NULL) return -1;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
893 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
894 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
895 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
896 return 1;
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
897 } else {
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
898 return 0;
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
899 }
1054
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
900 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
901
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
902 CxJsonValue* cxJsonObjPutObj(CxJsonValue* obj, cxstring name) {
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
903 CxJsonValue* v = cxJsonCreateObj(obj->allocator);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
904 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
905 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
906 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
907 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
908
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
909 CxJsonValue* cxJsonObjPutArr(CxJsonValue* obj, cxstring name) {
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
910 CxJsonValue* v = cxJsonCreateArr(obj->allocator);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
911 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
912 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
913 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
914 }
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 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
917 CxJsonValue* v = cxJsonCreateNumber(obj->allocator, num);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
918 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
919 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
920 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
921 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
922
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
923 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
924 CxJsonValue* v = cxJsonCreateInteger(obj->allocator, num);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
925 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
926 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
927 return v;
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
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
930 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
931 CxJsonValue* v = cxJsonCreateString(obj->allocator, str);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
932 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
933 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
934 return v;
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
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
937 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
938 CxJsonValue* v = cxJsonCreateCxString(obj->allocator, str);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
939 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
940 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
941 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
942 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
943
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
944 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
945 CxJsonValue* v = cxJsonCreateLiteral(obj->allocator, lit);
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
946 if (v == NULL) return NULL;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
947 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
948 return v;
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
949 }
fb1076ead56f add implementation for creating new json values
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
950
1009
7650e722437e minor cleanup
Mike Becker <universe@uap-core.de>
parents: 1008
diff changeset
951 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
952 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
953 return &cx_json_value_nothing;
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
954 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
955 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
956 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
957
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
958 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
959 return cxIteratorPtr(
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
960 value->value.array.array,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
961 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
962 );
1033
e3009345984b add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents: 1020
diff changeset
963 }
e3009345984b add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents: 1020
diff changeset
964
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
965 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
966 return cxIterator(
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
967 value->value.object.values,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
968 sizeof(CxJsonObjValue),
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
969 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
970 );
1033
e3009345984b add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents: 1020
diff changeset
971 }
e3009345984b add cxJsonArrIter() - fixes #524
Mike Becker <universe@uap-core.de>
parents: 1020
diff changeset
972
1037
83620ba72cc1 generify cxJsonObjGet() - fixes #525
Mike Becker <universe@uap-core.de>
parents: 1033
diff changeset
973 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
974 CxJsonObjValue *member = json_find_objvalue(value, name);
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
975 if (member == NULL) {
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
976 return &cx_json_value_nothing;
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
977 } else {
7799addf475f optimize cxJsonObjGet() part 1 - binary search
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
978 return member->value;
937
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
979 }
10123f4d5618 add first draft of json implementation - relates to #431
Mike Becker <universe@uap-core.de>
parents:
diff changeset
980 }
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
981
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
982 static const CxJsonWriter cx_json_writer_default = {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
983 false,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
984 true,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
985 255,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
986 false,
1079
4e1872151fb6 remove idea for an array wrapping feature - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1078
diff changeset
987 4
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
988 };
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
989
1077
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
990 CxJsonWriter cxJsonWriterCompact(void) {
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
991 return cx_json_writer_default;
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
992 }
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
993
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
994 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
995 return (CxJsonWriter) {
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
996 true,
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
997 true,
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
998 255,
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
999 use_spaces,
1079
4e1872151fb6 remove idea for an array wrapping feature - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1078
diff changeset
1000 4
1077
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
1001 };
911a154dd469 add functions to create default JSON writers - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1075
diff changeset
1002 }
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1003
1078
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1004 static int cx_json_writer_indent(
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1005 void *target,
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1006 cx_write_func wfunc,
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1007 const CxJsonWriter *settings,
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1008 unsigned int depth
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1009 ) {
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1010 if (depth == 0) return 0;
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1011
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1012 // 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
1013 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
1014 size_t width = depth;
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1015 if (settings->indent_space) {
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1016 if (settings->indent == 0) return 0;
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1017 width *= settings->indent;
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1018 indent = " ";
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1019 } else {
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1020 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
1021 }
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1022
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1023 // 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
1024 size_t full = width / 32;
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1025 size_t remaining = width % 32;
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1026 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
1027 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
1028 }
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1029 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
1030
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1031 return 0;
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1032 }
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1033
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1034
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1035 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
1036 void *target,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1037 const CxJsonValue *value,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1038 cx_write_func wfunc,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1039 const CxJsonWriter *settings,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1040 unsigned int depth
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1041 ) {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1042 // keep track of written items
1078
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1043 // 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
1044 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
1045
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1046 // small buffer for number to string conversions
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1047 char numbuf[32];
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1048
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1049 // recursively write the values
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1050 switch (value->type) {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1051 case CX_JSON_OBJECT: {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1052 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
1053 if (settings->pretty) {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1054 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
1055 expected += 2;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1056 } else {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1057 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
1058 expected++;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1059 }
1078
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1060 depth++;
1082
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
1061 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
1062 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
1063 // 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
1064 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
1065 ? look_idx
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
1066 : 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
1067 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
1068 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
1069 depth++;depth--;
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
1070 }
46cdc8689fc4 implement index array to preserve order of json object members
Mike Becker <universe@uap-core.de>
parents: 1079
diff changeset
1071
1078
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1072 // possible indentation
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1073 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
1074 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
1075 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
1076 }
1078
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1077 }
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1078
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1079 // the name
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1080 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
1081 // TODO: escape the string
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1082 actual += wfunc(member->name.ptr, 1,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1083 member->name.length, target);
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1084 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
1085 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
1086 if (settings->pretty) {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1087 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
1088 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
1089 } else {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1090 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
1091 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
1092 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1093
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1094 // the value
1078
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1095 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
1096
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1097 // 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
1098 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
1099 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
1100 if (settings->pretty) {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1101 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
1102 expected += 2;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1103 } else {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1104 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
1105 expected++;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1106 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1107 } else {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1108 if (settings->pretty) {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1109 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
1110 expected ++;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1111 }
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 }
1078
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1114 depth--;
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1115 if (settings->pretty) {
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1116 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
1117 }
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1118 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
1119 expected++;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1120 break;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1121 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1122 case CX_JSON_ARRAY: {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1123 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
1124 expected++;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1125 CxIterator iter = cxJsonArrIter(value);
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1126 cx_foreach(CxJsonValue*, element, iter) {
1078
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1127 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
1128 target, element,
1078
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1129 wfunc, settings, depth)
ffa8bb4e9288 implement JSON pretty printing - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1077
diff changeset
1130 ) return 1;
1072
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1131
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1132 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
1133 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
1134 if (settings->pretty) {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1135 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
1136 expected += 2;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1137 } else {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1138 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
1139 expected++;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1140 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1141 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1142 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1143 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
1144 expected++;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1145 break;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1146 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1147 case CX_JSON_STRING: {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1148 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
1149 // TODO: escape the string
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1150 actual += wfunc(value->value.string.ptr, 1,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1151 value->value.string.length, target);
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1152 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
1153 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
1154 break;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1155 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1156 case CX_JSON_NUMBER: {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1157 // TODO: locale bullshit
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1158 // TODO: formatting settings
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1159 snprintf(numbuf, 32, "%g", value->value.number);
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1160 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
1161 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
1162 expected += len;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1163 break;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1164 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1165 case CX_JSON_INTEGER: {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1166 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
1167 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
1168 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
1169 expected += len;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1170 break;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1171 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1172 case CX_JSON_LITERAL: {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1173 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
1174 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
1175 expected += 4;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1176 } 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
1177 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
1178 expected += 5;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1179 } else {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1180 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
1181 expected += 4;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1182 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1183 break;
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 case CX_JSON_NOTHING: {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1186 // 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
1187 // 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
1188 // 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
1189 // 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
1190 break;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1191 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1192 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
1193 }
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 return expected != actual;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1196 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1197
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1198 int cxJsonWrite(
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1199 void *target,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1200 const CxJsonValue *value,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1201 cx_write_func wfunc,
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1202 const CxJsonWriter *settings
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1203 ) {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1204 if (settings == NULL) {
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1205 settings = &cx_json_writer_default;
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1206 }
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1207 assert(target != NULL);
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1208 assert(value != NULL);
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1209 assert(wfunc != NULL);
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1210
c89283cd559b first mvp for the json writer - relates to #526
Mike Becker <universe@uap-core.de>
parents: 1067
diff changeset
1211 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
1212 }

mercurial