Exemplo n.º 1
0
bool JSONParser::parseArray()
{
    int position;
    Array * array;
    
    position = mPosition;
    
    if (!scanCharacter('[', position)) {
        return false;
    }
    position ++;
    position = skipBlank(position);
    
    array = Array::array();
    
    if (scanCharacter(']', position)) {
        position ++;
    }
    else {
        while (1) {
            JSONParser * valueParser;
            Object * value;
            
            if (position >= mContent->length())
                return false;
            
            value = NULL;
            valueParser = new JSONParser();
            valueParser->setContent(mContent);
            valueParser->setPosition(position);
            valueParser->autorelease();
            if (!valueParser->parse())
                return false;
            
            value = valueParser->result();
            
            position = valueParser->position();
            array->addObject(value);
            
            position = skipBlank(position);
            
            if (scanCharacter(',', position)) {
                position ++;
                position = skipBlank(position);
            }
            else if (scanCharacter(']', position)) {
                position ++;
                break;
            }
            else {
                return false;
            }
        }
    }
    
    mResult = array->retain();
    mPosition = position;
    
    return true;
}
Exemplo n.º 2
0
bool JSONParser::parseDictionary()
{
    int position;
    HashMap * dict;
    
    position = mPosition;
    
    if (!scanCharacter('{', position)) {
        return false;
    }
    position ++;
    position = skipBlank(position);
    
    dict = HashMap::hashMap();
    
    if (scanCharacter('}', position)) {
        position ++;
    }
    else {
        while (1) {
            JSONParser * keyParser;
            JSONParser * valueParser;
            String * key;
            Object * value;
            
            if (position >= mContent->length())
                return false;
            
            key = NULL;
            keyParser = new JSONParser();
            keyParser->autorelease();
            keyParser->setContent(mContent);
            keyParser->setPosition(position);
            if (!keyParser->parse())
                return false;
            
            if (!keyParser->result()->className()->isEqual(MCSTR("mailcore::String")))
                return false;
            
            key = (String *) keyParser->result();
            position = keyParser->position();
            
            position = skipBlank(position);
            
            if (!scanCharacter(':', position))
                return false;
            position ++;
            
            position = skipBlank(position);
            
            value = NULL;
            valueParser = new JSONParser();
            valueParser->autorelease();
            valueParser->setContent(mContent);
            valueParser->setPosition(position);
            if (!valueParser->parse())
                return false;
            
            value = valueParser->result();
            position = valueParser->position();
            
            dict->setObjectForKey(key, value);
            
            position = skipBlank(position);
            
            if (scanCharacter(',', position)) {
                position ++;
                position = skipBlank(position);
            }
            else if (scanCharacter('}', position)) {
                position ++;
                break;
            }
            else {
                return false;
            }
        }
    }
    
    mResult = dict->retain();
    mPosition = position;
    
    return true;
}