Test that nested JsonObject can be stored

This commit is contained in:
Benoit Blanchon
2014-09-27 16:18:40 +02:00
parent bcc8cece24
commit bc44c36385
9 changed files with 114 additions and 5 deletions

View File

@ -1,8 +1,10 @@
#include "JsonBuffer.h"
#include "JsonNode.h"
#include "JsonObject.h"
#include <string.h> // for memset
#include "JsonNode.h"
#include "JsonObject.h"
#include "JsonValue.h"
JsonObject JsonBuffer::createObject()
{
@ -14,6 +16,12 @@ JsonObject JsonBuffer::createObject()
return JsonObject(node);
}
JsonValue JsonBuffer::createValue()
{
JsonNode* node = createNode(JSON_UNDEFINED);
return JsonValue(node);
}
JsonNode* JsonBuffer::createNode(JsonNodeType type)
{
JsonNode* node = allocateNode();

View File

@ -4,6 +4,7 @@
#include "JsonNode.h"
class JsonObject;
class JsonValue;
struct JsonNode;
class JsonBuffer
@ -14,6 +15,8 @@ public:
// virtual ~JsonBuffer() = 0;
JsonObject createObject();
JsonValue createValue();
protected:
virtual JsonNode* allocateNode() = 0;

View File

@ -5,6 +5,8 @@ struct JsonNode;
class JsonObject
{
friend JsonValue;
public:
JsonObject()
: _node(0)
@ -20,6 +22,11 @@ public:
JsonValue operator[](const char* key);
bool operator== (const JsonObject& other) const
{
return _node == other._node;
}
private:
JsonNode* _node;

View File

@ -34,6 +34,13 @@ void JsonValue::operator=(int value)
_node->content.asInteger = value;
}
void JsonValue::operator=(const JsonObject& object)
{
if (!_node) return;
_node = object._node;
}
JsonValue::operator bool() const
{
if (!_node || _node->type != JSON_BOOLEAN) return 0;
@ -60,4 +67,9 @@ JsonValue::operator int() const
if (!_node || _node->type != JSON_INTEGER) return 0;
return _node->content.asInteger;
}
JsonValue::operator JsonObject() const
{
return JsonObject(_node);
}

View File

@ -1,12 +1,16 @@
#pragma once
struct JsonNode;
//class JsonBuffer;
class JsonValue
{
public:
explicit JsonValue()
: _node(0)
{
}
explicit JsonValue(JsonNode* node)
: _node(node)
{
@ -16,14 +20,15 @@ public:
void operator=(const char*);
void operator=(double);
void operator=(int);
void operator=(const JsonObject&);
operator bool() const;
operator const char*() const;
operator double() const;
operator int() const;
operator JsonObject() const;
private:
//JsonBuffer& _buffer;
JsonNode* _node;
};