Skip to content

Commit cf1c01e

Browse files
authored
Codifying the behavior of json.contains with nested properties, in the unit test. (Azure#4763)
1 parent 2f3e5de commit cf1c01e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

sdk/core/azure-core/test/ut/json_test.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,31 @@ TEST(Json, create)
1616

1717
EXPECT_EQ(expected, j.dump());
1818
}
19+
20+
TEST(Json, duplicateName)
21+
{
22+
json jsonRoot = json::parse(R"({"KeyName": 1, "AnotherObject": {"KeyName": 2}})");
23+
int value = 0;
24+
if (jsonRoot.contains("KeyName"))
25+
{
26+
value = jsonRoot["KeyName"].get<int>();
27+
}
28+
EXPECT_EQ(value, 1);
29+
30+
jsonRoot = json::parse(R"({"AnotherObject": {"KeyName": 2}})");
31+
value = 0;
32+
33+
// The nested KeyName property is considered not found, when at the root.
34+
if (jsonRoot.contains("KeyName"))
35+
{
36+
value = jsonRoot["KeyName"].get<int>();
37+
}
38+
EXPECT_EQ(value, 0);
39+
40+
// The nested KeyName property is considered found, when navigating to the nested object first.
41+
if (jsonRoot["AnotherObject"].contains("KeyName"))
42+
{
43+
value = jsonRoot["AnotherObject"]["KeyName"].get<int>();
44+
}
45+
EXPECT_EQ(value, 2);
46+
}

0 commit comments

Comments
 (0)