From 12e374d0dac0003e64ebf9389e0176bfc12cb63f Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 8 Nov 2014 21:37:02 +0100 Subject: [PATCH] Reduced size by 2 bytes --- src/JsonObject.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/JsonObject.cpp b/src/JsonObject.cpp index 02c6b760..d90f6854 100644 --- a/src/JsonObject.cpp +++ b/src/JsonObject.cpp @@ -29,16 +29,19 @@ const JsonVariant &JsonObject::at(const char *key) const { } JsonVariant &JsonObject::operator[](const char *key) { - node_type *existingNode = getNodeAt(key); - if (existingNode) return existingNode->content.value; + // try to find an existing node + node_type *node = getNodeAt(key); - node_type *newNode = createNode(); - if (!newNode) return JsonVariant::invalid(); + // not fount => create a new one + if (!node) { + node = createNode(); + if (!node) return JsonVariant::invalid(); - newNode->content.key = key; - addNode(newNode); + node->content.key = key; + addNode(node); + } - return newNode->content.value; + return node->content.value; } void JsonObject::remove(char const *key) { removeNode(getNodeAt(key)); }