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