void SuperastCPP::addElemsToArray(rapidjson::Value& parentValue, rapidjson::Value& elemsValue) { assert(parentValue.IsArray()); assert(elemsValue.IsArray()); for (unsigned i = 0; i < elemsValue.Size(); ++i) { parentValue.PushBack(elemsValue[i], 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); }
template<class InputIterator> inline void tws::core::copy_numeric_array(InputIterator first, InputIterator last, rapidjson::Value& jvalues, rapidjson::Document::AllocatorType& allocator) { if(!jvalues.IsArray()) throw conversion_error() << tws::error_description("copy_numeric_array: can only copy arrays of numeric data."); while(first != last) { jvalues.PushBack(*first, allocator); ++first; } }
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 tws::geoarray::write(const std::vector<dimension_t>& dims, rapidjson::Value& jdims, rapidjson::Document::AllocatorType& allocator) { if(!jdims.IsArray()) throw tws::conversion_error() << tws::error_description("dimensions metadata must be a JSON array!"); const std::size_t ndims = dims.size(); for(std::size_t i = 0; i != ndims; ++i) { const dimension_t& dim = dims[i]; rapidjson::Value jdim(rapidjson::kObjectType); write(dim, jdim, allocator); jdims.PushBack(jdim, allocator); } }
void Json_Parser::write_a_array(cocos2d::ValueVector &temp,rapidjson::Value &val2,rapidjson::Document::AllocatorType&allocator){ rapidjson::Value array2(rapidjson::kArrayType); for (int i=0; i<temp.size(); i++) { rapidjson::Value v; if(temp.at(i).getType()==Value::Type::DOUBLE){ v.SetDouble(temp.at(i).asDouble()); val2.PushBack(v,allocator); } else if(temp.at(i).getType()==Value::Type::STRING){ v.SetString(temp.at(i).asString().c_str(),allocator); val2.PushBack(v,allocator); } else if(temp.at(i).getType()==Value::Type::BOOLEAN){ v.SetBool(temp.at(i).asBool()); val2.PushBack(v,allocator); } else if(temp.at(i).getType()==Value::Type::INTEGER){ v.SetInt(temp.at(i).asInt()); val2.PushBack(v,allocator); } else if(temp.at(i).getType()==Value::Type::NONE){ v.SetNull(); val2.PushBack(v,allocator); } else if(temp.at(i).getType()==Value::Type::MAP){ ValueMap chmap=temp.at(i).asValueMap(); v.SetObject(); write_a_map(chmap,v,allocator); val2.PushBack(v,allocator); } else if(temp.at(i).getType()==Value::Type::VECTOR){ ValueVector chvec=temp.at(i).asValueVector(); v.SetArray(); write_a_array(chvec, v, allocator); val2.PushBack(v, allocator); } } }
static void encodeJsonArray(const Array_t &a,rapidjson::Value &d,rapidjson::Document::AllocatorType &allocator){ for(auto &value:a){ switch(value.type()){ case rapidjson::kStringType:{ d.PushBack(rapidjson::Value(value.string().c_str(),allocator).Move(), allocator); }break; case rapidjson::kNumberType:{ const auto doubleValue=value.dbl(); if(double_is_int(doubleValue)){ d.PushBack(rapidjson::Value(int(value.dbl())).Move(), allocator); }else{ d.PushBack(rapidjson::Value(value.dbl()).Move(), allocator); } }break; case rapidjson::kObjectType:{ rapidjson::Value d2; d2.SetObject(); encodeJsonObject(value.object(), d2,allocator); d.PushBack(d2.Move(), allocator); }break; case rapidjson::kArrayType:{ rapidjson::Value d2; d2.SetArray(); encodeJsonArray(value.array(), d2, allocator); d.PushBack(d2.Move(), allocator); }break; case rapidjson::kTrueType: case rapidjson::kFalseType:{ rapidjson::Value d2; d2.SetBool(value.boolean()); d.PushBack(d2.Move(), allocator); }break; case rapidjson::kNullType:{ rapidjson::Value d2; d2.SetNull(); d.PushBack(d2.Move(), allocator); }break; } } }
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; }
void tws::wtss::compute_time_series(const timeseries_request_parameters& parameters, const timeseries_validated_parameters& vparameters, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& jattributes) { const std::size_t ntime_pts = vparameters.end_time_idx - vparameters.start_time_idx + 1; const std::size_t nattributes = parameters.queried_attributes.size(); // get a connection from the pool in order to retrieve the time series data std::unique_ptr<tws::scidb::connection> conn(tws::scidb::connection_pool::instance().get()); // iterate through each queried attribute for(std::size_t i = 0; i != nattributes; ++i) { const auto& attr_name = parameters.queried_attributes[i]; const std::size_t& attr_pos = vparameters.attribute_positions[i]; // the scidb query string std::string str_afl = "project( between(" + parameters.cv_name + ", " + std::to_string(vparameters.pixel_col) + "," + std::to_string(vparameters.pixel_row) + "," + std::to_string(vparameters.start_time_idx) + "," + std::to_string(vparameters.pixel_col) + "," + std::to_string(vparameters.pixel_row) + "," + std::to_string(vparameters.end_time_idx) + "), " + attr_name + ")"; boost::shared_ptr< ::scidb::QueryResult > qresult = conn->execute(str_afl, true); if((qresult == nullptr) || (qresult->array == nullptr)) { rapidjson::Value jattribute(rapidjson::kObjectType); jattribute.AddMember("attribute", attr_name.c_str(), allocator); rapidjson::Value jvalues(rapidjson::kArrayType); jattribute.AddMember("values", jvalues, allocator); jattributes.PushBack(jattribute, allocator); if(qresult != nullptr) conn->completed(qresult->queryID); continue; // no query result returned after querying database. } try { std::vector<double> values(ntime_pts, vparameters.geo_array->attributes[attr_pos].missing_value); const ::scidb::ArrayDesc& array_desc = qresult->array->getArrayDesc(); const ::scidb::Attributes& array_attributes = array_desc.getAttributes(true); const ::scidb::AttributeDesc& attr = array_attributes.front(); std::shared_ptr< ::scidb::ConstArrayIterator > array_it = qresult->array->getConstIterator(attr.getId()); fill_time_series(values, ntime_pts, array_it.get(), attr.getType(), 2, -(vparameters.start_time_idx)); rapidjson::Value jattribute(rapidjson::kObjectType); jattribute.AddMember("attribute", attr_name.c_str(), allocator); rapidjson::Value jvalues(rapidjson::kArrayType); tws::core::copy_numeric_array(values.begin(), values.end(), jvalues, allocator); jattribute.AddMember("values", jvalues, allocator); jattributes.PushBack(jattribute, allocator); } catch(...) { conn->completed(qresult->queryID); throw; } conn->completed(qresult->queryID); } }
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 } }
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."); } } }