diff --git a/Release/include/cpprest/json.h b/Release/include/cpprest/json.h index 5f2572ca01..f4987f5c21 100644 --- a/Release/include/cpprest/json.h +++ b/Release/include/cpprest/json.h @@ -544,6 +544,55 @@ namespace json /// True if the field exists, false otherwise. bool has_field(const utility::string_t &key) const; + /// + /// Tests for the presence of a number field + /// + /// The name of the field + /// True if the field exists, false otherwise. + _ASYNCRTIMP bool has_number_field(const utility::string_t &key) const; + + /// + /// Tests for the presence of an integer field + /// + /// The name of the field + /// True if the field exists, false otherwise. + _ASYNCRTIMP bool has_integer_field(const utility::string_t &key) const; + + /// + /// Tests for the presence of a double field + /// + /// The name of the field + /// True if the field exists, false otherwise. + _ASYNCRTIMP bool has_double_field(const utility::string_t &key) const; + + /// + /// Tests for the presence of a boolean field + /// + /// The name of the field + /// True if the field exists, false otherwise. + _ASYNCRTIMP bool has_boolean_field(const utility::string_t &key) const; + + /// + /// Tests for the presence of a string field + /// + /// The name of the field + /// True if the field exists, false otherwise. + _ASYNCRTIMP bool has_string_field(const utility::string_t &key) const; + + /// + /// Tests for the presence of an array field + /// + /// The name of the field + /// True if the field exists, false otherwise. + _ASYNCRTIMP bool has_array_field(const utility::string_t &key) const; + + /// + /// Tests for the presence of an object field + /// + /// The name of the field + /// True if the field exists, false otherwise. + _ASYNCRTIMP bool has_object_field(const utility::string_t &key) const; + /// /// Accesses a field of a JSON object. /// diff --git a/Release/src/json/json.cpp b/Release/src/json/json.cpp index c61740d46d..e5a6089aea 100644 --- a/Release/src/json/json.cpp +++ b/Release/src/json/json.cpp @@ -382,6 +382,41 @@ bool web::json::details::_Object::has_field(const utility::string_t &key) const return m_object.find(key) != m_object.end(); } +bool web::json::value::has_number_field(const utility::string_t &key) const +{ + return has_field(key) && at(key).is_number(); +} + +bool web::json::value::has_integer_field(const utility::string_t &key) const +{ + return has_field(key) && at(key).is_integer(); +} + +bool web::json::value::has_double_field(const utility::string_t &key) const +{ + return has_field(key) && at(key).is_double(); +} + +bool web::json::value::has_boolean_field(const utility::string_t &key) const +{ + return has_field(key) && at(key).is_boolean(); +} + +bool web::json::value::has_string_field(const utility::string_t &key) const +{ + return has_field(key) && at(key).is_string(); +} + +bool web::json::value::has_array_field(const utility::string_t &key) const +{ + return has_field(key) && at(key).is_array(); +} + +bool web::json::value::has_object_field(const utility::string_t &key) const +{ + return has_field(key) && at(key).is_object(); +} + utility::string_t json::value::to_string() const { #ifndef _WIN32 diff --git a/Release/tests/functional/json/to_as_and_operators_tests.cpp b/Release/tests/functional/json/to_as_and_operators_tests.cpp index c1bbb732e0..eb1dc4f71b 100644 --- a/Release/tests/functional/json/to_as_and_operators_tests.cpp +++ b/Release/tests/functional/json/to_as_and_operators_tests.cpp @@ -312,11 +312,64 @@ TEST(negative_get_element_array) TEST(has_field_object) { + + json::value v1; v1[U("a")] = json::value::number(1); + v1[U("b")] = json::value::boolean(true); + v1[U("c")] = json::value::string(U("a string")); + v1[U("d")] = json::value::array({}); + json::value sub_field; + sub_field[U("x")] = json::value::number(1); + v1[U("e")] = sub_field; + VERIFY_IS_TRUE(v1.has_field(U("a"))); - VERIFY_IS_FALSE(v1.has_field(U("b"))); + VERIFY_IS_TRUE(v1.has_field(U("b"))); + VERIFY_IS_TRUE(v1.has_field(U("c"))); + VERIFY_IS_TRUE(v1.has_field(U("d"))); + VERIFY_IS_TRUE(v1.has_field(U("e"))); + VERIFY_IS_FALSE(v1.has_field(U("f"))); + + VERIFY_IS_TRUE(v1.has_number_field(U("a"))); + VERIFY_IS_TRUE(v1.has_integer_field(U("a"))); + VERIFY_IS_FALSE(v1.has_double_field(U("a"))); + VERIFY_IS_FALSE(v1.has_boolean_field(U("a"))); + VERIFY_IS_FALSE(v1.has_string_field(U("a"))); + VERIFY_IS_FALSE(v1.has_array_field(U("a"))); + VERIFY_IS_FALSE(v1.has_object_field(U("a"))); + + VERIFY_IS_TRUE(v1.has_boolean_field(U("b"))); + VERIFY_IS_FALSE(v1.has_number_field(U("b"))); + VERIFY_IS_FALSE(v1.has_integer_field(U("b"))); + VERIFY_IS_FALSE(v1.has_double_field(U("b"))); + VERIFY_IS_FALSE(v1.has_string_field(U("b"))); + VERIFY_IS_FALSE(v1.has_array_field(U("b"))); + VERIFY_IS_FALSE(v1.has_object_field(U("b"))); + + VERIFY_IS_TRUE(v1.has_string_field(U("c"))); + VERIFY_IS_FALSE(v1.has_boolean_field(U("c"))); + VERIFY_IS_FALSE(v1.has_number_field(U("c"))); + VERIFY_IS_FALSE(v1.has_integer_field(U("c"))); + VERIFY_IS_FALSE(v1.has_double_field(U("c"))); + VERIFY_IS_FALSE(v1.has_array_field(U("c"))); + VERIFY_IS_FALSE(v1.has_object_field(U("c"))); + + VERIFY_IS_TRUE(v1.has_array_field(U("d"))); + VERIFY_IS_FALSE(v1.has_string_field(U("d"))); + VERIFY_IS_FALSE(v1.has_boolean_field(U("d"))); + VERIFY_IS_FALSE(v1.has_number_field(U("d"))); + VERIFY_IS_FALSE(v1.has_integer_field(U("d"))); + VERIFY_IS_FALSE(v1.has_double_field(U("d"))); + VERIFY_IS_FALSE(v1.has_object_field(U("d"))); + + VERIFY_IS_TRUE(v1.has_object_field(U("e"))); + VERIFY_IS_FALSE(v1.has_array_field(U("e"))); + VERIFY_IS_FALSE(v1.has_string_field(U("e"))); + VERIFY_IS_FALSE(v1.has_boolean_field(U("e"))); + VERIFY_IS_FALSE(v1.has_number_field(U("e"))); + VERIFY_IS_FALSE(v1.has_integer_field(U("e"))); + VERIFY_IS_FALSE(v1.has_double_field(U("e"))); json::value v2;