Esempio n. 1
0
void jsonValueToRapidJSON(JSONValue* value, rapidjson::Value& rapidValue, rapidjson::Document& document) {
	JSONInt* intValue = dynamic_cast<JSONInt*>(value);
	if (intValue) {
		rapidValue.SetInt(intValue->getValue());
		return;
	}
	JSONString* stringValue = dynamic_cast<JSONString*>(value);
	if (stringValue) {
		rapidValue.SetString(stringValue->getValue().c_str(), stringValue->getValue().size(), document.GetAllocator());
		return;
	}
	JSONBool* boolValue = dynamic_cast<JSONBool*>(value);
	if (boolValue) {
		rapidValue.SetBool(boolValue->getValue());
		return;
	}
	JSONArray* arrayValue = dynamic_cast<JSONArray*>(value);
	if (arrayValue) {
		rapidValue.SetArray();
		std::vector<JSONValue::ref> values = arrayValue->getValues();
		for (size_t i = 0; i < values.size(); i++) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(values[i].get(), obj, document);
			rapidValue.PushBack(obj, document.GetAllocator());
		}
		return;
	}
	JSONObject* objectValue = dynamic_cast<JSONObject*>(value);
	if (objectValue) {
		rapidValue.SetObject();
		typedef std::map<std::string, JSONValue::ref> ValuesMap;
		ValuesMap values = objectValue->getValues();
		for (ValuesMap::iterator it = values.begin(); it != values.end(); it++) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(it->second.get(), obj, document);
			rapidjson::Value key;
			key.SetString(it->first.c_str(), it->first.size(), document.GetAllocator());
			rapidValue.AddMember(key, obj, document.GetAllocator());
		}
		return;
	}
	assert(false);
}
Esempio n. 2
0
void ConvertToJSON(const msgpack::object& object, rapidjson::Value& value, rapidjson::MemoryPoolAllocator<>& allocator)
{
	switch (object.type)
	{
		case msgpack::type::BOOLEAN:
			value.SetBool(object.as<bool>());
			break;

		case msgpack::type::POSITIVE_INTEGER:
		case msgpack::type::NEGATIVE_INTEGER:
			value.SetInt(object.as<int>());
			break;

		case msgpack::type::FLOAT:
			value.SetDouble(object.as<double>());
			break;

		case msgpack::type::STR:
		{
			std::string string = object.as<std::string>();
			value.SetString(string.c_str(), string.size(), allocator);
			break;
		}

		case msgpack::type::ARRAY:
		{
			auto list = object.as<std::vector<msgpack::object>>();
			value.SetArray();

			for (auto& entry : list)
			{
				rapidjson::Value inValue;
				ConvertToJSON(entry, inValue, allocator);

				value.PushBack(inValue, allocator);
			}

			break;
		}

		case msgpack::type::MAP:
		{
			auto list = object.as<std::map<std::string, msgpack::object>>();
			value.SetObject();

			for (auto& entry : list)
			{
				rapidjson::Value inValue;
				ConvertToJSON(entry.second, inValue, allocator);

				rapidjson::Value name;
				name.SetString(entry.first.c_str(), entry.first.size(), allocator);

				value.AddMember(name, inValue, allocator);
			}

			break;
		}

		default:
			value.SetNull();
			break;
	}
}
Esempio n. 3
0
static void ToRapidjsonValue(rapidjson::Value& rapidjsonValue, const JSONValue& jsonValue, rapidjson::MemoryPoolAllocator<>& allocator)
{
    switch (jsonValue.GetValueType())
    {
    case JSON_NULL:
        rapidjsonValue.SetNull();
        break;

    case JSON_BOOL:
        rapidjsonValue.SetBool(jsonValue.GetBool());
        break;

    case JSON_NUMBER:
        {
            switch (jsonValue.GetNumberType())
            {
            case JSONNT_INT:
                rapidjsonValue.SetInt(jsonValue.GetInt());
                break;

            case JSONNT_UINT:
                rapidjsonValue.SetUint(jsonValue.GetUInt());
                break;

            default:
                rapidjsonValue.SetDouble(jsonValue.GetDouble());
                break;
            }
        }
        break;

    case JSON_STRING:
        rapidjsonValue.SetString(jsonValue.GetCString(), allocator);
        break;

    case JSON_ARRAY:
        {
            const JSONArray& jsonArray = jsonValue.GetArray();

            rapidjsonValue.SetArray();
            rapidjsonValue.Reserve(jsonArray.Size(), allocator);

            for (unsigned i = 0; i < jsonArray.Size(); ++i)
            {
                rapidjson::Value value;
                rapidjsonValue.PushBack(value, allocator);
                ToRapidjsonValue(rapidjsonValue[i], jsonArray[i], allocator);
            }
        }
        break;

    case JSON_OBJECT:
        {
            const JSONObject& jsonObject = jsonValue.GetObject();

            rapidjsonValue.SetObject();
            for (JSONObject::ConstIterator i = jsonObject.Begin(); i != jsonObject.End(); ++i)
            {
                const char* name = i->first_.CString();
                rapidjson::Value value;
                rapidjsonValue.AddMember(name, value, allocator);
                ToRapidjsonValue(rapidjsonValue[name], i->second_, allocator);
            }
        }
        break;

    default:
        break;
    }
}
Esempio n. 4
0
static void
pyobj2doc(PyObject *object, rapidjson::Value& doc, rapidjson::Document& root)
{
    if (PyBool_Check(object)) {
        if (Py_True == object) {
	        doc.SetBool(true);
        }
        else {
	        doc.SetBool(false);
        }
    }
    else if (Py_None == object) {
        doc.SetNull();
    }
    else if (PyFloat_Check(object)) {
        doc.SetDouble(PyFloat_AsDouble(object));
    }
    else if (PyInt_Check(object)) {
        doc.SetInt(PyLong_AsLong(object));
    }
    else if (PyString_Check(object)) {
        doc.SetString(PyString_AsString(object), PyString_GET_SIZE(object));
    }
    else if (PyUnicode_Check(object)) {
#ifdef PY3
        PyObject *utf8_item;
        utf8_item = PyUnicode_AsUTF8String(object);
        if (!utf8_item) {
            // TODO: error handling
            printf("error\n");
        }

        doc.SetString(PyBytes_AsString(utf8_item),
                      PyBytes_Size(utf8_item), root.GetAllocator());

        Py_XDECREF(utf8_item);
#else
        doc.SetString(PyBytes_AsString(object), PyBytes_GET_SIZE(object));
#endif
    }
    else if (PyTuple_Check(object)) {
        int len = PyTuple_Size(object), i;

        doc.SetArray();
        rapidjson::Value _v;
        for (i = 0; i < len; ++i) {
            PyObject *elm = PyList_GetItem(object, i);
            pyobj2doc(elm, _v, root);
            doc.PushBack(_v, root.GetAllocator());
        }
    }
    else if (PyList_Check(object)) {
        int len = PyList_Size(object), i;

        doc.SetArray();
        rapidjson::Value _v;
        for (i = 0; i < len; ++i) {
            PyObject *elm = PyList_GetItem(object, i);
            pyobj2doc(elm, _v, root);
            doc.PushBack(_v, root.GetAllocator());
        }
    }
    else if (PyDict_Check(object)) {
        doc.SetObject();
        PyObject *key, *value;
        Py_ssize_t pos = 0;
        while (PyDict_Next(object, &pos, &key, &value)) {
            pyobj2doc_pair(key, value, doc, root);
        }
    }
    else {
        // TODO: error handle
    }
}
Esempio n. 5
0
void xml2json_traverse_node(rapidxml::xml_node<> *xmlnode, rapidjson::Value &jsvalue, rapidjson::Document::AllocatorType& allocator)
{
    //cout << "this: " << xmlnode->type() << " name: " << xmlnode->name() << " value: " << xmlnode->value() << endl;
    rapidjson::Value jsvalue_chd;

    jsvalue.SetObject();
    jsvalue_chd.SetObject();
    rapidxml::xml_node<> *xmlnode_chd;

    // classified discussion:
    if((xmlnode->type() == rapidxml::node_data || xmlnode->type() == rapidxml::node_cdata) && xmlnode->value())
    {
        // case: pure_text
        jsvalue.SetString(xmlnode->value(), allocator);  // then addmember("#text" , jsvalue, allocator)
    }
    else if(xmlnode->type() == rapidxml::node_element)
    {
        if(xmlnode->first_attribute())
        {
            if(xmlnode->first_node() && xmlnode->first_node()->type() == rapidxml::node_data && count_children(xmlnode) == 1)
            {
                // case: <e attr="xxx">text</e>
                rapidjson::Value jn, jv;
                jn.SetString(xml2json_text_additional_name, allocator);
                jv.SetString(xmlnode->first_node()->value(), allocator);
                jsvalue.AddMember(jn, jv, allocator);
                xml2json_add_attributes(xmlnode, jsvalue, allocator);
                return;
            }
            else
            {
                // case: <e attr="xxx">...</e>
                xml2json_add_attributes(xmlnode, jsvalue, allocator);
            }
        }
        else
        {
            if(!xmlnode->first_node())
            {
                // case: <e />
                jsvalue.SetNull();
                return;
            }
            else if(xmlnode->first_node()->type() == rapidxml::node_data && count_children(xmlnode) == 1)
            {
                // case: <e>text</e>
                if (xml2json_numeric_support == false)
                {
                    jsvalue.SetString(rapidjson::StringRef(xmlnode->first_node()->value()), allocator);
                }
                else
                {
                    bool hasDecimal;
                    if (xml2json_has_digits_only(xmlnode->first_node()->value(), &hasDecimal) == false)
                    {
                        jsvalue.SetString(rapidjson::StringRef(xmlnode->first_node()->value()), allocator);
                    }
                    else
                    {
                        char *bgnptr = xmlnode->first_node()->value();
                        char *endptr = bgnptr;
                        if (hasDecimal)
                        {
                            double value = std::strtod(bgnptr, &endptr);
                            if (bgnptr != endptr)
                              jsvalue.SetDouble(value);
                            else
                              jsvalue.SetString(rapidjson::StringRef(bgnptr), allocator);
                        }
                        else
                        {
                            long int value = std::strtol(bgnptr, &endptr, 0);
                            if (bgnptr != endptr)
                              jsvalue.SetInt(value);
                            else
                              jsvalue.SetString(rapidjson::StringRef(bgnptr), allocator);
                        }
                    }
                }
                return;
            }
        }
        if(xmlnode->first_node())
        {
            // case: complex else...
            std::map<std::string, int> name_count;
            for(xmlnode_chd = xmlnode->first_node(); xmlnode_chd; xmlnode_chd = xmlnode_chd->next_sibling())
            {
                std::string current_name;
                const char *name_ptr = NULL;
                rapidjson::Value jn, jv;
                if(xmlnode_chd->type() == rapidxml::node_data || xmlnode_chd->type() == rapidxml::node_cdata)
                {
                    current_name = xml2json_text_additional_name;
                    name_count[current_name]++;
                    jv.SetString(xml2json_text_additional_name, allocator);
                    name_ptr = jv.GetString();
                }
                else if(xmlnode_chd->type() == rapidxml::node_element)
                {
                    current_name = xmlnode_chd->name();
                    name_count[current_name]++;
                    name_ptr = xmlnode_chd->name();
                }
                xml2json_traverse_node(xmlnode_chd, jsvalue_chd, allocator);
                if(name_count[current_name] > 1 && name_ptr)
                    xml2json_to_array_form(name_ptr, jsvalue, jsvalue_chd, allocator);
                else
                {
                    jn.SetString(name_ptr, allocator);
                    jsvalue.AddMember(jn, jsvalue_chd, allocator);
                }
            }
        }
    }
    else
    {
        std::cerr << "err data!!" << std::endl;
    }
}
Esempio n. 6
0
 bool clone_value(rapidjson::Value & output, const rapidjson::Value & input, rapidjson::Value::AllocatorType & allocator)
 {
     if(&output == &input)
     {
         return false;
     }
     
     if(input.IsNull())
     {
         output.SetNull();
     }
     else if(input.IsInt64())
     {
         output.SetInt(input.GetInt64());
     }
     else if(input.IsInt())
     {
         output.SetInt(input.GetInt());
     }
     else if(input.IsDouble())
     {
         output.SetDouble(input.GetDouble());
     }
     else if(input.IsBool())
     {
         output.SetBool(input.GetBool());
     }
     else if(input.IsString())
     {
         output.SetString(input.GetString(), input.GetStringLength(), allocator);
     }
     else if(input.IsArray())
     {
         rapidjson::Value temp;
         output.SetArray();
         output.Reserve(input.Size(), allocator);
         for(rapidjson::SizeType i = 0; i < input.Size(); ++i)
         {
             clone_value(temp, input[i], allocator);
             output.PushBack(temp, allocator);
         }
     }
     else if(input.IsObject())
     {
         output.SetObject();
         
         rapidjson::Value key, val;
         for(rapidjson::Value::ConstMemberIterator it = input.MemberBegin();
             it != input.MemberEnd(); ++it)
         {
             clone_value(key, it->name, allocator);
             clone_value(val, it->value, allocator);
             
             output.AddMember(key, val, allocator);
         }
     }
     else
     {
         assert(false && "shuldn't execute to here.");
         return false;
     }
     return true;
 }