Parse invalid array

This commit is contained in:
Benoit Blanchon
2014-10-14 17:28:57 +02:00
parent f468db6757
commit 081b345e7c
5 changed files with 51 additions and 20 deletions

View File

@ -147,3 +147,20 @@ void JsonNode::setAsProxyOfSelf()
setAsProxyOf(newNode); setAsProxyOf(newNode);
} }
void JsonNode::duplicate(JsonNode* other)
{
if (!other)
{
type = JSON_UNDEFINED;
}
else if (other->type == JSON_ARRAY || other->type==JSON_OBJECT)
{
other->setAsProxyOfSelf();
setAsProxyOf(other->content.asProxy.target);
}
else
{
*this = *other;
}
}

View File

@ -152,26 +152,16 @@ public:
return type == JSON_PROXY ? content.asProxy.target : this; return type == JSON_PROXY ? content.asProxy.target : this;
} }
bool isArray()
{
return type == JSON_ARRAY;
}
void addChild(JsonNode* childToAdd); void addChild(JsonNode* childToAdd);
void removeChild(JsonNode* childToRemove); void removeChild(JsonNode* childToRemove);
void duplicate(JsonNode* other) void duplicate(JsonNode* other);
{
if (!other)
{
type = JSON_UNDEFINED;
}
else if (other->type == JSON_ARRAY || other->type==JSON_OBJECT)
{
other->setAsProxyOfSelf();
setAsProxyOf(other->content.asProxy.target);
}
else
{
*this = *other;
}
}
private: private:
JsonNode* next; JsonNode* next;

View File

@ -25,5 +25,10 @@ public:
JsonArray createNestedArray(); JsonArray createNestedArray();
JsonObject createNestedObject(); JsonObject createNestedObject();
bool success()
{
return _node && _node->isArray();
}
}; };

View File

@ -1,9 +1,7 @@
#include "JsonBuffer.h" #include "JsonBuffer.h"
#include <new> #include <new>
#include <string.h> // for memset
#include "JsonObject.h"
#include "JsonValue.h" #include "JsonValue.h"
#include "Internals/JsonNode.h" #include "Internals/JsonNode.h"
@ -20,7 +18,19 @@ JsonNode* JsonBuffer::createNode()
return new (node) JsonNode(); return new (node) JsonNode();
} }
JsonArray JsonBuffer::parseArray(char const *string) JsonArray JsonBuffer::parseArray(const char* json)
{ {
return JsonArray(); JsonNode* root;
if (json[0] == '[')
{
root = createNode();
root->setAsArray(this);
}
else
{
root = 0;
}
return JsonArray(root);
} }

View File

@ -11,5 +11,14 @@ TEST_F(JsonArray_Parser_Tests, EmptyArray)
{ {
JsonArray array = json.parseArray("[]"); JsonArray array = json.parseArray("[]");
EXPECT_TRUE(array.success());
EXPECT_EQ(0, array.size());
}
TEST_F(JsonArray_Parser_Tests, Garbage)
{
JsonArray array = json.parseArray("%*$£¤");
EXPECT_FALSE(array.success());
EXPECT_EQ(0, array.size()); EXPECT_EQ(0, array.size());
} }