Example #1
0
	Value convert_json(const Json::Value &val)
	{
		using namespace Json;
		switch (val.type())
		{
		case nullValue:
			return Nil;
		case intValue:
			return value(val.asInt64());
		case uintValue:
			return value((int64_t)val.asUInt64());
		case realValue:
			return value(val.asDouble());
		case stringValue:
			return value(val.asString());
		case booleanValue:
			return bvalue(val.asBool());
		case arrayValue:
			return convert_json_array(val);
		case objectValue:
			return convert_json_complex(val);
		default:
			throw loader_error("Invalid value encountered while parsing json");
		}
	}
Example #2
0
            // Read a natural 
            Natural natural(
                Optizelle::Messaging const & msg,
                Json::Value const & json,
                std::string const & name
            ) {
                // Set the error message
                std::string const err_msg = "Invalid JSON parameter: "
                    + name + " contains an invalid natural.";

                // As long as we have an unsigned integer, grab it
                if(json.isUInt())
                    return Natural(Json::Value::UInt64(json.asUInt64()));
                
                // If we have an integer, grab it if it's positive
                else if(json.isInt()) {
                    Integer val(json.asInt64());
                    if(val>=0)
                        return Natural(val);
                    else
                        msg.error(err_msg);

                // Anything else is an error
                } else
                    msg.error(err_msg);

                // We should not hit this point
                throw;
            }
Example #3
0
 static void add(
     MetadataNode&      parent,
     const std::string& name,
     const Json::Value& node
 )
 {
     if      (node.isNull())   { parent.add(name, ""); }
     else if (node.isBool())   { parent.add(name, node.asBool()); }
     else if (node.isInt())    { parent.add(name, node.asInt64()); }
     else if (node.isUInt())   { parent.add(name, node.asUInt64()); }
     else if (node.isDouble()) { parent.add(name, node.asDouble()); }
     else if (node.isString()) { parent.add(name, node.asString()); }
     else if (node.isObject())
     {
         MetadataNode object = parent.add(name);
         for (const std::string& name: node.getMemberNames())
         {
             add(object, name, node[name]);
         }
     }
     else if (node.isArray())
     {
         for (const Json::Value& item: node)
         {
             add(parent, name, item);
         }
     }
 }
Example #4
0
		AnyValue JsonSerializer::toAny(const Json::Value & val)
		{
			std::vector<AnyValue> arr;
			Bundle b;
			switch (val.type())
			{
			case Json::ValueType::arrayValue:
				for (auto e : val) arr.push_back(this->toAny(e));
				return arr;
			case Json::ValueType::booleanValue:
				return val.asBool();
			case Json::ValueType::intValue:
				return (int64_t)val.asInt64();
			case Json::ValueType::nullValue:
				return nullptr;
			case Json::ValueType::objectValue:
				for (Json::ValueConstIterator it = val.begin(); it != val.end(); it++)
				{
					std::string name = it.name();
					b.set(name, this->toAny(*it));
				}
				return b;
			case Json::ValueType::realValue:
				return val.asDouble();
			case Json::ValueType::stringValue:
				return val.asString();
			case Json::ValueType::uintValue:
				return (uint64_t)val.asUInt64();
			}
			return AnyValue();
		}
/// 从 Json 解析数据到 dictNode 中
static void parseFromJsonToDictionary(const Json::Value & jsonNode, CCDictionary * dictNode)
{
	Json::Value::Members members = jsonNode.getMemberNames();
	for (Json::Value::Members::iterator beg = members.begin(); beg != members.end(); ++beg)
	{
        std::string name = *beg;
		Json::Value child = jsonNode[name];
		
		if (child.isArray())
		{
			CCArray * arr = CCArray::create();
			parseFromJsonToArray(child, arr);
			dictNode->setObject(arr, name);
		}
		else if (child.isObject())
		{
			CCDictionary * dict = CCDictionary::create();
			parseFromJsonToDictionary(child, dict);
			dictNode->setObject(dict, name);
		}
		else if (child.isString())
		{
			CCString * str = CCString::createWithFormat("%s", child.asCString());
			dictNode->setObject(str, name);
		}
		else if (child.isInt())
		{
			CCString * str = CCString::createWithFormat("%d", child.asInt());
			dictNode->setObject(str, name);
		}
		else if (child.isUInt())
		{
			CCString * str = CCString::createWithFormat("%u", child.asUInt());
			dictNode->setObject(str, name);
		}
		else if (child.isInt64())
		{
			CCString * str = CCString::createWithFormat("%lld", child.asInt64());
			dictNode->setObject(str, name);
			
		}
		else if (child.isUInt64())
		{
			CCString * str = CCString::createWithFormat("%llu", child.asUInt64());
			dictNode->setObject(str, name);
		}
		else if (child.isDouble())
		{
			CCString * str = CCString::createWithFormat("%f", child.asDouble());
			dictNode->setObject(str, name);
		}
		else if (child.isBool())
		{
			CCString * str = CCString::createWithFormat("%d", child.asInt());
			dictNode->setObject(str, name);
		}
	}
}
 bool Deserialize ( const Json::Value& json_val, json_lib::int64& obj_val )
 {
     if ( json_val.isIntegral () )
     {
         obj_val = json_val.asInt64 ();
         return true;
     }
     return false;
 }
Example #7
0
bool deserialize(const Json::Value& node, int64_t& val)
{
	if (node.empty())
	{
		std::cout << "Node is empty." << std::endl;
		return false;
	}
	if (!node.isNumeric())
	{
		std::cout << "Node data type is not numeric." << std::endl;
		return false;
	}
	val = node.asInt64();
	return true;
}
Example #8
0
template<> ZEROBUF_INL int64_t fromJSON( const Json::Value& json )
    { return json.asInt64(); }
Example #9
0
	int64_t jsonNodeAs<int64_t>(const Json::Value& node) {
		if (node.isNull()) {
			return 0;
		}
		return node.asInt64();
	}
Example #10
0
    int ToPbSingle(const Json::Value &value, const FieldDescriptor *pFieldDescriptor, Message &message, map<string, string>& key_map)
    {
        if (!check_type(value.type(), pFieldDescriptor->cpp_type()))
        {
            return 1;
        }

        const Reflection *pReflection = message.GetReflection();
        EnumDescriptor      *pEnumDes = NULL;
        EnumValueDescriptor *pEnumValueDes = NULL;
        int ret = 0;
        switch(pFieldDescriptor->cpp_type())
        {
            case FieldDescriptor::CPPTYPE_INT32:
            {
                pReflection->SetInt32(&message, pFieldDescriptor, value.asInt());
                break;
            }
            case FieldDescriptor::CPPTYPE_UINT32:
            {
                pReflection->SetUInt32(&message, pFieldDescriptor, value.asUInt());
                break;
            }
            case FieldDescriptor::CPPTYPE_INT64:
            {
                pReflection->SetInt64(&message, pFieldDescriptor, value.asInt64());
                break;
            }
            case FieldDescriptor::CPPTYPE_UINT64:
            {
                pReflection->SetUInt64(&message, pFieldDescriptor, value.asUInt64());
                break;
            }
            case FieldDescriptor::CPPTYPE_STRING:
            {
                pReflection->SetString(&message, pFieldDescriptor, value.asString());
                break;
            }
            case FieldDescriptor::CPPTYPE_BOOL:
            {
                pReflection->SetBool(&message, pFieldDescriptor, value.asBool());
                break;
            }
            case FieldDescriptor::CPPTYPE_DOUBLE:
            {
                pReflection->SetDouble(&message, pFieldDescriptor, value.asDouble());
                break;
            }
            case FieldDescriptor::CPPTYPE_FLOAT:
            {
                pReflection->SetFloat(&message, pFieldDescriptor, value.asFloat());
                break;
            }
            case FieldDescriptor::CPPTYPE_ENUM:
            {
                if ((pEnumDes = (EnumDescriptor *)pFieldDescriptor->enum_type()) == NULL)
                {
                    return 1;
                }

                if ((pEnumValueDes = (EnumValueDescriptor *)pEnumDes->FindValueByNumber(value.asInt())) == NULL)
                {
                    return 1;
                }

                pReflection->SetEnum(&message, pFieldDescriptor, pEnumValueDes);
                break;
            }
            case FieldDescriptor::CPPTYPE_MESSAGE:
            {
                Message *pmessage = pReflection->MutableMessage(&message, pFieldDescriptor);
                if (key_map.size() == 0)
                {
                    ret = ToPb(*pmessage, value);
                }
                else
                {
                    ret = ToPbMap(*pmessage, value, key_map);
                }

                break;
            }
            default:
            {
                ret = 1;
                break;
            }
        }

        return ret;
    }