示例#1
0
void ConvertTo(json_spirit::Value& value, bool fAllowNull=false)
{
    if (fAllowNull && value.type() == json_spirit::null_type)
        return;
    if (value.type() == json_spirit::str_type)
    {
        // reinterpret string as unquoted json value
        json_spirit::Value value2;
        std::string strJSON = value.get_str();
        if (!read_string(strJSON, value2))
            throw std::runtime_error(std::string("Error parsing JSON:")+strJSON);
        ConvertTo<T>(value2, fAllowNull);
        value = value2;
    }
    else
    {
        value = value.get_value<T>();
    }
}
示例#2
0
// Convert from a JSON to an AtNode
static AtSmartPtr<AtNode> ConvertNode(json_spirit::Value node)
{
	AtSmartPtr<AtNode> obj (new AtNode());
	
	if (node.type() == json_spirit::str_type)
	{
		obj->value = std::wstring(node.get_str().begin(),node.get_str().end());
	}
	else if (node.type() == json_spirit::int_type || node.type() == json_spirit::real_type)
	{
		std::wstringstream stream;
		if (node.type() == json_spirit::int_type)
			stream << node.get_int();
		if (node.type() == json_spirit::real_type)
			stream << node.get_real();
		
		obj->value = stream.str().c_str();
		obj->children.insert(AtNode::child_pairtype(
			"@number", AtSmartPtr<AtNode>(new AtNode())
		));
	}
	else if (node.type() == json_spirit::bool_type)
	{
		if (node.get_bool())
			obj->value = L"true";
		else
			obj->value = L"false";
		
		obj->children.insert(AtNode::child_pairtype(
			"@boolean", AtSmartPtr<AtNode>(new AtNode())
		));
	}
	else if (node.type() == json_spirit::array_type)
	{
		obj->children.insert(AtNode::child_pairtype(
			"@array", AtSmartPtr<AtNode>(new AtNode())
		));

		json_spirit::Array nodeChildren = node.get_array();
		json_spirit::Array::iterator itr = nodeChildren.begin();
		
		for (; itr != nodeChildren.end(); itr++)
		{
			obj->children.insert(AtNode::child_pairtype(
				"item", ConvertNode(*itr)
			));
		}
	}
	else if (node.type() == json_spirit::obj_type)
	{
		json_spirit::Object objectProperties = node.get_obj();
		json_spirit::Object::iterator itr = objectProperties.begin();
		for (; itr != objectProperties.end(); itr++)
		{
			obj->children.insert(AtNode::child_pairtype(
				itr->name_, ConvertNode(itr->value_)
			));
		}
	}
	else if (node.type() == json_spirit::null_type)
	{
		return obj;
	}
	else
	{
		assert(! "Unimplemented type found when parsing JSON!");
	}
	
	return obj;
}