void Stream::jsonReader(string name, const Value &v, vector<ProtoMessage> &messages) { // json to protobuf vector<string> name_spl = split(name, '_'); if(name_spl.size()>0) { name = name_spl[0]; } Protos::ClassName *cl = new Protos::ClassName; cl->set_class_name(name); messages.push_back(cl); bool has_object = false; for (Value::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr) { if (itr->value.IsObject()) { jsonReader(itr->name.GetString(), itr->value, messages); has_object = true; } } if (!has_object) { if (!Factory::inst().isProtoType(name)) { // trying to deduce proto name if (!Factory::inst().isProtoType(name + string("C"))) { throw dnnException()<< "Erros while reading " << name << ": unkwnown proto type\n"; } name += string("C"); Protos::ClassName *ccl = new Protos::ClassName; ccl->set_class_name(name); messages.push_back(ccl); } ProtoMessage m = Factory::inst().createProto(name); Json::JsonToProtobuf(v, m); messages.push_back(m); } }
/* ============================================================================ * PROTECTED Methods */ void THGoogleDetectLanguage::parseResponse (const QByteArray& data) { THJsonStreamReader::TokenType tokenType; THJsonStreamReader jsonReader(data); while (!jsonReader.atEnd()) { tokenType = jsonReader.readNext(); if (!jsonReader.isProperty()) continue; QStringRef propertyName = jsonReader.name(); if (propertyName == "language") { d->language = jsonReader.value().toString(); } else if (propertyName == "isReliable") { d->isReliable = jsonReader.value().toBool(); } else if (propertyName == "confidence") { d->confidence = jsonReader.value().toDouble(); } else if (propertyName == "responseStatus") { setResponseStatus(jsonReader.value().toInt()); } else if (propertyName == "responseDetails" && tokenType != THJsonStreamReader::PropertyNull) { setErrorString(jsonReader.value().toString()); } } emit finished(responseStatus() != 200); }
/* ============================================================================ * PROTECTED Methods */ void THGoogleTranslator::parseResponse (const QByteArray& data) { THJsonStreamReader::TokenType tokenType; THJsonStreamReader jsonReader(data); while (!jsonReader.atEnd()) { tokenType = jsonReader.readNext(); if (!jsonReader.isProperty()) continue; QStringRef propertyName = jsonReader.name(); if (propertyName == "translatedText") { d->translatedText = jsonReader.value().toString(); } else if (propertyName == "detectedSourceLanguage") { d->detectedSourceLanguage = jsonReader.value().toString(); } else if (propertyName == "responseStatus") { setResponseStatus(jsonReader.value().toInt()); } else if (propertyName == "responseDetails" && tokenType != THJsonStreamReader::PropertyNull) { setErrorString(jsonReader.value().toString()); } } emit finished(responseStatus() != 200); }
void ModuleManifest::Parse(std::istream& is) { Json::Value root; Json::Reader jsonReader(Json::Features::strictMode()); if (!jsonReader.parse(is, root, false)) { throw std::runtime_error(jsonReader.getFormattedErrorMessages()); } if (!root.isObject()) { throw std::runtime_error("The Json root element must be an object."); } ParseJsonObject(root, m_Properties); }
vector<ProtoMessage> Stream::readObjectProtos() { vector<ProtoMessage> messages; if (r == Binary) { protoReader(messages); } if (r == Text) { if (!iterator->value.IsObject()) { throw dnnException()<< "Fail to read " << Json::stringify(iterator->value) << ". Expected object\n"; } if (iterator == document.MemberEnd()) { return messages; } jsonReader(iterator->name.GetString(), iterator->value, messages); iterator++; } return messages; }