Parse key value pairs

This commit is contained in:
Benoit Blanchon
2014-10-21 23:37:17 +02:00
parent cfbe50057a
commit 04330a7a47
7 changed files with 103 additions and 31 deletions

View File

@ -119,9 +119,9 @@ void JsonNode::writeObjectTo(JsonWriter& writer)
for (;;)
{
writer.writeString(child->content.asKey.key);
writer.writeString(child->content.asKeyValue.key);
writer.writeColon();
child->content.asKey.value->writeTo(writer);
child->content.asKeyValue.value->writeTo(writer);
child = child->next;
if (!child) break;

View File

@ -18,16 +18,6 @@ bool JsonParser::isArrayStop()
return *_ptr == ']';
}
bool JsonParser::isObjectStart()
{
return *_ptr == '{';
}
bool JsonParser::isObjectStop()
{
return *_ptr == '}';
}
bool JsonParser::isBoolean()
{
return *_ptr == 't' || *_ptr == 'f';
@ -38,6 +28,11 @@ bool JsonParser::isComma()
return *_ptr == ',';
}
bool JsonParser::isColon()
{
return *_ptr == ':';
}
bool JsonParser::isDouble()
{
char* ptr = _ptr;
@ -80,6 +75,16 @@ bool JsonParser::isNull()
return *_ptr == 'n';
}
bool JsonParser::isObjectStart()
{
return *_ptr == '{';
}
bool JsonParser::isObjectStop()
{
return *_ptr == '}';
}
bool JsonParser::isSpace()
{
return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r';
@ -131,7 +136,10 @@ JsonNode* JsonParser::parseArray()
return 0;
if (isArrayStop())
{
skipOneChar(); // skip the ']'
return node;
}
for(;;)
{
@ -149,17 +157,6 @@ JsonNode* JsonParser::parseArray()
}
}
JsonNode* JsonParser::parseObject()
{
JsonNode* node = _buffer->createObjectNode();
skipOneChar(); // skip the '['
skipSpaces();
if (isObjectStop())
return node;
}
JsonNode *JsonParser::parseBoolean()
{
bool value = *_ptr == 't';
@ -196,6 +193,55 @@ JsonNode* JsonParser::parseNull()
return _buffer->createStringNode(0);
}
JsonNode* JsonParser::parseObject()
{
JsonNode* node = _buffer->createObjectNode();
skipOneChar(); // skip the '{'
skipSpaces();
if (isEnd())
return 0;
if (isObjectStop())
{
skipOneChar(); // skip the '}'
return node;
}
for(;;)
{
node->addChild(parseObjectKeyValue());
skipSpaces();
if (isObjectStop())
return node;
if (!isComma())
return 0;
skipOneChar(); // skip the ','
}
}
JsonNode* JsonParser::parseObjectKeyValue()
{
const char* key = QuotedString::extractFrom(_ptr, &_ptr);
skipSpaces();
if (!isColon())
return 0;
skipOneChar(); // skip the :
skipSpaces();
JsonNode* value = parseAnything();
return _buffer->createObjectKeyValueNode(key, value);
}
JsonNode* JsonParser::parseString()
{
const char* s = QuotedString::extractFrom(_ptr, &_ptr);

View File

@ -90,6 +90,16 @@ JsonNode* JsonBuffer::createObjectNode()
return node;
}
Internals::JsonNode* JsonBuffer::createObjectKeyValueNode(const char* key, JsonNode* value)
{
JsonNode* node = createNode();
if (node)
node->setAsObjectKeyValue(key, value);
return node;
}
JsonNode* JsonBuffer::createStringNode(const char* value)
{
JsonNode* node = createNode();