8000 User can now use a JsonString as a key or a value · java64/ArduinoJson@b184af6 · GitHub
[go: up one dir, main page]

Skip to content

Commit b184af6

Browse files
committed
User can now use a JsonString as a key or a value
1 parent 6f55d1e commit b184af6

29 files changed

+509
-385
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ HEAD
2828
* `JsonDocument` now support the same operations as `JsonVariant`.
2929
Calling `JsonDocument::as<T>()` is not required anymore.
3030
* Fixed example `JsonHttpClient.ino`
31+
* User can now use a `JsonString` as a key or a value
3132

3233
> ### BREAKING CHANGES
3334
>

src/ArduinoJson/Collection/CollectionData.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class CollectionData {
2424

2525
VariantData *add(MemoryPool *pool);
2626

27-
template <typename TKey>
28-
VariantData *add(TKey key, MemoryPool *pool);
27+
template <typename TAdaptedString>
28+
VariantData *add(TAdaptedString key, MemoryPool *pool);
2929

3030
void clear();
3131

32-
template <typename TKey>
33-
bool containsKey(const TKey &key) const;
32+
template <typename TAdaptedString>
33+
bool containsKey(const TAdaptedString &key) const;
3434

3535
bool copyFrom(const CollectionData &src, MemoryPool *pool);
3636

@@ -39,17 +39,17 @@ class CollectionData {
3939

4040
VariantData *get(size_t index) const;
4141

42-
template <typename TKey>
43-
VariantData *get(TKey key) const;
42+
template <typename TAdaptedString>
43+
VariantData *get(TAdaptedString key) const;
4444

4545
VariantSlot *head() const {
4646
return _head;
4747
}
4848

4949
void remove(size_t index);
5050

51-
template <typename TKey>
52-
void remove(TKey key) {
51+
template <typename TAdaptedString>
52+
void remove(TAdaptedString key) {
5353
remove(getSlot(key));
5454
}
5555

@@ -62,8 +62,8 @@ class CollectionData {
6262
private:
6363
VariantSlot *getSlot(size_t index) const;
6464

65-
template <typename TKey>
66-
VariantSlot *getSlot(TKey key) const;
65+
template <typename TAdaptedString>
66+
VariantSlot *getSlot(TAdaptedString key) const;
6767

6868
VariantSlot *getPreviousSlot(VariantSlot *) const;
6969
};

src/ArduinoJson/Collection/CollectionImpl.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ inline VariantData* CollectionData::add(MemoryPool* pool) {
2929
return addSlot(pool)->data();
3030
}
3131

32-
template <typename TKey>
33-
inline VariantData* CollectionData::add(TKey key, MemoryPool* pool) {
32+
template <typename TAdaptedString>
33+
inline VariantData* CollectionData::add(TAdaptedString key, MemoryPool* pool) {
3434
VariantSlot* slot = addSlot(pool);
3535
if (!slotSetKey(slot, key, pool)) return 0;
3636
return slot->data();
@@ -41,8 +41,8 @@ inline void CollectionData::clear() {
4141
_tail = 0;
4242
}
4343

44-
template <typename TKey>
45-
inline bool CollectionData::containsKey(const TKey& key) const {
44+
template <typename TAdaptedString>
45+
inline bool CollectionData::containsKey(const TAdaptedString& key) const {
4646
return getSlot(key) != 0;
4747
}
4848

@@ -53,9 +53,9 @@ inline bool CollectionData::copyFrom(const CollectionData& src,
5353
VariantData* var;
5454
if (s->key() != 0) {
5555
if (s->ownsKey())
56-
var = add(RamStringWrapper(s->key()), pool);
56+
var = add(RamStringAdapter(s->key()), pool);
5757
else
58-
var = add(ConstRamStringWrapper(s->key()), pool);
58+
var = add(ConstRamStringAdapter(s->key()), pool);
5959
} else {
6060
var = add(pool);
6161
}
@@ -69,7 +69,7 @@ inline bool CollectionData::equalsObject(const CollectionData& other) const {
6969
size_t count = 0;
7070
for (VariantSlot* slot = _head; slot; slot = slot->next()) {
7171
VariantData* v1 = slot->data();
72-
VariantData* v2 = other.get(wrapString(slot->key()));
72+
VariantData* v2 = other.get(adaptString(slot->key()));
7373
if (!variantEquals(v1, v2)) return false;
7474
count++;
7575
}
@@ -88,8 +88,8 @@ inline bool CollectionData::equalsArray(const CollectionData& other) const {
8888
}
8989
}
9090

91-
template <typename TKey>
92-
inline VariantSlot* CollectionData::getSlot(TKey key) const {
91+
template <typename TAdaptedString>
92+
inline VariantSlot* CollectionData::getSlot(TAdaptedString key) const {
9393
VariantSlot* slot = _head;
9494
while (slot) {
9595
if (key.equals(slot->key())) break;
@@ -112,8 +112,8 @@ inline VariantSlot* CollectionData::getPreviousSlot(VariantSlot* target) const {
112112
return 0;
113113
}
114114

115-
template <typename TKey>
116-
inline VariantData* CollectionData::get(TKey key) const {
115+
template <typename TAdaptedString>
116+
inline VariantData* CollectionData::get(TAdaptedString key) const {
117117
VariantSlot* slot = getSlot(key);
118118
return slot ? slot->data() : 0;
119119
}

src/ArduinoJson/Deserialization/deserialize.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ TDeserializer<TReader, TWriter> makeDeserializer(MemoryPool &pool,
2323
return TDeserializer<TReader, TWriter>(pool, reader, writer, nestingLimit);
2424
}
2525

26-
// DeserializationError deserialize(JsonDocument& doc, TString input);
27-
// TString = const std::string&, const String&
26+
// deserialize(JsonDocument&, const std::string&);
27+
// deserialize(JsonDocument&, const String&);
2828
template <template <typename, typename> class TDeserializer, typename TString>
2929
typename enable_if<!is_array<TString>::value, DeserializationError>::type
3030
deserialize(JsonDocument &doc, const TString &input,
@@ -36,8 +36,9 @@ deserialize(JsonDocument &doc, const TString &input,
3636
.parse(doc.data());
3737
}
3838
//
39-
// DeserializationError deserialize(JsonDocument& doc, TChar* input);
40-
// TChar* = char*, const char*, const __FlashStringHelper*
39+
// deserialize(JsonDocument&, char*);
40+
// deserialize(JsonDocument&, const char*);
41+
// deserialize(JsonDocument&, const __FlashStringHelper*);
4142
template <template <typename, typename> class TDeserializer, typename TChar>
4243
DeserializationError deserialize(JsonDocument &doc, TChar *input,
4344
NestingLimit nestingLimit) {
@@ -48,9 +49,9 @@ DeserializationError deserialize(JsonDocument &doc, TChar *input,
4849
.parse(doc.data());
4950
}
5051
//
51-
// DeserializationError deserialize(JsonDocument& doc, TChar* input, size_t
52-
// inputSize);
53-
// TChar* = char*, const char*, const __FlashStringHelper*
52+
// deserialize(JsonDocument&, char*, size_t);
53+
// deserialize(JsonDocument&, const char*, size_t);
54+
// deserialize(JsonDocument&, const __FlashStringHelper*, size_t);
5455
template <template <typename, typename> class TDeserializer, typename TChar>
5556
DeserializationError deserialize(JsonDocument &doc, TChar *input,
5657
size_t inputSize, NestingLimit nestingLimit) {
@@ -61,8 +62,8 @@ DeserializationError deserialize(JsonDocument &doc, TChar *input,
6162
.parse(doc.data());
6263
}
6364
//
64-
// DeserializationError deserialize(JsonDocument& doc, TStream input);
65-
// TStream = std::istream&, Stream&
65+
// deserialize(JsonDocument&, std::istream&);
66+
// deserialize(JsonDocument&, Stream&);
6667
template <template <typename, typename> class < 179B span class="pl-en">TDeserializer, typename TStream>
6768
DeserializationError deserialize(JsonDocument &doc, TStream &input,
6869
NestingLimit nestingLimit) {

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -86,62 +86,76 @@ class JsonDocument : public Visitable {
8686
return add().to<ArrayRef>();
8787
}
8888

89-
template <typename TKey>
90-
ArrayRef createNestedArray(TKey* key) {
89+
// createNestedArray(char*)
90+
// createNestedArray(const char*)
91+
// createNestedArray(const __FlashStringHelper*)
92+
template <typename TChar>
93+
ArrayRef createNestedArray(TChar* key) {
9194
return getOrCreate(key).template to<ArrayRef>();
9295
}
9396

94-
template <typename TKey>
95-
ArrayRef createNestedArray(const TKey& key) {
97+
// createNestedArray(const std::string&)
98+
// createNestedArray(const String&)
99+
template <typename TString>
100+
ArrayRef createNestedArray(const TString& key) {
96101
return getOrCreate(key).template to<ArrayRef>();
97102
}
98103

99104
ObjectRef createNestedObject() {
100105
return add().to<ObjectRef>();
101106
}
102107

103-
template <typename TKey>
104-
ObjectRef createNestedObject(TKey* key) {
108+
// createNestedObject(char*)
109+
// createNestedObject(const char*)
110+
// createNestedObject(const __FlashStringHelper*)
111+
template <typename TChar>
112+
ObjectRef createNestedObject(TChar* key) {
105113
return getOrCreate(key).template to<ObjectRef>();
106114
}
107115

108-
template <typename TKey>
109-
ObjectRef createNestedObject(const TKey& key) {
116+
// createNestedObject(const std::string&)
117+
// createNestedObject(const String&)
118+
template <typename TString>
119+
ObjectRef createNestedObject(const TString& key) {
110120
return getOrCreate(key).template to<ObjectRef>();
111121
}
112122

113-
// MemberProxy operator[](TKey)
114-
// TKey = const std::string&, const String&
115-
template <typename TKey>
123+
// operator[](const std::string&)
124+
// operator[](const String&)
125+
template <typename TString>
116126
FORCE_INLINE
117-
typename enable_if<IsString<TKey>::value,
118-
MemberProxy<JsonDocument&, const TKey&> >::type
119-
operator[](const TKey& key) {
120-
return MemberProxy<JsonDocument&, const TKey&>(*this, key);
121-
}
122-
123-
// MemberProxy operator[](TKey);
124-
// TKey = const char*, const char[N], const __FlashStringHelper*
125-
template <typename TKey>
126-
FORCE_INLINE typename enable_if<IsString<TKey*>::value,
127-
MemberProxy<JsonDocument&, TKey*> >::type
128-
operator[](TKey* key) {
129-
return MemberProxy<JsonDocument&, TKey*>(*this, key);
130-
}
131-
132-
// VariantConstRef operator[](TKey) const
133-
// TKey = const std::string&, const String&
134-
template <typename TKey>
135-
FORCE_INLINE typename enable_if<IsString<TKey>::value, VariantConstRef>::type
136-
operator[](const TKey& key) const {
127+
typename enable_if<IsString<TString>::value,
128+
MemberProxy<JsonDocument&, const TString&> >::type
129+
operator[](const TString& key) {
130+
return MemberProxy<JsonDocument&, const TString&>(*this, key);
131+
}
132+
133+
// operator[](char*)
134+
// operator[](const char*)
135+
// operator[](const __FlashStringHelper*)
136+
template <typename TChar>
137+
FORCE_INLINE typename enable_if<IsString<TChar*>::value,
138+
MemberProxy<JsonDocument&, TChar*> >::type
139+
operator[](TChar* key) {
140+
return MemberProxy<JsonDocument&, TChar*>(*this, key);
141+
}
142+
143+
// operator[](const std::string&) const
144+
// operator[](const String&) const
145+
template <typename TString>
146+
FORCE_INLINE
147+
typename enable_if<IsString<TString>::value, VariantConstRef>::type
148+
operator[](const TString& key) const {
137149
return getVariant()[key];
138150
}
139151

140-
// VariantConstRef operator[](TKey) const;
141-
// TKey = const char*, const char[N], const __FlashStringHelper*
142-
template <typename TKey>
143-
FORCE_INLINE typename enable_if<IsString<TKey*>::value, VariantConstRef>::type
144-
operator[](TKey* key) const {
152+
// operator[](char*) const
153+
// operator[](const char*) const
154+
// operator[](const __FlashStringHelper*) const
155+
template <typename TChar>
156+
FORCE_INLINE
157+
typename enable_if<IsString<TChar*>::value, VariantConstRef>::type
158+
operator[](TChar* key) const {
145159
return getVariant()[key];
146160
}
147161

@@ -157,43 +171,51 @@ class JsonDocument : public Visitable {
157171
return VariantRef(&_pool, _data.get(index));
158172
}
159173

160-
template <typename TKey>
161-
FORCE_INLINE VariantRef get(TKey* key) {
162-
return VariantRef(&_pool, _data.get(wrapString(key)));
174+
// get(char*) const
175+
// get(const char*) const
176+
// get(const __FlashStringHelper*) const
177+
template <typename TChar>
178+
FORCE_INLINE VariantRef get(TChar* key) {
179+
return VariantRef(&_pool, _data.get(adaptString(key)));
163180
}
164181

165-
template <typename TKey>
166-
FORCE_INLINE typename enable_if<IsString<TKey>::value, VariantRef>::type get(
167-
const TKey& key) {
168-
return VariantRef(&_pool, _data.get(wrapString(key)));
182+
// get(const std::string&) const
183+
// get(const String&) const
184+
template <typename TString>
185+
FORCE_INLINE typename enable_if<IsString<TString>::value, VariantRef>::type
186+
get(const TString& key) {
187+
return VariantRef(&_pool, _data.get(adaptString(key)));
169188
}
170189

171-
template <typename TKey>
172-
FORCE_INLINE VariantRef getOrCreate(TKey* key) {
173-
return VariantRef(&_pool, _data.getOrCreate(wrapString(key), &_pool));
190+
// getOrCreate(char*)
191+
// getOrCreate(const char*)
192+
// getOrCreate(const __FlashStringHelper*)
193+
template <typename TChar>
194+
FORCE_INLINE VariantRef getOrCreate(TChar* key) {
195+
return VariantRef(&_pool, _data.getOrCreate(adaptString(key), &_pool));
174196
}
175197

176-
template <typename TKey>
177-
FORCE_INLINE VariantRef getOrCreate(const TKey& key) {
178-
return VariantRef(&_pool, _data.getOrCreate(wrapString(key), &_pool));
198+
// getOrCreate(const std::string&)
199+
// getOrCreate(const String&)
200+
template <typename TString>
201+
FORCE_INLINE VariantRef getOrCreate(const TString& key) {
202+
return VariantRef(&_pool, _data.getOrCreate(adaptString(key), &_pool));
179203
}
180204

181205
FORCE_INLINE VariantRef add() {
182206
return VariantRef(&_pool, _data.add(&_pool));
183207
}
184-
//
185-
// bool add(TValue);
186-
// TValue = bool, long, int, short, float, double, serialized, VariantRef,
187-
// std::string, String, ObjectRef
188-
template <typename T>
189-
FORCE_INLINE bool add(const T& value) {
208+
209+
template <typename TValue>
210+
FORCE_INLINE bool add(const TValue& value) {
190211
return add().set(value);
191212
}
192-
//
193-
// bool add(TValue);
194-
// TValue = char*, const char*, const __FlashStringHelper*
195-
template <typename T>
196-
FORCE_INLINE bool add(T* value) {
213+
214+
// add(char*) const
215+
// add(const char*) const
216+
// add(const __FlashStringHelper*) const
217+
template <typename TChar>
218+
FORCE_INLINE bool add(TChar* value) {
197219
return add().set(value);
198220
}
199221

src/ArduinoJson/Misc/SerializedValue.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#pragma once
66

7-
#include "../Strings/StringWrappers.hpp"
7+
#include "../Strings/StringAdapters.hpp"
88

99
namespace ARDUINOJSON_NAMESPACE {
1010

@@ -58,7 +58,7 @@ inline SerializedValue<T> serialized(T str) {
5858

5959
template <typename TChar>
6060
inline SerializedValue<TChar*> serialized(TChar* p) {
61-
return SerializedValue<TChar*>(p, wrapString(p).size());
61+
return SerializedValue<TChar*>(p, adaptString(p).size());
6262
}
6363

6464
template <typename TChar>

0 commit comments

Comments
 (0)
0