Skip to content

Commit 5aed642

Browse files
author
N3xoX1
committed
Json: Add removeMember and isMember
1 parent c5a5215 commit 5aed642

2 files changed

Lines changed: 51 additions & 18 deletions

File tree

‎src/common/json.cpp‎

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ JsonValue::JsonValue(const std::string & value)
123123
m_value.String = DuplicateAndPrefixStringValue(value.data(), static_cast<unsigned>(value.length()));
124124
}
125125

126-
JsonValue::JsonValue(const char * Begin, const char * end)
126+
JsonValue::JsonValue(const char * begin, const char * end)
127127
{
128128
InitBasic(JsonValueType::String, true);
129-
m_value.String = DuplicateAndPrefixStringValue(Begin, static_cast<unsigned>(end - Begin));
129+
m_value.String = DuplicateAndPrefixStringValue(begin, static_cast<unsigned>(end - begin));
130130
}
131131

132132
JsonValue::JsonValue(const JsonValue & other)
@@ -414,14 +414,14 @@ JsonValue & JsonValue::operator[](uint32_t Index)
414414
*this = JsonValue(JsonValueType::Array);
415415
}
416416

417-
CZString Key(Index);
418-
ObjectValues::iterator it = m_value.Map->lower_bound(Key);
419-
if (it != m_value.Map->end() && (*it).first == Key)
417+
CZString key(Index);
418+
ObjectValues::iterator it = m_value.Map->lower_bound(key);
419+
if (it != m_value.Map->end() && (*it).first == key)
420420
{
421421
return (*it).second;
422422
}
423423

424-
ObjectValues::value_type defaultValue(Key, NullSingleton());
424+
ObjectValues::value_type defaultValue(key, NullSingleton());
425425
it = m_value.Map->insert(it, defaultValue);
426426
return (*it).second;
427427
}
@@ -461,19 +461,19 @@ const JsonValue & JsonValue::operator[](uint32_t index) const
461461
return (*it).second;
462462
}
463463

464-
const JsonValue & JsonValue::operator[](const char * Key) const
464+
const JsonValue & JsonValue::operator[](const char * key) const
465465
{
466-
JsonValue const * found = Find(Key, Key + strlen(Key));
466+
JsonValue const * found = Find(key, key + strlen(key));
467467
if (!found)
468468
{
469469
return NullSingleton();
470470
}
471471
return *found;
472472
}
473473

474-
JsonValue const & JsonValue::operator[](const std::string & Key) const
474+
JsonValue const & JsonValue::operator[](const std::string & key) const
475475
{
476-
JsonValue const * found = Find(Key.data(), Key.data() + Key.length());
476+
JsonValue const * found = Find(key.data(), key.data() + key.length());
477477
if (!found)
478478
{
479479
return NullSingleton();
@@ -495,18 +495,46 @@ JsonValue & JsonValue::Append(JsonValue && value)
495495
return m_value.Map->emplace(size(), std::move(value)).first->second;
496496
}
497497

498-
JsonValue const * JsonValue::Find(char const * Begin) const
498+
void JsonValue::removeMember(const char * key)
499499
{
500-
return Find(Begin, Begin + strlen(Begin));
500+
assert(Type() == JsonValueType::Null || Type() == JsonValueType::Object);
501+
if (Type() == JsonValueType::Null)
502+
{
503+
return;
504+
}
505+
506+
CZString actualKey(key, unsigned(strlen(key)), CZString::NoDuplication);
507+
m_value.Map->erase(actualKey);
508+
}
509+
510+
bool JsonValue::isMember(char const * key) const
511+
{
512+
return isMember(key, key + strlen(key));
513+
}
514+
515+
bool JsonValue::isMember(const std::string & key) const
516+
{
517+
return isMember(key.data(), key.data() + key.length());
518+
}
519+
520+
bool JsonValue::isMember(char const * begin, char const * end) const
521+
{
522+
JsonValue const * value = Find(begin, end);
523+
return nullptr != value;
524+
}
525+
526+
JsonValue const * JsonValue::Find(char const * begin) const
527+
{
528+
return Find(begin, begin + strlen(begin));
501529
}
502530

503-
JsonValue const * JsonValue::Find(char const * Begin, char const * end) const
531+
JsonValue const * JsonValue::Find(char const * begin, char const * end) const
504532
{
505533
if (Type() == JsonValueType::Null)
506534
{
507535
return nullptr;
508536
}
509-
CZString actualKey(Begin, static_cast<unsigned>(end - Begin), CZString::NoDuplication);
537+
CZString actualKey(begin, static_cast<unsigned>(end - begin), CZString::NoDuplication);
510538
ObjectValues::const_iterator it = m_value.Map->find(actualKey);
511539
if (it == m_value.Map->end())
512540
{
@@ -578,13 +606,13 @@ void JsonValue::DupMeta(const JsonValue & other)
578606
m_limit = other.m_limit;
579607
}
580608

581-
JsonValue & JsonValue::ResolveReference(const char * Key)
609+
JsonValue & JsonValue::ResolveReference(const char * key)
582610
{
583611
if (Type() == JsonValueType::Null)
584612
{
585613
*this = JsonValue(JsonValueType::Object);
586614
}
587-
CZString actualKey(Key, static_cast<unsigned>(strlen(Key)), CZString::NoDuplication);
615+
CZString actualKey(key, static_cast<unsigned>(strlen(key)), CZString::NoDuplication);
588616
auto it = m_value.Map->lower_bound(actualKey);
589617
if (it != m_value.Map->end() && (*it).first == actualKey)
590618
{
@@ -598,13 +626,13 @@ JsonValue & JsonValue::ResolveReference(const char * Key)
598626
}
599627

600628
// @param key is not null-terminated.
601-
JsonValue & JsonValue::ResolveReference(char const * Key, char const * end)
629+
JsonValue & JsonValue::ResolveReference(char const * key, char const * end)
602630
{
603631
if (Type() == JsonValueType::Null)
604632
{
605633
*this = JsonValue(JsonValueType::Object);
606634
}
607-
CZString actualKey(Key, static_cast<unsigned>(end - Key), CZString::DuplicateOnCopy);
635+
CZString actualKey(key, static_cast<unsigned>(end - key), CZString::DuplicateOnCopy);
608636
auto it = m_value.Map->lower_bound(actualKey);
609637
if (it != m_value.Map->end() && (*it).first == actualKey)
610638
{

‎src/common/json.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ class JsonValue
161161

162162
JsonValue & Append(const JsonValue & value);
163163
JsonValue & Append(JsonValue && value);
164+
void removeMember(const char * key);
165+
166+
bool isMember(const char * key) const;
167+
bool isMember(const std::string & key) const;
168+
bool isMember(const char * begin, const char * end) const;
164169

165170
JsonValue const * Find(char const * begin) const;
166171
JsonValue const * Find(char const * begin, char const * end) const;

0 commit comments

Comments
 (0)