Parse empty object

This commit is contained in:
Benoit Blanchon
2014-10-21 21:11:30 +02:00
parent 0daf82eee2
commit 9c1b6b80aa
8 changed files with 64 additions and 7 deletions

View File

@ -162,6 +162,11 @@ namespace ArduinoJson
return type == JSON_ARRAY; return type == JSON_ARRAY;
} }
bool isObject()
{
return type == JSON_OBJECT;
}
void addChild(JsonNode* childToAdd); void addChild(JsonNode* childToAdd);
void removeChild(JsonNode* childToRemove); void removeChild(JsonNode* childToRemove);

View File

@ -33,6 +33,8 @@ namespace ArduinoJson
inline bool isEnd(); inline bool isEnd();
inline bool isLong(); inline bool isLong();
inline bool isNull(); inline bool isNull();
inline bool isObjectStop();
inline bool isObjectStart();
inline bool isSpace(); inline bool isSpace();
inline void skipOneChar(); inline void skipOneChar();
@ -42,6 +44,7 @@ namespace ArduinoJson
inline JsonNode* parseBoolean(); inline JsonNode* parseBoolean();
inline JsonNode* parseLong(); inline JsonNode* parseLong();
inline JsonNode* parseNull(); inline JsonNode* parseNull();
inline JsonNode* parseObject();
inline JsonNode* parseString(); inline JsonNode* parseString();
JsonNode *parseDouble(); JsonNode *parseDouble();

View File

@ -32,8 +32,8 @@ namespace ArduinoJson
JsonValue createValue(); JsonValue createValue();
JsonArray parseArray(char* json); JsonArray parseArray(char* json);
JsonObject parseObject(char* json);
JsonValue parseValue(char* json); JsonValue parseValue(char* json); // TODO: remove
protected: protected:
virtual void* allocateNode() = 0; virtual void* allocateNode() = 0;

View File

@ -23,6 +23,11 @@ namespace ArduinoJson
JsonArray createNestedArray(const char* key); JsonArray createNestedArray(const char* key);
JsonObject createNestedObject(const char* key); JsonObject createNestedObject(const char* key);
bool success()
{
return _node && _node->isObject();
}
private: private:
Internals::JsonNode* getOrCreateNodeAt(const char* key); Internals::JsonNode* getOrCreateNodeAt(const char* key);
}; };

View File

@ -166,3 +166,4 @@ void JsonNode::duplicate(JsonNode* other)
*this = *other; *this = *other;
} }
} }

View File

@ -18,6 +18,16 @@ bool JsonParser::isArrayStop()
return *_ptr == ']'; return *_ptr == ']';
} }
bool JsonParser::isObjectStart()
{
return *_ptr == '{';
}
bool JsonParser::isObjectStop()
{
return *_ptr == '}';
}
bool JsonParser::isBoolean() bool JsonParser::isBoolean()
{ {
return *_ptr == 't' || *_ptr == 'f'; return *_ptr == 't' || *_ptr == 'f';
@ -104,13 +114,15 @@ JsonNode* JsonParser::parseAnything()
if (isNull()) if (isNull())
return parseNull(); return parseNull();
if (isObjectStart())
return parseObject();
return parseString(); return parseString();
} }
JsonNode* JsonParser::parseArray() JsonNode* JsonParser::parseArray()
{ {
JsonNode* node = _buffer->createNode(); JsonNode* node = _buffer->createArrayNode();
node->setAsArray(_buffer);
skipOneChar(); // skip the '[' skipOneChar(); // skip the '['
skipSpaces(); skipSpaces();
@ -137,6 +149,12 @@ JsonNode* JsonParser::parseArray()
} }
} }
JsonNode* JsonParser::parseObject()
{
_ptr+=2;
return _buffer->createObjectNode();
}
JsonNode *JsonParser::parseBoolean() JsonNode *JsonParser::parseBoolean()
{ {
bool value = *_ptr == 't'; bool value = *_ptr == 't';
@ -168,8 +186,7 @@ JsonNode* JsonParser::parseLong()
JsonNode* JsonParser::parseNull() JsonNode* JsonParser::parseNull()
{ {
_ptr += 4; _ptr += 4; // strlen("null")
// 4 = strlen("null")
return _buffer->createStringNode(0); return _buffer->createStringNode(0);
} }

View File

@ -28,6 +28,12 @@ JsonArray JsonBuffer::parseArray(char* json)
return JsonArray(parser.parseAnything()); return JsonArray(parser.parseAnything());
} }
JsonObject JsonBuffer::parseObject(char* json)
{
JsonParser parser(this, json);
return JsonObject(parser.parseAnything());
}
JsonValue JsonBuffer::parseValue(char* json) JsonValue JsonBuffer::parseValue(char* json)
{ {
JsonParser parser(this, json); JsonParser parser(this, json);

View File

@ -0,0 +1,20 @@
#include <gtest/gtest.h>
#include <ArduinoJson/StaticJsonBuffer.hpp>
using namespace ArduinoJson;
class JsonParser_Object_Test : public testing::Test
{
protected:
};
TEST_F(JsonParser_Object_Test, EmptyObject)
{
StaticJsonBuffer<10> jsonBuffer;
char jsonString[] = "{}";
JsonObject object = jsonBuffer.parseObject(jsonString);
EXPECT_TRUE(object.success());
}