Exemple #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;
}
Exemple #2
0
void App::readConfig()
{
	string configFileName = config()->get(CPPAPP_CONFIG_FILE_CFG_KEY,
								   getDefaultConfigFile())->asString();
	
	Ref<FileInput> configInput = new FileInput(configFileName);
	if (!configInput->exists()) {
		LOG_WARNING("Configuration file " << configFileName << " does not exist.");
		return;
	}
	
	JSONParser parser;
	config_ = parser.parse(configInput);
	configInput->close();
	
	if (config_->isError()) {
		Ref<DynError> err = config_.as<DynError>();
		LOG_ERROR("Syntax error in config file at " << err->getErrorLoc() << ": " << err->getString());
		exit(1);
	}
	
	//Ref<PrettyPrinter> printer = new PrettyPrinter();
	//config_->print(printer);
	//printer->print("\n");
	
	// Override the configuration read from the file by the command line
	// options.
	options().setConfigKeys(config_);
}
Object * JSONParser::objectFromString(String * str)
{
	Object * result;
	JSONParser * parser;
	
	parser = new JSONParser();
    parser->setContent(str);
	parser->parse();
	result = parser->result();
    result->retain()->autorelease();
    parser->release();
	
	return result;
}
Exemple #4
0
bool Round::RemoteNode::postMessage(uHTTP::HTTPRequest *httpReq, JSONObject **rootObj, Error *error) {
  // HTTP Request
  
  std::string requestAddr;
  int requestPort;
  if (!getRequestAddress(&requestAddr, error) || !getRequestPort(&requestPort, error))
    return false;
  
  httpReq->setHost(requestAddr, requestPort);
  
  uHTTP::HTTPResponse httpRes;
  httpReq->post(requestAddr, requestPort, &httpRes);
  
  // Set error code and message
  
  int statusCode = httpRes.getStatusCode();
  if (!uHTTP::HTTP::IsStatusCodeSuccess(statusCode)) {
    error->setCode(statusCode);
    error->setMessage(uHTTP::HTTP::StatusCodeToString(statusCode));
    return false;
  }
  
  std::string httpContent = httpRes.getContent();
  if (httpContent.length() <= 0) {
    statusCode = uHTTP::HTTP::NOT_FOUND;
    error->setCode(statusCode);
    error->setMessage(uHTTP::HTTP::StatusCodeToString(statusCode));
    return false;
  }
  
  JSONParser jsonParser;
  if (jsonParser.parse(httpContent, error) == false) {
    RPC::JSON::ErrorCodeToError(RPC::JSON::ErrorCodeParserError, error);
    return false;
  }
  
  *rootObj = jsonParser.popRootObject();
  if (!(*rootObj)) {
    RPC::JSON::ErrorCodeToError(RPC::JSON::ErrorCodeParserError, error);
    return false;
  }

  return true;
}
Exemple #5
0
bool Round::set_alias::exec(LocalNode *node, const NodeRequest *nodeReq, NodeResponse *nodeRes) const {
  std::string params;
  if (!nodeReq->getParams(&params))
    return false;
  
  JSONParser jsonParser;
  Error error;
  if (!jsonParser.parse(params, &error))
    return false;
  
  JSONDictionary *paramDict = dynamic_cast<JSONDictionary *>(jsonParser.getRootObject());
  if (!paramDict)
    return false;
  
  // Essential Parameters
  
  std::string name, method;
  
  if (!paramDict->get(ALIAS_NAME, &name))
    return false;
  
  if (!paramDict->get(ALIAS_METHOD, &method))
    return false;
  
  if ((name.length() <= 0) || (method.length() <= 0))
    return false;

  // Optional Parameters
  
  std::string defaults;
  paramDict->get(ALIAS_DEFAULTS, &defaults);

  // Set alias
  
  Alias *alias = new Alias();
  
  alias->setName(name);
  alias->setObject(method);
  alias->setDefaults(defaults);
  
  return node->setAlias(alias);
}
Exemple #6
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;
}