inline error_code unmarshall_json(const rapidjson::Value &doc, rpc_address& val) { TEST_PARAM(doc.IsUint64()); dsn_address_t addr; addr.u.value = doc.GetUint64(); val = rpc_address(addr); TEST_PARAM(!val.is_invalid()) return ERR_OK; }
bool getJsonNumber(const rapidjson::Value &v, T &dst) { if (v.IsDouble()) dst = T(v.GetDouble()); else if (v.IsInt()) dst = T(v.GetInt()); else if (v.IsUint()) dst = T(v.GetUint()); else if (v.IsInt64()) dst = T(v.GetInt64()); else if (v.IsUint64()) dst = T(v.GetUint64()); else return false; return true; }
std::uint64_t get_value(const rapidjson::Value& root, const char* key) { if (key != nullptr) { if (root.HasMember(key) && root[key].IsUint64()) { return root[key].GetUint64(); } else throw std::system_error(std::error_code(), "Parse error."); } else { if (root.IsUint64()) return root.GetUint64(); else throw std::system_error(std::error_code(), "Parse error."); } }
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; } }