void rewriteLocalScheme(rapidjson::Value &value, rapidjson::Document::AllocatorType &allocator) { ASSERT_TRUE(value.IsString()); auto string = std::string { value.GetString(),value.GetStringLength() }; if (string.compare(0, 8, "local://") == 0) { string.replace(0, 8, "http://127.0.0.1:2900/"); value.SetString(string.data(), string.size(), allocator); } }
void jsonValueToRapidJSON(JSONValue* value, rapidjson::Value& rapidValue, rapidjson::Document& document) { JSONNull* nullValue = dynamic_cast<JSONNull*>(value); if (nullValue) { rapidValue.SetNull(); return; } JSONNumber* numberValue = dynamic_cast<JSONNumber*>(value); if (numberValue) { if (numberValue->canBeUInt64()) { rapidValue.SetUint64(numberValue->getUInt64Value()); } else if (numberValue->canBeInt64()) { rapidValue.SetInt64(numberValue->getInt64Value()); } else { rapidValue.SetDouble(numberValue->getDoubleValue()); } 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 (auto & value : values) { rapidjson::Value obj; jsonValueToRapidJSON(value.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 (auto & value : values) { rapidjson::Value obj; jsonValueToRapidJSON(value.second.get(), obj, document); rapidjson::Value key; key.SetString(value.first.c_str(), value.first.size(), document.GetAllocator()); rapidValue.AddMember(key, obj, document.GetAllocator()); } return; } assert(false); }
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); }
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; } }
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; } }
static bool 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.SetInt64(PyLong_AsLong(object)); } else if (PyString_Check(object)) { doc.SetString(PyString_AsString(object), PyString_GET_SIZE(object)); } else if (PyUnicode_Check(object)) { PyObject *utf8_item = PyUnicode_AsUTF8String(object); if (!utf8_item) { PyErr_SetString(PyExc_RuntimeError, "codec error."); return false; } #ifdef PY3 doc.SetString(PyBytes_AsString(utf8_item), PyBytes_GET_SIZE(utf8_item), root.GetAllocator()); #else doc.SetString(PyString_AsString(utf8_item), PyString_GET_SIZE(utf8_item), root.GetAllocator()); #endif Py_XDECREF(utf8_item); } else if (PyTuple_Check(object)) { int len = PyTuple_Size(object), i; doc.SetArray(); rapidjson::Value _v; for (i = 0; i < len; ++i) { PyObject *elm = PyTuple_GetItem(object, i); if (false == pyobj2doc(elm, _v, root)) { return false; } 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); if (false == pyobj2doc(elm, _v, root)) { return false; } 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)) { if (false == pyobj2doc_pair(key, value, doc, root)) { return false; } } } else { PyErr_SetString(PyExc_RuntimeError, "invalid python object"); return false; } return true; }
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 } }
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; } }
void 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("#text", allocator); jv.SetString(xmlnode->first_node()->value(), allocator); jsvalue.AddMember(jn, jv, allocator); add_attributes(xmlnode, jsvalue, allocator); return; } else { // case: <e attr="xxx">...</e> 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> jsvalue.SetString(rapidjson::StringRef(xmlnode->first_node()->value()), 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 = "#text"; name_count[current_name]++; jv.SetString("#text", 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(); } traverse_node(xmlnode_chd, jsvalue_chd, allocator); if (name_count[current_name] > 1 && name_ptr) 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; } }
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; }
void tvalue2json(rapidjson::Value & output, const QVariant & input, rapidjson::Value::AllocatorType & allocator) { switch(input.type()) { case QVariant::Invalid: { output.SetNull(); break; } case QVariant::Bool: { output.SetBool(input.toBool()); break; } case QVariant::Int: { output.SetInt64(input.toInt()); break; } case QVariant::LongLong: { output.SetInt64(input.toLongLong()); break; } case QVariant::Double: { output.SetDouble(input.toDouble()); break; } case QVariant::String: { QByteArray str = input.toString().toUtf8(); output.SetString(str.data(), str.size(), allocator); break; } case QVariant::StringList: { QStringList list = input.toStringList(); output.SetArray(); output.Reserve(list.size(), allocator); rapidjson::Value temp; for(QStringList::const_iterator it = list.begin(); it != list.end(); ++it) { QByteArray str = it->toUtf8(); temp.SetString(str.data(), str.size(), allocator); output.PushBack(temp, allocator); } break; } case QVariant::List: { QList<QVariant> list = input.toList(); output.SetArray(); output.Reserve(list.size(), allocator); rapidjson::Value temp; for(QList<QVariant>::const_iterator it = list.begin(); it != list.end(); ++it) { tvalue2json(temp, *it, allocator); output.PushBack(temp, allocator); } break; } case QVariant::Map: { output.SetObject(); rapidjson::Value tempK, tempV; QMap<QString, QVariant> qmap = input.toMap(); for(QMap<QString, QVariant>::const_iterator it = qmap.begin(); it != qmap.end(); ++it) { tvalue2json(tempK, it.key(), allocator); tvalue2json(tempV, it.value(), allocator); output.AddMember(tempK, tempV, allocator); } break; } case QVariant::Point: { QPoint pt = input.toPoint(); output.SetArray(); output.Reserve(2, allocator); output.PushBack(pt.x(), allocator); output.PushBack(pt.y(), allocator); break; } case QVariant::PointF: { QPointF pt = input.toPointF(); output.SetArray(); output.Reserve(2, allocator); output.PushBack(pt.x(), allocator); output.PushBack(pt.y(), allocator); break; } case QVariant::Size: { QSize pt = input.toSize(); output.SetArray(); output.Reserve(2, allocator); output.PushBack(pt.width(), allocator); output.PushBack(pt.height(), allocator); break; } case QVariant::SizeF: { QSizeF pt = input.toSizeF(); output.SetArray(); output.Reserve(2, allocator); output.PushBack(pt.width(), allocator); output.PushBack(pt.height(), allocator); break; } case QVariant::Rect: { QRect pt = input.toRect(); output.SetArray(); output.Reserve(4, allocator); output.PushBack(pt.x(), allocator); output.PushBack(pt.y(), allocator); output.PushBack(pt.width(), allocator); output.PushBack(pt.height(), allocator); break; } case QVariant::RectF: { QRectF pt = input.toRectF(); output.SetArray(); output.Reserve(4, allocator); output.PushBack(pt.x(), allocator); output.PushBack(pt.y(), allocator); output.PushBack(pt.width(), allocator); output.PushBack(pt.height(), allocator); break; } case QVariant::Vector2D: { QVector2D pt = input.value<QVector2D>(); output.SetArray(); output.Reserve(2, allocator); output.PushBack(pt.x(), allocator); output.PushBack(pt.y(), allocator); break; } case QVariant::Vector3D: { QVector3D pt = input.value<QVector3D>(); output.SetArray(); output.Reserve(3, allocator); output.PushBack(pt.x(), allocator); output.PushBack(pt.y(), allocator); output.PushBack(pt.z(), allocator); break; } case QVariant::Vector4D: { QVector4D pt = input.value<QVector4D>(); output.SetArray(); output.Reserve(4, allocator); output.PushBack(pt.x(), allocator); output.PushBack(pt.y(), allocator); output.PushBack(pt.z(), allocator); output.PushBack(pt.w(), allocator); break; } case QVariant::Color: { QColor pt = input.value<QColor>(); output.SetArray(); output.Reserve(4, allocator); output.PushBack(pt.red(), allocator); output.PushBack(pt.green(), allocator); output.PushBack(pt.blue(), allocator); output.PushBack(pt.alpha(), allocator); break; } default: { output.SetNull(); assert(false && "shuldn't execute to here."); } } }