Skip to content

Commit a37480e

Browse files
committed
Fixed deserializeJson() when input contains duplicate keys (fixes #1095)
1 parent 713aaa3 commit a37480e

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ HEAD
66

77
* Added support for custom writer classes (issue #1088)
88
* Added conversion from `JsonArray` and `JsonObject` to `bool`, to be consistent with `JsonVariant`
9+
* Fixed `deserializeJson()` when input contains duplicate keys (issue #1095)
910

1011
v6.12.0 (2019-09-05)
1112
-------

extras/tests/JsonDeserializer/object.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ TEST_CASE("deserialize JSON object") {
277277
DeserializationError err = deserializeJson(doc, "{a:{b:{c:1}},a:2}");
278278

279279
REQUIRE(err == DeserializationError::Ok);
280+
REQUIRE(doc["a"] == 2);
280281
}
281282
}
282283

src/ArduinoJson/Json/JsonDeserializer.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,21 @@ class JsonDeserializer {
130130

131131
// Read each key value pair
132132
for (;;) {
133-
// Allocate slot in object
134-
VariantSlot *slot = object.addSlot(_pool);
135-
if (!slot) return DeserializationError::NoMemory;
136-
137133
// Parse key
138134
const char *key;
139135
err = parseKey(key);
140136
if (err) return err;
141-
slot->setOwnedKey(make_not_null(key));
137+
138+
VariantData *variant = object.get(adaptString(key));
139+
if (!variant) {
140+
// Allocate slot in object
141+
VariantSlot *slot = object.addSlot(_pool);
142+
if (!slot) return DeserializationError::NoMemory;
143+
144+
slot->setOwnedKey(make_not_null(key));
145+
146+
variant = slot->data();
147+
}
142148

143149
// Skip spaces
144150
err = skipSpacesAndComments();
@@ -147,7 +153,7 @@ class JsonDeserializer {
147153

148154
// Parse value
149155
_nestingLimit--;
150-
err = parseVariant(*slot->data());
156+
err = parseVariant(*variant);
151157
_nestingLimit++;
152158
if (err) return err;
153159

0 commit comments

Comments
 (0)