diff options
Diffstat (limited to '3party/jansson/myjansson.cpp')
-rw-r--r-- | 3party/jansson/myjansson.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/3party/jansson/myjansson.cpp b/3party/jansson/myjansson.cpp index c91c9ea420..0402cc4fce 100644 --- a/3party/jansson/myjansson.cpp +++ b/3party/jansson/myjansson.cpp @@ -37,4 +37,46 @@ void FromJSONObject(json_t * root, string const & field, double & result) MYTHROW(my::Json::Exception, ("The field", field, "must contain a json number.")); result = json_number_value(val); } + +void FromJSONObject(json_t * root, string const & field, json_int_t & result) +{ + if (!json_is_object(root)) + MYTHROW(my::Json::Exception, ("Bad json object when parsing", field)); + json_t * val = json_object_get(root, field.c_str()); + if (!val) + MYTHROW(my::Json::Exception, ("Obligatory field", field, "is absent.")); + if (!json_is_number(val)) + MYTHROW(my::Json::Exception, ("The field", field, "must contain a json number.")); + result = json_integer_value(val); +} + +void FromJSONObjectOptionalField(json_t * root, string const & field, string & result) +{ + if (!json_is_object(root)) + MYTHROW(my::Json::Exception, ("Bad json object when parsing", field)); + json_t * val = json_object_get(root, field.c_str()); + if (!val) + { + result = string(""); + return; + } + if (!json_is_string(val)) + MYTHROW(my::Json::Exception, ("The field", field, "must contain a json string.")); + result = string(json_string_value(val)); +} + +void FromJSONObjectOptionalField(json_t * root, string const & field, json_int_t & result) +{ + if (!json_is_object(root)) + MYTHROW(my::Json::Exception, ("Bad json object when parsing", field)); + json_t * val = json_object_get(root, field.c_str()); + if (!val) + { + result = 0; + return; + } + if (!json_is_number(val)) + MYTHROW(my::Json::Exception, ("The field", field, "must contain a json number.")); + result = json_integer_value(val); +} } // namespace my |