[Support] Add erase() to json::Object

This commit is contained in:
Sam McCall 2019-11-11 18:25:01 +01:00
parent b6cd799e29
commit 4162875c3b
2 changed files with 37 additions and 5 deletions

View File

@ -122,6 +122,8 @@ public:
std::pair<iterator, bool> try_emplace(ObjectKey &&K, Ts &&... Args) {
return M.try_emplace(std::move(K), std::forward<Ts>(Args)...);
}
bool erase(StringRef K);
void erase(iterator I) { M.erase(I); }
iterator find(StringRef K) { return M.find_as(K); }
const_iterator find(StringRef K) const { return M.find_as(K); }
@ -555,6 +557,9 @@ inline Object::Object(std::initializer_list<KV> Properties) {
inline std::pair<Object::iterator, bool> Object::insert(KV E) {
return try_emplace(std::move(E.K), std::move(E.V));
}
inline bool Object::erase(StringRef K) {
return M.erase(ObjectKey(K));
}
// Standard deserializers are provided for primitive types.
// See comments on Value.

View File

@ -71,7 +71,7 @@ TEST(JSONTest, CanonicalOutput) {
}
TEST(JSONTest, Escaping) {
std::string test = {
std::string Test = {
0, // Strings may contain nulls.
'\b', '\f', // Have mnemonics, but we escape numerically.
'\r', '\n', '\t', // Escaped with mnemonics.
@ -80,17 +80,17 @@ TEST(JSONTest, Escaping) {
'\xce', '\x94', // Non-ASCII UTF-8 is not escaped.
};
std::string teststring = R"("\u0000\u0008\u000c\r\n\tS\"\\)"
std::string TestString = R"("\u0000\u0008\u000c\r\n\tS\"\\)"
"\x7f\xCE\x94\"";
EXPECT_EQ(teststring, s(test));
EXPECT_EQ(TestString, s(Test));
EXPECT_EQ(R"({"object keys are\nescaped":true})",
s(Object{{"object keys are\nescaped", true}}));
}
TEST(JSONTest, PrettyPrinting) {
const char str[] = R"({
const char Str[] = R"({
"empty_array": [],
"empty_object": {},
"full_array": [
@ -106,7 +106,7 @@ TEST(JSONTest, PrettyPrinting) {
}
})";
EXPECT_EQ(str, sp(Object{
EXPECT_EQ(Str, sp(Object{
{"empty_object", Object{}},
{"empty_array", {}},
{"full_array", {1, nullptr}},
@ -120,6 +120,33 @@ TEST(JSONTest, PrettyPrinting) {
}));
}
TEST(JSONTest, Array) {
Array A{1, 2};
A.emplace_back(3);
A.emplace(++A.begin(), 0);
A.push_back(4);
A.insert(++++A.begin(), 99);
EXPECT_EQ(A.size(), 6u);
EXPECT_EQ(R"([1,0,99,2,3,4])", s(std::move(A)));
}
TEST(JSONTest, Object) {
Object O{{"a", 1}, {"b", 2}, {"c", 3}};
EXPECT_TRUE(O.try_emplace("d", 4).second);
EXPECT_FALSE(O.try_emplace("a", 4).second);
auto D = O.find("d");
EXPECT_FALSE(D == O.end());
auto E = O.find("e");
EXPECT_TRUE(E == O.end());
O.erase("b");
O.erase(D);
EXPECT_EQ(O.size(), 2u);
EXPECT_EQ(R"({"a":1,"c":3})", s(std::move(O)));
}
TEST(JSONTest, Parse) {
auto Compare = [](llvm::StringRef S, Value Expected) {
if (auto E = parse(S)) {