8000 Added implicit conversion from `JsonArray` and `JsonObject` to `JsonV… · java64/ArduinoJson@29e71cb · GitHub
[go: up one dir, main page]

Skip to content

Commit 29e71cb

Browse files
committed
Added implicit conversion from JsonArray and JsonObject to JsonVariant
1 parent 2d54019 commit 29e71cb

File tree

12 files changed

+219
-190
lines changed

12 files changed

+219
-190
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
ArduinoJson: change log
22
=======================
33

4+
HEAD
5+
----
6+
7+
* Added implicit conversion from `JsonArray` and `JsonObject` to `JsonVariant`
8+
49
v6.4.0-beta (2018-09-11)
510
-----------
611

src/ArduinoJson/Data/JsonVariantData.hpp

Lines changed: 17 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -4,158 +4,32 @@
44

55
#pragma once
66

7-
#include "../Numbers/parseFloat.hpp"
8-
#include "../Numbers/parseInteger.hpp"
97
#include "JsonVariantContent.hpp"
108
#include "JsonVariantType.hpp"
119

1210
namespace ArduinoJson {
1311
namespace Internals {
1412

13+
// this struct must be a POD type to prevent error calling offsetof on clang
1514
struct JsonVariantData {
1615
JsonVariantType type;
1716
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-
}
15917
};
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+
}
16034
} // namespace Internals
16135
} // namespace ArduinoJson

src/ArduinoJson/DynamicJsonDocument.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class DynamicJsonDocument {
10000 3737

3838
void clear() {
3939
_memoryPool.clear();
40-
_rootData.setNull();
40+
_rootData.type = Internals::JSON_NULL;
4141
}
4242

4343
size_t memoryUsage() const {

src/ArduinoJson/JsonArray.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ class JsonArray {
2626
typedef JsonArrayIterator iterator;
2727

2828
FORCE_INLINE JsonArray() : _memoryPool(0), _data(0) {}
29-
FORCE_INLINE JsonArray(Internals::MemoryPool* buf,
29+
FORCE_INLINE JsonArray(Internals::MemoryPool* pool,
3030
Internals::JsonArrayData* arr)
31-
: _memoryPool(buf), _data(arr) {}
31+
: _memoryPool(pool), _data(arr) {}
32+
33+
operator JsonVariant() {
34+
using namespace Internals;
35+
return JsonVariant(_memoryPool, getVariantData(_data));
36+
}
3237

3338
// Adds the specified value at the end of the array.
3439
//

src/ArduinoJson/JsonObject.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class JsonObject {
2424
Internals::JsonObjectData* object)
2525
: _memoryPool(buf), _data(object) {}
2626

27+
operator JsonVariant() {
28+
using namespace Internals;
29+
return JsonVariant(_memoryPool, getVariantData(_data));
30+
}
31+
2732
FORCE_INLINE iterator begin() const {
2833
if (!_data) return iterator();
2934
return iterator(_memoryPool, _data->head);

0 commit comments

Comments
 (0)
0