diff options
author | Yuri Gorshenin <y@maps.me> | 2017-04-24 13:06:20 +0300 |
---|---|---|
committer | Yuri Gorshenin <y@maps.me> | 2017-04-24 13:34:06 +0300 |
commit | cdc6c255832eca9bc90ec7a6e07f53c0d8118ad0 (patch) | |
tree | abebe69836253dce8af3360d11fefe871988cfe4 /3party | |
parent | 4db5e689769eeda252c57e320e24d58f1010123c (diff) |
[assessment-tool] Allow null value for position.
Diffstat (limited to '3party')
-rw-r--r-- | 3party/jansson/myjansson.cpp | 4 | ||||
-rw-r--r-- | 3party/jansson/myjansson.hpp | 25 |
2 files changed, 28 insertions, 1 deletions
diff --git a/3party/jansson/myjansson.cpp b/3party/jansson/myjansson.cpp index 2987c51f17..5adab297c9 100644 --- a/3party/jansson/myjansson.cpp +++ b/3party/jansson/myjansson.cpp @@ -50,6 +50,8 @@ json_t * GetJSONOptionalField(json_t * root, std::string const & field) MYTHROW(my::Json::Exception, ("Bad json object while parsing", field)); return json_object_get(root, field.c_str()); } + +bool JSONIsNull(json_t * root) { return json_is_null(root); } } // namespace my void FromJSONObject(json_t * root, string const & field, double & result) @@ -157,7 +159,7 @@ void ToJSONObject(json_t & root, string const & field, string const & value) void FromJSONObjectOptionalField(json_t * root, string const & field, string & result) { auto * val = my::GetJSONOptionalField(root, field); - if (!val) + if (!val || my::JSONIsNull(val)) { result.clear(); return; diff --git a/3party/jansson/myjansson.hpp b/3party/jansson/myjansson.hpp index 7b68c4211e..e5657d120d 100644 --- a/3party/jansson/myjansson.hpp +++ b/3party/jansson/myjansson.hpp @@ -45,6 +45,7 @@ public: json_t * GetJSONObligatoryField(json_t * root, std::string const & field); json_t * GetJSONOptionalField(json_t * root, std::string const & field); +bool JSONIsNull(json_t * root); } // namespace my inline void FromJSON(json_t * root, json_t *& value) { value = root; } @@ -82,6 +83,30 @@ void FromJSONObject(json_t * root, std::string const & field, std::vector<T> & r FromJSON(json_array_get(arr, i), result[i]); } +// The function tries to parse array of values from a value +// corresponding to |field| in a json object corresponding to |root|. +// Returns true when the value is non-null and array is successfully +// parsed. Returns false when there are no such |field| in a |root| +// or value is null. Also, the method may throw exception in case of +// json parsing errors. +template <typename T> +bool FromJSONObjectOptional(json_t * root, std::string const & field, std::vector<T> & result) +{ + auto * arr = my::GetJSONOptionalField(root, field); + if (!arr || my::JSONIsNull(arr)) + { + result.clear(); + return false; + } + if (!json_is_array(arr)) + MYTHROW(my::Json::Exception, ("The field", field, "must contain a json array.")); + size_t const sz = json_array_size(arr); + result.resize(sz); + for (size_t i = 0; i < sz; ++i) + FromJSON(json_array_get(arr, i), result[i]); + return true; +} + template <typename T> void ToJSONObject(json_t & root, std::string const & field, std::vector<T> const & values) { |