Skip to content

Commit

Permalink
return verbose error when json contains object with duplicated keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Przemyslaw Ciezkowski committed Nov 10, 2021
1 parent 431d397 commit de512d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/vcpkg-test/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,14 @@ TEST_CASE ("JSON track newlines", "[json]")
^
)");
}

TEST_CASE ("JSON duplicated object keys", "[json]")
{
auto res = Json::parse("{\"name\": 1, \"name\": 2}", "filename");
REQUIRE(!res);
REQUIRE(res.error()->format() ==
R"(filename:1:13: error: Duplicated key "name" in an object
on expression: {"name": 1, "name": 2}
^
)");
}
6 changes: 6 additions & 0 deletions src/vcpkg/base/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,13 @@ namespace vcpkg::Json
add_error("Unexpected character; expected comma or close brace");
}

auto keyPairLoc = cur_loc();
auto val = parse_kv_pair();
if (obj.contains(val.first))
{
add_error(Strings::format("Duplicated key \"%s\" in an object", val.first), keyPairLoc);
return Value();
}
obj.insert(std::move(val.first), std::move(val.second));
}
}
Expand Down

0 comments on commit de512d6

Please sign in to comment.