diff options
author | Vladimir Byko-Ianko <v.bykoianko@corp.mail.ru> | 2016-03-10 17:45:36 +0300 |
---|---|---|
committer | Sergey Yershov <yershov@corp.mail.ru> | 2016-03-23 16:53:00 +0300 |
commit | 89c87a15c79af9cfd16979da7fa3bacf8e593dbe (patch) | |
tree | aa49562b2f421287d25ae9404ad6590514d37a10 /3party/jansson | |
parent | e80c37d2416cff876c2e50a6663cf294cdaacfe8 (diff) |
[new downloader] Review fixes.
Diffstat (limited to '3party/jansson')
-rw-r--r-- | 3party/jansson/myjansson.cpp | 9 | ||||
-rw-r--r-- | 3party/jansson/myjansson.hpp | 18 |
2 files changed, 25 insertions, 2 deletions
diff --git a/3party/jansson/myjansson.cpp b/3party/jansson/myjansson.cpp index 84ed12e0dd..f1de73f435 100644 --- a/3party/jansson/myjansson.cpp +++ b/3party/jansson/myjansson.cpp @@ -2,7 +2,12 @@ namespace my { -void FromJSON(json_t * root, string & result) { result = string(json_string_value(root)); } +void FromJSON(json_t * root, string & result) +{ + if (!json_is_string(root)) + MYTHROW(my::Json::Exception, ("The field must contain a json string.")); + result = string(json_string_value(root)); +} void FromJSONObject(json_t * root, string const & field, string & result) { @@ -54,7 +59,7 @@ void FromJSONObjectOptionalField(json_t * root, string const & field, string & r json_t * val = json_object_get(root, field.c_str()); if (!val) { - result = string(""); + result.clear(); return; } if (!json_is_string(val)) diff --git a/3party/jansson/myjansson.hpp b/3party/jansson/myjansson.hpp index 7b6b587368..b3e93169a1 100644 --- a/3party/jansson/myjansson.hpp +++ b/3party/jansson/myjansson.hpp @@ -30,6 +30,7 @@ public: }; void FromJSON(json_t * root, string & result); +void FromJSON(json_t * root, json_t *& value) { value = root; } void FromJSONObject(json_t * root, string const & field, string & result); void FromJSONObject(json_t * root, string const & field, strings::UniString & result); void FromJSONObject(json_t * root, string const & field, double & result); @@ -51,4 +52,21 @@ void FromJSONObject(json_t * root, string const & field, vector<T> & result) void FromJSONObjectOptionalField(json_t * root, string const & field, string & result); void FromJSONObjectOptionalField(json_t * root, string const & field, json_int_t & result); + +template <typename T> +void FromJSONObjectOptionalField(json_t * root, string const & field, vector<T> & result) +{ + json_t * arr = json_object_get(root, field.c_str()); + if (!arr) + { + result.clear(); + return; + } + if (!json_is_array(arr)) + MYTHROW(my::Json::Exception, ("The field", field, "must contain a json array.")); + size_t sz = json_array_size(arr); + result.resize(sz); + for (size_t i = 0; i < sz; ++i) + FromJSON(json_array_get(arr, i), result[i]); +} } // namespace my |