void json2tvalue(QVariant & output, const rapidjson::Value & input, int itype)
    {
        if(input.IsNull())
        {
            output.clear();
        }
        else if(input.IsInt64())
        {
            output = QVariant(input.GetInt64());
        }
        else if(input.IsInt())
        {
            output = QVariant(input.GetInt());
        }
        else if(input.IsDouble())
        {
            output = QVariant(input.GetDouble());
        }
        else if(input.IsBool())
        {
            output = QVariant(input.GetBool());
        }
        else if(input.IsString())
        {
            output = QVariant(QString(input.GetString()));
        }
        else if(input.IsArray())
        {
            switch(itype)
            {
            case QVariant::Point:
            {
                assert(input.Size() == 2);
                output = QPoint(input[0u].GetDouble(), input[1].GetDouble());
                break;
            }
            case QVariant::PointF:
            {
                assert(input.Size() == 2);
                output = QPointF(input[0u].GetDouble(), input[1].GetDouble());
                break;
            }
            case QVariant::Size:
            {
                assert(input.Size() == 2);
                output = QSize(input[0u].GetDouble(), input[1].GetDouble());
                break;
            }
            case QVariant::SizeF:
            {
                assert(input.Size() == 2);
                output = QSizeF(input[0u].GetDouble(), input[1].GetDouble());
                break;
            }
            case QVariant::Rect:
            {
                assert(input.Size() == 4);
                output = QRect(input[0u].GetDouble(), input[1].GetDouble(), input[2].GetDouble(), input[3].GetDouble());
                break;
            }
            case QVariant::RectF:
            {
                assert(input.Size() == 4);
                output = QRectF(input[0u].GetDouble(), input[1].GetDouble(), input[2].GetDouble(), input[3].GetDouble());
                break;
            }
            case QVariant::Vector2D:
            {
                assert(input.Size() == 2);
                output = QVector2D(input[0u].GetDouble(), input[1].GetDouble());
                break;
            }
            case QVariant::Vector3D:
            {
                assert(input.Size() == 3);
                output = QVector3D(input[0u].GetDouble(), input[1].GetDouble(), input[2].GetDouble());
                break;
            }
            case QVariant::Vector4D:
            {
                assert(input.Size() == 4);
                output = QVector4D(input[0u].GetDouble(), input[1].GetDouble(), input[2].GetDouble(), input[3].GetDouble());
                break;
            }
            case QVariant::Color:
            {
                assert(input.Size() == 4);
                output = QColor(input[0u].GetDouble(), input[1].GetDouble(), input[2].GetDouble(), input[3].GetDouble());
                break;
            }
            case QVariant::StringList:
            {
                QStringList list;
                list.reserve(input.Size());

                for(rapidjson::Value::ConstValueIterator it = input.Begin(); it != input.End(); ++it)
                {
                    QString tmp(it->GetString());
                    list.append(tmp);
                }
                output = list;
                break;
            }
            case QVariant::List:
            default:
            {
                QList<QVariant> list;
                list.reserve(input.Size());

                for(rapidjson::SizeType i = 0; i < input.Size(); ++i)
                {
                    QVariant tmp;
                    json2tvalue(tmp, input[i], QVariant::Invalid);
                    list.append(tmp);
                }
                output = list;
                break;
            }
            }
        }
        else if(input.IsObject())
        {
            QMap<QString, QVariant> map;

            for(rapidjson::Value::ConstMemberIterator it= input.MemberBegin();
                it != input.MemberEnd(); ++it)
            {
                QString qstring(QLatin1String(it->name.GetString(), it->name.GetStringLength()));
                QVariant qvalue;
                json2tvalue(qvalue, it->value, QVariant::Invalid);

                map.insert(qstring, qvalue);
            }

            output = map;
        }
        else
        {
            output.clear();
            assert(false && "shouldn't execute to here.");
        }
    }
Exemple #2
0
 void UNIFAQParameterLibrary::populate(rapidjson::Value &group_data, rapidjson::Value &interaction_data, rapidjson::Value &comp_data)
 {
     // Schema should have been used to validate the data already, so by this point we are can safely consume the data without checking ...
     for (rapidjson::Value::ValueIterator itr = group_data.Begin(); itr != group_data.End(); ++itr)
     {
         Group g;
         g.sgi = (*itr)["sgi"].GetInt();
         g.mgi = (*itr)["mgi"].GetInt();
         g.R_k = (*itr)["R_k"].GetDouble();
         g.Q_k = (*itr)["Q_k"].GetDouble();
         groups.push_back(g);
     }
     for (rapidjson::Value::ValueIterator itr = interaction_data.Begin(); itr != interaction_data.End(); ++itr)
     {
         InteractionParameters ip;
         ip.mgi1 = (*itr)["mgi1"].GetInt();
         ip.mgi2 = (*itr)["mgi2"].GetInt();
         ip.a_ij = (*itr)["a_ij"].GetDouble();
         ip.a_ji = (*itr)["a_ji"].GetDouble();
         ip.b_ij = (*itr)["b_ij"].GetDouble();
         ip.b_ji = (*itr)["b_ji"].GetDouble();
         ip.c_ij = (*itr)["c_ij"].GetDouble();
         ip.c_ji = (*itr)["c_ji"].GetDouble();
         interaction_parameters.push_back(ip);
     }
     for (rapidjson::Value::ValueIterator itr = comp_data.Begin(); itr != comp_data.End(); ++itr)
     {
         Component c;
         c.inchikey = (*itr)["inchikey"].GetString();
         c.registry_number = (*itr)["registry_number"].GetString();
         c.name = (*itr)["name"].GetString();
         // userid is an optional user identifier
         if ((*itr).HasMember("userid")){
             c.userid = (*itr)["userid"].GetString();
         }
         rapidjson::Value &groups = (*itr)["groups"];
         for (rapidjson::Value::ValueIterator itrg = groups.Begin(); itrg != groups.End(); ++itrg)
         {
             int count = (*itrg)["count"].GetInt();
             int sgi = (*itrg)["sgi"].GetInt();
             if  (has_group(sgi)){
                 ComponentGroup cg(count, get_group(sgi));
                 c.groups.push_back(cg);
             }
         }
         components.push_back(c);
     }
 }
bool System::Parse(rapidjson::Value& node) {
    if(node.IsArray()) {
        for(auto sound_itr = node.Begin(); sound_itr != node.End(); sound_itr ++) {
            if(sound_itr->IsObject()) {
                auto& element = (*sound_itr);

                std::shared_ptr<sound_info> sinfo = std::shared_ptr<sound_info>(new sound_info());

                if(element.HasMember("id") && element["id"].IsString()) {
                    sinfo->id = element["id"].GetString();
                }
                if(element.HasMember("src") && element["src"].IsString()) {
                    sinfo->src = element["src"].GetString();
                }
                if(element.HasMember("loop") && element["loop"].IsBool()) {
                    sinfo->loop = element["loop"].GetBool();
                }
                if(element.HasMember("volume") && element["volume"].IsNumber()) {
                    sinfo->volume = element["volume"].GetDouble();
                }
                if(element.HasMember("spatial") && element["spatial"].IsBool()) {
                    sinfo->spatial = element["spatial"].GetBool();
                }
                // store sound info
                sounds[sinfo->id] = sinfo;
            }
        }
    }

    return true;
}
int32_t fetch_event_diff::parse(const rapidjson::Value& _node_event_data)
{
    try
    {
        if (!_node_event_data.IsArray())
            return wpie_error_parse_response;

        for (auto iter = _node_event_data.Begin(), end = _node_event_data.End(); iter != end; ++iter)
        {
            std::string type;
            const auto iter_type = iter->FindMember("type");
            if (iter_type != iter->MemberEnd() && iter_type->value.IsString())
                type = rapidjson_get_string(iter_type->value);

            const auto iter_data = iter->FindMember("data");
            if (iter_data != iter->MemberEnd() && iter_data->value.IsArray())
            {
                auto cl = std::make_shared<contactlist>();
                cl->unserialize_from_diff(iter_data->value);
                diff_->insert(std::make_pair(std::move(type), std::move(cl)));
            }
        }
    }
    catch (const std::exception&)
    {
        return wpie_error_parse_response;
    }

    return 0;
}
Exemple #5
0
void GeoJson::extractPoly(const rapidjson::Value& _in, Polygon& _out, const MapTile& _tile) {
    
    for (auto itr = _in.Begin(); itr != _in.End(); ++itr) {
        _out.emplace_back();
        extractLine(*itr, _out.back(), _tile);
    }
    
}
bool JSASTNode::ParseStatementArray(const rapidjson::Value &value, Vector<JSASTStatement *> &statements)
{

    assert(value.IsArray());

    for (Value::ConstValueIterator itr = value.Begin(); itr != value.End(); itr++)
    {
        JSASTStatement* stmt = ParseStatement(*itr);
        //assert(stmt);
        statements.Push(stmt);

    }

    return true;
}
bool JSASTNode::ParseExpressionArray(const rapidjson::Value &value, Vector<JSASTExpression *> &expressions)
{

    assert(value.IsArray());

    for (Value::ConstValueIterator itr = value.Begin(); itr != value.End(); itr++)
    {
        JSASTExpression* expr = ParseExpression(*itr);
        //assert(expr);
        expressions.Push(expr);

    }

    return true;
}
std::string SettingRegistry::toString(const rapidjson::Value& dflt, std::string setting_name)
{
    if (dflt.IsString())
    {
        return dflt.GetString();
    }
    else if (dflt.IsTrue())
    {
        return "true";
    }
    else if (dflt.IsFalse())
    {
        return "false";
    }
    else if (dflt.IsNumber())
    {
        std::ostringstream ss;
        ss << dflt.GetDouble();
        return ss.str();
    } 
    else if (dflt.IsArray())
    {
        std::stringstream ss;
        ss << "[";
        bool first = true;
        for (auto it = dflt.Begin(); it != dflt.End(); ++it)
        {
            if (!first)
            {
                ss << ",";
            }
            ss << toString(*it);
            first = false;
        }
        ss << "]";
        return ss.str();
    }
    else 
    {
        logError("Unrecognized data type in JSON: %s has type %s\n", setting_name.c_str(), toString(dflt.GetType()).c_str());
        return "";
    }
}
int32_t search_contacts_response::unserialize(const rapidjson::Value& array)
{
    data_.clear();
    for (auto node = array.Begin(), nend = array.End(); node != nend; ++node)
    {
        chunk c;
        { auto i = node->FindMember("sn"); if (i != node->MemberEnd() && i->value.IsString()) c.aimid_ = i->value.GetString(); }
        { auto i = node->FindMember("stamp"); if (i != node->MemberEnd() && i->value.IsString()) c.stamp_ = i->value.GetString(); }
        { auto i = node->FindMember("type"); if (i != node->MemberEnd() && i->value.IsString()) c.type_ = i->value.GetString(); }
        { auto i = node->FindMember("score"); if (i != node->MemberEnd() && i->value.IsInt()) c.score_ = i->value.GetInt(); }
        {
            auto a = node->FindMember("anketa");
            if (a != node->MemberEnd() && a->value.IsObject())
            {
                const auto& sub_node = a->value;
                { auto i = sub_node.FindMember("firstName"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.first_name_ = i->value.GetString(); }
                { auto i = sub_node.FindMember("lastName"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.last_name_ = i->value.GetString(); }
                { auto i = sub_node.FindMember("nickname"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.nick_name_ = i->value.GetString(); }
                { auto i = sub_node.FindMember("city"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.city_ = i->value.GetString(); }
                { auto i = sub_node.FindMember("state"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.state_ = i->value.GetString(); }
                { auto i = sub_node.FindMember("country"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.country_ = i->value.GetString(); }
                { auto i = sub_node.FindMember("gender"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.gender_ = i->value.GetString(); }
                {
                    auto b = sub_node.FindMember("birthDate");
                    if (b != sub_node.MemberEnd() && b->value.IsObject())
                    {
                        const auto& sub_sub_node = b->value;
                        { auto i = sub_sub_node.FindMember("year"); if (i != sub_sub_node.MemberEnd() && i->value.IsInt()) c.birthdate_.year_= i->value.GetInt(); }
                        { auto i = sub_sub_node.FindMember("month"); if (i != sub_sub_node.MemberEnd() && i->value.IsInt()) c.birthdate_.month_= i->value.GetInt(); }
                        { auto i = sub_sub_node.FindMember("day"); if (i != sub_sub_node.MemberEnd() && i->value.IsInt()) c.birthdate_.day_= i->value.GetInt(); }
                    }
                }
                { auto i = sub_node.FindMember("aboutMeCut"); if (i != sub_node.MemberEnd() && i->value.IsString()) c.about_ = i->value.GetString(); }
            }
        }
        data_.push_back(c);
    }
    return 0;
}
Exemple #10
0
double traverse_rapidjson(const rapidjson::Value &v)
{
	double x = 0;
	if (v.IsObject())
	{
		for (auto i = v.MemberBegin(); i != v.MemberEnd(); ++i)
		{
			x += traverse_rapidjson(i->value);
		}
	}
	else if (v.IsArray())
	{
		for (auto i = v.Begin(); i != v.End(); ++i)
		{
			x += traverse_rapidjson(*i);
		}
	}
	else if (v.IsNumber())
	{
		x = v.GetDouble();
	}
	return x;
}
static void parseValue(SettingValue &value, const rapidjson::Value &json_val)
{
    if (json_val.IsInt())
        value = json_val.GetInt();
    else if (json_val.IsString())
        value = std::string(json_val.GetString(), json_val.GetStringLength());
    else if (json_val.IsBool())
        value = json_val.GetBool();
    else if (json_val.IsArray())
    {
        SettingList value_list;

        for (auto it = json_val.Begin(); it != json_val.End(); ++it)
        {
            SettingValue child_val;
            parseValue(child_val, *it);
            value_list.push_back(child_val);
        }

        value = value_list;
    }
    else if (json_val.IsObject())
    {
        SettingMap value_map;

        for (auto it = json_val.MemberBegin(); it != json_val.MemberEnd(); ++it)
        {
            SettingValue child_val;
            parseValue(child_val, it->value);
            value_map[it->name.GetString()] = child_val;
        }

        value = value_map;
    }
    else
        throw std::runtime_error("Unexpected JSON value type.");
}
Exemple #12
0
bool RequestMessage::parsePayload(rapidjson::Value& value, IceServalBridge* bridge)
{
  this->requests.clear();

  if (false == value.IsArray())
  {
    _log->error("Payload could not be parsed: Is not an array");
    return false;
  }

  std::string entity, entityType, scope, rep, relatedEntity;
  int count = 0;

  for (auto it = value.Begin(); it != value.End(); ++it)
  {
    if (false == it->IsArray())
    {
      _log->error("Payload could not be parsed: Id is not an array");
      return false;
    }

    count = 0;

    for (auto it2 = it->Begin(); it2 != it->End(); ++it2)
    {
      if (false == it2->IsString())
      {
        _log->error("Payload could not be parsed: Field is not a string");
        return false;
      }

      switch(count)
      {
        case 0:
          entity = it2->GetString();
          break;
        case 1:
          entityType = it2->GetString();
          break;
        case 2:
          scope = it2->GetString();
          break;
        case 3:
          rep = it2->GetString();
          break;
        case 4:
          relatedEntity = it2->GetString();
          break;
      }

      ++count;
    }

    if (count != 5)
    {
      _log->error("Payload could not be parsed: Wrong number of fields for offer");
      return false;
    }

    auto spec = std::make_shared<InformationSpecification>(entity, entityType, scope, rep, relatedEntity);
    this->requests.push_back(spec);
  }

  return true;
}
void ConvertToMsgPack(const rapidjson::Value& json, msgpack::object& object, msgpack::zone& zone)
{
	switch (json.GetType())
	{
		case rapidjson::kFalseType:
			object = false;
			break;

		case rapidjson::kTrueType:
			object = true;
			break;

		case rapidjson::kNumberType:
		{
			if (json.IsInt())
			{
				object = json.GetInt();
			}
			else if (json.IsUint())
			{
				object = json.GetUint();
			}
			else if (json.IsInt64())
			{
				object = json.GetInt64();
			}
			else if (json.IsUint64())
			{
				object = json.GetUint64();
			}
			else if (json.IsDouble())
			{
				object = json.GetDouble();
			}

			break;
		}

		case rapidjson::kStringType:
			// we allocate with 'zone', otherwise the std::string's raw pointer gets used, which won't work as it gets destructed later on
			object = msgpack::object(std::string(json.GetString(), json.GetStringLength()), zone);
			break;

		case rapidjson::kObjectType:
		{
			std::map<std::string, msgpack::object> list;

			for (auto it = json.MemberBegin(); it != json.MemberEnd(); it++)
			{
				msgpack::object newObject;
				ConvertToMsgPack(it->value, newObject, zone);

				list.insert({ it->name.GetString(), newObject });
			}

			object = msgpack::object(list, zone);

			break;
		}

		case rapidjson::kArrayType:
		{
			std::vector<msgpack::object> list;

			for (auto it = json.Begin(); it != json.End(); it++)
			{
				msgpack::object newObject;
				ConvertToMsgPack(*it, newObject, zone);

				list.push_back(newObject);
			}

			object = msgpack::object(list, zone);

			break;
		}

		default:
			object = msgpack::type::nil();
			break;
	}
}