Skip to content

Commit 0cffa77

Browse files
committed
Adding has_<type>_field utility to json value
Adding unit tests for has_<type>_field utilities
1 parent bcb746b commit 0cffa77

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed

Release/include/cpprest/json.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,55 @@ namespace json
544544
/// <returns>True if the field exists, false otherwise.</returns>
545545
bool has_field(const utility::string_t &key) const;
546546

547+
/// <summary>
548+
/// Tests for the presence of a number field
549+
/// </summary>
550+
/// <param name="key">The name of the field</param>
551+
/// <returns>True if the field exists, false otherwise.</returns>
552+
_ASYNCRTIMP bool has_number_field(const utility::string_t &key) const;
553+
554+
/// <summary>
555+
/// Tests for the presence of an integer field
556+
/// </summary>
557+
/// <param name="key">The name of the field</param>
558+
/// <returns>True if the field exists, false otherwise.</returns>
559+
_ASYNCRTIMP bool has_integer_field(const utility::string_t &key) const;
560+
561+
/// <summary>
562+
/// Tests for the presence of a double field
563+
/// </summary>
564+
/// <param name="key">The name of the field</param>
565+
/// <returns>True if the field exists, false otherwise.</returns>
566+
_ASYNCRTIMP bool has_double_field(const utility::string_t &key) const;
567+
568+
/// <summary>
569+
/// Tests for the presence of a boolean field
570+
/// </summary>
571+
/// <param name="key">The name of the field</param>
572+
/// <returns>True if the field exists, false otherwise.</returns>
573+
_ASYNCRTIMP bool has_boolean_field(const utility::string_t &key) const;
574+
575+
/// <summary>
576+
/// Tests for the presence of a string field
577+
/// </summary>
578+
/// <param name="key">The name of the field</param>
579+
/// <returns>True if the field exists, false otherwise.</returns>
580+
_ASYNCRTIMP bool has_string_field(const utility::string_t &key) const;
581+
582+
/// <summary>
583+
/// Tests for the presence of an array field
584+
/// </summary>
585+
/// <param name="key">The name of the field</param>
586+
/// <returns>True if the field exists, false otherwise.</returns>
587+
_ASYNCRTIMP bool has_array_field(const utility::string_t &key) const;
588+
589+
/// <summary>
590+
/// Tests for the presence of an object field
591+
/// </summary>
592+
/// <param name="key">The name of the field</param>
593+
/// <returns>True if the field exists, false otherwise.</returns>
594+
_ASYNCRTIMP bool has_object_field(const utility::string_t &key) const;
595+
547596
/// <summary>
548597
/// Accesses a field of a JSON object.
549598
/// </summary>

Release/src/json/json.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,41 @@ bool web::json::details::_Object::has_field(const utility::string_t &key) const
382382
return m_object.find(key) != m_object.end();
383383
}
384384

385+
bool web::json::value::has_number_field(const utility::string_t &key) const
386+
{
387+
return has_field(key) && at(key).is_number();
388+
}
389+
390+
bool web::json::value::has_integer_field(const utility::string_t &key) const
391+
{
392+
return has_field(key) && at(key).is_integer();
393+
}
394+
395+
bool web::json::value::has_double_field(const utility::string_t &key) const
396+
{
397+
return has_field(key) && at(key).is_double();
398+
}
399+
400+
bool web::json::value::has_boolean_field(const utility::string_t &key) const
401+
{
402+
return has_field(key) && at(key).is_boolean();
403+
}
404+
405+
bool web::json::value::has_string_field(const utility::string_t &key) const
406+
{
407+
return has_field(key) && at(key).is_string();
408+
}
409+
410+
bool web::json::value::has_array_field(const utility::string_t &key) const
411+
{
412+
return has_field(key) && at(key).is_array();
413+
}
414+
415+
bool web::json::value::has_object_field(const utility::string_t &key) const
416+
{
417+
return has_field(key) && at(key).is_object();
418+
}
419+
385420
utility::string_t json::value::to_string() const
386421
{
387422
#ifndef _WIN32

Release/tests/functional/json/to_as_and_operators_tests.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,66 @@ TEST(negative_get_element_array)
312312

313313
TEST(has_field_object)
314314
{
315+
316+
315317
json::value v1;
316318

317319
v1[U("a")] = json::value::number(1);
320+
v1[U("b")] = json::value::boolean(true);
321+
v1[U("c")] = json::value::string(U("a string"));
322+
v1[U("d")] = json::value::array({});
323+
324+
json::value sub_field;
325+
sub_field[U("x")] = json::value::number(1);
326+
327+
v1[U("e")] = sub_field;
328+
318329
VERIFY_IS_TRUE(v1.has_field(U("a")));
319-
VERIFY_IS_FALSE(v1.has_field(U("b")));
330+
VERIFY_IS_TRUE(v1.has_field(U("b")));
331+
VERIFY_IS_TRUE(v1.has_field(U("c")));
332+
VERIFY_IS_TRUE(v1.has_field(U("d")));
333+
VERIFY_IS_TRUE(v1.has_field(U("e")));
334+
VERIFY_IS_FALSE(v1.has_field(U("f")));
335+
336+
VERIFY_IS_TRUE(v1.has_number_field(U("a")));
337+
VERIFY_IS_TRUE(v1.has_integer_field(U("a")));
338+
VERIFY_IS_FALSE(v1.has_double_field(U("a")));
339+
VERIFY_IS_FALSE(v1.has_boolean_field(U("a")));
340+
VERIFY_IS_FALSE(v1.has_string_field(U("a")));
341+
VERIFY_IS_FALSE(v1.has_array_field(U("a")));
342+
VERIFY_IS_FALSE(v1.has_object_field(U("a")));
343+
344+
VERIFY_IS_TRUE(v1.has_boolean_field(U("b")));
345+
VERIFY_IS_FALSE(v1.has_number_field(U("b")));
346+
VERIFY_IS_FALSE(v1.has_integer_field(U("b")));
347+
VERIFY_IS_FALSE(v1.has_double_field(U("b")));
348+
VERIFY_IS_FALSE(v1.has_string_field(U("b")));
349+
VERIFY_IS_FALSE(v1.has_array_field(U("b")));
350+
VERIFY_IS_FALSE(v1.has_object_field(U("b")));
351+
352+
VERIFY_IS_TRUE(v1.has_string_field(U("c")));
353+
VERIFY_IS_FALSE(v1.has_boolean_field(U("c")));
354+
VERIFY_IS_FALSE(v1.has_number_field(U("c")));
355+
VERIFY_IS_FALSE(v1.has_integer_field(U("c")));
356+
VERIFY_IS_FALSE(v1.has_double_field(U("c")));
357+
VERIFY_IS_FALSE(v1.has_array_field(U("c")));
358+
VERIFY_IS_FALSE(v1.has_object_field(U("c")));
359+
360+
VERIFY_IS_TRUE(v1.has_array_field(U("d")));
361+
VERIFY_IS_FALSE(v1.has_string_field(U("d")));
362+
VERIFY_IS_FALSE(v1.has_boolean_field(U("d")));
363+
VERIFY_IS_FALSE(v1.has_number_field(U("d")));
364+
VERIFY_IS_FALSE(v1.has_integer_field(U("d")));
365+
VERIFY_IS_FALSE(v1.has_double_field(U("d")));
366+
VERIFY_IS_FALSE(v1.has_object_field(U("d")));
367+
368+
VERIFY_IS_TRUE(v1.has_object_field(U("e")));
369+
VERIFY_IS_FALSE(v1.has_array_field(U("e")));
370+
VERIFY_IS_FALSE(v1.has_string_field(U("e")));
371+
VERIFY_IS_FALSE(v1.has_boolean_field(U("e")));
372+
VERIFY_IS_FALSE(v1.has_number_field(U("e")));
373+
VERIFY_IS_FALSE(v1.has_integer_field(U("e")));
374+
VERIFY_IS_FALSE(v1.has_double_field(U("e")));
320375

321376
json::value v2;
322377

0 commit comments

Comments
 (0)