Made JsonNode::type private

This commit is contained in:
Benoit Blanchon
2014-10-09 14:17:09 +02:00
parent b0e12e8852
commit 24c60619d5
10 changed files with 62 additions and 61 deletions

View File

@ -34,6 +34,41 @@ void JsonNode::writeTo(JsonWriter& writer)
} }
} }
void JsonNode::addChildToContainer(JsonNode* childToAdd)
{
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
JsonNode* lastChild = content.asContainer.child;
if (!lastChild)
{
content.asContainer.child = childToAdd;
return;
}
while (lastChild->next)
lastChild = lastChild->next;
lastChild->next = childToAdd;
}
void JsonNode::removeChildFromContainer(JsonNode* childToRemove)
{
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
if (content.asContainer.child == childToRemove)
{
content.asContainer.child = childToRemove->next;
return;
}
for (JsonNode* child = content.asContainer.child; child; child = child->next)
{
if (child->next == childToRemove)
child->next = childToRemove->next;
}
}
void JsonNode::writeArrayTo(JsonWriter& writer) void JsonNode::writeArrayTo(JsonWriter& writer)
{ {
JsonNode* child = content.asContainer.child; JsonNode* child = content.asContainer.child;

View File

@ -22,9 +22,13 @@ class JsonWriter;
struct JsonNode struct JsonNode
{ {
JsonNode* next; JsonNode()
JsonNodeType type; // <- TODO: hide : type(JSON_UNDEFINED), next(0)
{
}
JsonNode* next;
void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer
void setAsArray(JsonBuffer* buffer) void setAsArray(JsonBuffer* buffer)
@ -112,42 +116,13 @@ struct JsonNode
return type == JSON_KEY_VALUE ? content.asKey.value : 0; return type == JSON_KEY_VALUE ? content.asKey.value : 0;
} }
void addChildToContainer(JsonNode* childToAdd) void addChildToContainer(JsonNode* childToAdd);
{
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
JsonNode* lastChild = content.asContainer.child; void removeChildFromContainer(JsonNode* childToRemove);
if (!lastChild)
{
content.asContainer.child = childToAdd;
return;
}
while (lastChild->next)
lastChild = lastChild->next;
lastChild->next = childToAdd;
}
void removeChildFromContainer(JsonNode* childToRemove)
{
if (type != JSON_ARRAY && type != JSON_OBJECT) return;
if (content.asContainer.child == childToRemove)
{
content.asContainer.child = childToRemove->next;
return;
}
for (JsonNode* child = content.asContainer.child; child; child = child->next)
{
if (child->next == childToRemove)
child->next = childToRemove->next;
}
}
private: private:
JsonNodeType type; // <- TODO: hide
inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer inline void writeObjectTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer

View File

@ -43,7 +43,7 @@ void JsonArray::add(double value, int decimals)
void JsonArray::add(long value) void JsonArray::add(long value)
{ {
JsonNode* node = createNode(JSON_LONG); JsonNode* node = createNode();
if (!node) return; if (!node) return;
node->setAsLong(value); node->setAsLong(value);

View File

@ -1,5 +1,6 @@
#include "JsonBuffer.h" #include "JsonBuffer.h"
#include <new>
#include <string.h> // for memset #include <string.h> // for memset
#include "JsonObject.h" #include "JsonObject.h"
@ -8,16 +9,13 @@
JsonValue JsonBuffer::createValue() JsonValue JsonBuffer::createValue()
{ {
JsonNode* node = createNode(JSON_UNDEFINED); return JsonValue(createNode());
return JsonValue(node);
} }
JsonNode* JsonBuffer::createNode(JsonNodeType type) JsonNode* JsonBuffer::createNode()
{ {
JsonNode* node = allocateNode(); void* node = allocateNode();
if (!node) return 0; if (!node) return 0;
memset(node, 0, sizeof(JsonNode)); return new (node) JsonNode();
node->type = type;
return node;
} }

View File

@ -27,9 +27,9 @@ public:
JsonValue createValue(); JsonValue createValue();
protected: protected:
virtual JsonNode* allocateNode() = 0; virtual void* allocateNode() = 0;
private: private:
JsonNode* createNode(JsonNodeType type = JSON_UNDEFINED); JsonNode* createNode();
}; };

View File

@ -33,19 +33,14 @@ size_t JsonContainer::prettyPrintTo(IndentedPrint& p) const
return writer.bytesWritten(); return writer.bytesWritten();
} }
JsonNode* JsonContainer::createNode(JsonNodeType type) JsonNode* JsonContainer::createNode()
{ {
if (!_node) return 0; if (!_node) return 0;
JsonBuffer* buffer = _node->getContainerBuffer(); JsonBuffer* buffer = _node->getContainerBuffer();
if (!buffer) return 0; if (!buffer) return 0;
return buffer->createNode(type); return buffer->createNode();
}
bool JsonContainer::checkNodeType(JsonNodeType expectedType)
{
return _node && _node->type == expectedType;
} }
bool JsonContainer::operator==(const JsonContainer & other) const bool JsonContainer::operator==(const JsonContainer & other) const

View File

@ -53,9 +53,7 @@ protected:
void addChild(JsonNode*); void addChild(JsonNode*);
void removeChild(JsonNode*); void removeChild(JsonNode*);
JsonNode* createNode(JsonNodeType type = JSON_UNDEFINED); JsonNode* createNode();
bool checkNodeType(JsonNodeType expectedType);
JsonNode* _node; JsonNode* _node;
}; };

View File

@ -51,8 +51,6 @@ JsonObject JsonObject::createNestedObject(char const* key)
JsonNode* JsonObject::getOrCreateNodeAt(const char* key) JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
{ {
if (!checkNodeType(JSON_OBJECT)) return 0;
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{ {
const char* childKey = it->getAsObjectKey(); const char* childKey = it->getAsObjectKey();
@ -61,10 +59,10 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
return it->getAsObjectValue(); return it->getAsObjectValue();
} }
JsonNode* newValueNode = createNode(JSON_UNDEFINED); JsonNode* newValueNode = createNode();
if (!newValueNode) return 0; if (!newValueNode) return 0;
JsonNode* newKeyNode = createNode(JSON_KEY_VALUE); JsonNode* newKeyNode = createNode();
if (!newKeyNode) return 0; if (!newKeyNode) return 0;
newKeyNode->setAsObjectKeyValue(key, newValueNode); newKeyNode->setAsObjectKeyValue(key, newValueNode);

View File

@ -28,7 +28,7 @@ public:
} }
protected: protected:
virtual JsonNode* allocateNode() virtual void* allocateNode()
{ {
if (_size >= CAPACITY) return 0; if (_size >= CAPACITY) return 0;

View File

@ -104,6 +104,7 @@ TEST_F(JsonValueTests, ObjectsAreCopiedByReference)
jsonValue2 = jsonValue1; jsonValue2 = jsonValue1;
object["hello"] = "world"; object["hello"] = "world";
jsonValue1 = 0;
EXPECT_EQ(1, ((JsonObject) jsonValue2).size()); EXPECT_EQ(1, ((JsonObject) jsonValue2).size());
} }
@ -114,6 +115,7 @@ TEST_F(JsonValueTests, ArraysAreCopiedByReference)
jsonValue1 = array; jsonValue1 = array;
jsonValue2 = jsonValue1; jsonValue2 = jsonValue1;
jsonValue1 = 0;
array.add("world"); array.add("world");