Skip to content
Snippets Groups Projects
Commit 26c2e16a authored by Adrien Béraud's avatar Adrien Béraud
Browse files

value: optimize json parsing

parent e8b385c2
Branches
Tags
No related merge requests found
...@@ -390,7 +390,7 @@ struct OPENDHT_PUBLIC Value ...@@ -390,7 +390,7 @@ struct OPENDHT_PUBLIC Value
* Build a value from a json object * Build a value from a json object
* @param json * @param json
*/ */
Value(Json::Value& json); Value(const Json::Value& json);
#endif #endif
template <typename Type> template <typename Type>
......
...@@ -131,7 +131,7 @@ base64_encode(const std::vector<unsigned char>& str) ...@@ -131,7 +131,7 @@ base64_encode(const std::vector<unsigned char>& str)
return base64_encode(str.cbegin(), str.cend()); return base64_encode(str.cbegin(), str.cend());
} }
std::string std::vector<unsigned char>
base64_decode(const std::string& str) base64_decode(const std::string& str)
{ {
size_t output_length = str.length() / 4 * 3 + 2; size_t output_length = str.length() / 4 * 3 + 2;
...@@ -139,5 +139,5 @@ base64_decode(const std::string& str) ...@@ -139,5 +139,5 @@ base64_decode(const std::string& str)
output.resize(output_length); output.resize(output_length);
base64_decode(str.data(), str.size(), output.data(), &output_length); base64_decode(str.data(), str.size(), output.data(), &output_length);
output.resize(output_length); output.resize(output_length);
return std::string(output.begin(), output.end()); return output;
} }
...@@ -33,4 +33,4 @@ std::string base64_encode(const std::vector<unsigned char>& str); ...@@ -33,4 +33,4 @@ std::string base64_encode(const std::vector<unsigned char>& str);
* @param str the input buffer * @param str the input buffer
* @return a base64-decoded buffer * @return a base64-decoded buffer
*/ */
std::string base64_decode(const std::string& str); std::vector<unsigned char> base64_decode(const std::string& str);
...@@ -165,39 +165,36 @@ Value::msgpack_unpack_body(const msgpack::object& o) ...@@ -165,39 +165,36 @@ Value::msgpack_unpack_body(const msgpack::object& o)
} }
#ifdef OPENDHT_JSONCPP #ifdef OPENDHT_JSONCPP
Value::Value(Json::Value& json) Value::Value(const Json::Value& json)
{ {
id = Value::Id(unpackId(json, "id")); id = Value::Id(unpackId(json, "id"));
if (json.isMember("cypher")) { const auto& jcypher = json["cypher"];
auto cypherStr = json["cypher"].asString(); if (jcypher.isString())
cypherStr = base64_decode(cypherStr); cypher = base64_decode(jcypher.asString());
cypher = std::vector<unsigned char>(cypherStr.begin(), cypherStr.end()); const auto& jsig = json["sig"];
} if (jsig.isString())
if (json.isMember("sig")) { signature = base64_decode(jsig.asString());
auto sigStr = json["sig"].asString(); const auto& jseq = json["seq"];
sigStr = base64_decode(sigStr); if (!jseq.isNull())
signature = std::vector<unsigned char>(sigStr.begin(), sigStr.end()); seq = jseq.asInt();
} const auto& jowner = json["owner"];
if (json.isMember("seq")) if (jowner.isString()) {
seq = json["seq"].asInt(); auto ownerStr = jowner.asString();
if (json.isMember("owner")) {
auto ownerStr = json["owner"].asString();
auto ownerBlob = std::vector<unsigned char>(ownerStr.begin(), ownerStr.end()); auto ownerBlob = std::vector<unsigned char>(ownerStr.begin(), ownerStr.end());
owner = std::make_shared<const crypto::PublicKey>(ownerBlob); owner = std::make_shared<const crypto::PublicKey>(ownerBlob);
} }
if (json.isMember("to")) { const auto& jto = json["to"];
auto toStr = json["to"].asString(); if (jto.isString())
recipient = InfoHash(toStr); recipient = InfoHash(jto.asString());
} const auto& jtype = json["type"];
if (json.isMember("type")) if (!jtype.isNull())
type = json["type"].asInt(); type = jtype.asInt();
if (json.isMember("data")){ const auto& jdata = json["data"];
auto dataStr = json["data"].asString(); if (jdata.isString())
dataStr = base64_decode(dataStr); data = base64_decode(jdata.asString());
data = std::vector<unsigned char>(dataStr.begin(), dataStr.end()); const auto& jutype = json["utype"];
} if (jutype.isString())
if (json.isMember("utype")) user_type = jutype.asString();
user_type = json["utype"].asString();
} }
Json::Value Json::Value
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment