|
4 | 4 |
|
5 | 5 | #pragma once
|
6 | 6 |
|
7 |
| -#include "../Numbers/parseFloat.hpp" |
8 |
| -#include "../Numbers/parseInteger.hpp" |
9 | 7 | #include "JsonVariantContent.hpp"
|
10 | 8 | #include "JsonVariantType.hpp"
|
11 | 9 |
|
12 | 10 | namespace ArduinoJson {
|
13 | 11 | namespace Internals {
|
14 | 12 |
|
| 13 | +// this struct must be a POD type to prevent error calling offsetof on clang |
15 | 14 | struct JsonVariantData {
|
16 | 15 | JsonVariantType type;
|
17 | 16 | JsonVariantContent content;
|
18 |
| - |
19 |
| - JsonVariantData() { |
20 |
| - type = JSON_NULL; |
21 |
| - } |
22 |
| - |
23 |
| - void setBoolean(bool value) { |
24 |
| - type = JSON_BOOLEAN; |
25 |
| - content.asInteger = static_cast<JsonUInt>(value); |
26 |
| - } |
27 |
| - |
28 |
| - void setFloat(JsonFloat value) { |
29 |
| - type = JSON_FLOAT; |
30 |
| - content.asFloat = value; |
31 |
| - } |
32 |
| - |
33 |
| - void setNegativeInteger(JsonUInt value) { |
34 |
| - type = JSON_NEGATIVE_INTEGER; |
35 |
| - content.asInteger = value; |
36 |
| - } |
37 |
| - |
38 |
| - void setPostiveInteger(JsonUInt value) { |
39 |
| - type = JSON_POSITIVE_INTEGER; |
40 |
| - content.asInteger = value; |
41 |
| - } |
42 |
| - |
43 |
| - void setOwnedString(const char *value) { |
44 |
| - type = JSON_OWNED_STRING; |
45 |
| - content.asString = value; |
46 |
| - } |
47 |
| - |
48 |
| - void setLinkedString(const char *value) { |
49 |
| - type = JSON_LINKED_STRING; |
50 |
| - content.asString = value; |
51 |
| - } |
52 |
| - |
53 |
| - void setOwnedRaw(const char *data, size_t size) { |
54 |
| - type = JSON_OWNED_RAW; |
55 |
| - content.asRaw.data = data; |
56 |
| - content.asRaw.size = size; |
57 |
| - } |
58 |
| - |
59 |
| - void setLinkedRaw(const char *data, size_t size) { |
60 |
| - type = JSON_LINKED_RAW; |
61 |
| - content.asRaw.data = data; |
62 |
| - content.asRaw.size = size; |
63 |
| - } |
64 |
| - |
65 |
| - void setNull() { |
66 |
| - type = JSON_NULL; |
67 |
| - } |
68 |
| - |
69 |
| - JsonArrayData *toArray() { |
70 |
| - type = JSON_ARRAY; |
71 |
| - content.asArray.head = 0; |
72 |
| - content.asArray.tail = 0; |
73 |
| - return &content.asArray; |
74 |
| - } |
75 |
| - |
76 |
| - JsonObjectData *toObject() { |
77 |
| - type = JSON_OBJECT; |
78 |
| - content.asObject.head = 0; |
79 |
| - content.asObject.tail = 0; |
80 |
| - return &content.asObject; |
81 |
| - } |
82 |
| - |
83 |
| - JsonArrayData *asArray() { |
84 |
| - return type == JSON_ARRAY ? &content.asArray : 0; |
85 |
| - } |
86 |
| - |
87 |
| - JsonObjectData *asObject() { <
28BE
/td> |
88 |
| - return type == JSON_OBJECT ? &content.asObject : 0; |
89 |
| - } |
90 |
| - |
91 |
| - template <typename T> |
92 |
| - T asInteger() const { |
93 |
| - switch (type) { |
94 |
| - case JSON_POSITIVE_INTEGER: |
95 |
| - case JSON_BOOLEAN: |
96 |
| - return T(content.asInteger); |
97 |
| - case JSON_NEGATIVE_INTEGER: |
98 |
| - return T(~content.asInteger + 1); |
99 |
| - case JSON_LINKED_STRING: |
100 |
| - case JSON_OWNED_STRING: |
101 |
| - return parseInteger<T>(content.asString); |
102 |
| - case JSON_FLOAT: |
103 |
| - return T(content.asFloat); |
104 |
| - default: |
105 |
| - return 0; |
106 |
| - } |
107 |
| - } |
108 |
| - |
109 |
| - template <typename T> |
110 |
| - T asFloat() const { |
111 |
| - switch (type) { |
112 |
| - case JSON_POSITIVE_INTEGER: |
113 |
| - case JSON_BOOLEAN: |
114 |
| - return static_cast<T>(content.asInteger); |
115 |
| - case JSON_NEGATIVE_INTEGER: |
116 |
| - return -static_cast<T>(content.asInteger); |
117 |
| - case JSON_LINKED_STRING: |
118 |
| - case JSON_OWNED_STRING: |
119 |
| - return parseFloat<T>(content.asString); |
120 |
| - case JSON_FLOAT: |
121 |
| - return static_cast<T>(content.asFloat); |
122 |
| - default: |
123 |
| - return 0; |
124 |
| - } |
125 |
| - } |
126 |
| - |
127 |
| - const char *asString() const { |
128 |
| - return isString() ? content.asString : NULL; |
129 |
| - } |
130 |
| - |
131 |
| - bool isArray() const { |
132 |
| - return type == JSON_ARRAY; |
133 |
| - } |
134 |
| - |
135 |
| - bool isBoolean() const { |
136 |
| - return type == JSON_BOOLEAN; |
137 |
| - } |
138 |
| - |
139 |
| - bool isFloat() const { |
140 |
| - return type == JSON_FLOAT || type == JSON_POSITIVE_INTEGER || |
141 |
| - type == JSON_NEGATIVE_INTEGER; |
142 |
| - } |
143 |
| - |
144 |
| - bool isInteger() const { |
145 |
| - return type == JSON_POSITIVE_INTEGER || type == JSON_NEGATIVE_INTEGER; |
146 |
| - } |
147 |
| - |
148 |
| - bool isNull() const { |
149 |
| - return type == JSON_NULL; |
150 |
| - } |
151 |
| - |
152 |
| - bool isObject() const { |
153 |
| - return type == JSON_OBJECT; |
154 |
| - } |
155 |
| - |
156 |
| - bool isString() const { |
157 |
| - return type == JSON_LINKED_STRING || type == JSON_OWNED_STRING; |
158 |
| - } |
159 | 17 | };
|
| 18 | + |
| 19 | +inline JsonVariantData *getVariantData(JsonArrayData *arr) { |
| 20 | + const ptrdiff_t offset = offsetof(JsonVariantData, content) - |
| 21 | + offsetof(JsonVariantContent, asArray); |
| 22 | + if (!arr) return 0; |
| 23 | + return reinterpret_cast<JsonVariantData *>(reinterpret_cast<char *>(arr) - |
| 24 | + offset); |
| 25 | +} |
| 26 | + |
| 27 | +inline JsonVariantData *getVariantData(JsonObjectData *obj) { |
| 28 | + const ptrdiff_t offset = offsetof(JsonVariantData, content) - |
| 29 | + offsetof(JsonVariantContent, asObject); |
| 30 | + if (!obj) return 0; |
| 31 | + return reinterpret_cast<JsonVariantData *>(reinterpret_cast<char *>(obj) - |
| 32 | + offset); |
| 33 | +} |
160 | 34 | } // namespace Internals
|
161 | 35 | } // namespace ArduinoJson
|
0 commit comments