diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2015-06-09 21:13:30 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2015-06-09 21:13:30 +0200 |
| commit | 977c5975e4ead8e27becef8b78740fe5da631195 (patch) | |
| tree | 28049d37ab4dd26cb6588ea0145a26e5f1a4b53d /src/json.cc | |
| parent | 8d087b24cd40f609d030cfd0f2bebe8bb976086a (diff) | |
Add unittest for JSON and fix put/get overload for const char*
Diffstat (limited to 'src/json.cc')
| -rw-r--r-- | src/json.cc | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/json.cc b/src/json.cc index 6e11c8f..d37cba0 100644 --- a/src/json.cc +++ b/src/json.cc @@ -108,6 +108,14 @@ public: } } + bool erase(const std::string& name) override { + return data_.erase(name); + } + + void clear() override { + data_.clear(); + } + bool contains(const std::string& name) const override { return data_.find(name) != data_.end(); } @@ -122,6 +130,14 @@ public: return it->second->type == JsonType::STRING ? static_cast<StringJsonValue*>(it->second.get())->str : fallback; } + const char* get(const std::string& name, + const char* fallback) const override { + auto it = data_.find(name); + if (it == data_.end() || !it->second) return fallback; + return it->second->type == JsonType::STRING ? + static_cast<StringJsonValue*>(it->second.get())->str.c_str() : + fallback; + } double get(const std::string& name, double fallback) const override { auto it = data_.find(name); if (it == data_.end() || !it->second) return fallback; @@ -209,6 +225,10 @@ public: } } + void erase(size_t index) { + data_.erase(data_.begin() + index); + } + bool is_null(size_t index) const override { return index < data_.size() && !data_[index]; } @@ -218,6 +238,12 @@ public: return data_[index]->type == JsonType::STRING ? static_cast<StringJsonValue*>(data_[index].get())->str : fallback; } + const char* get(size_t index, const char* fallback) const override { + if (index >= data_.size() || !data_[index]) return fallback; + return data_[index]->type == JsonType::STRING ? + static_cast<StringJsonValue*>(data_[index].get())->str.c_str() : + fallback; + } double get(size_t index, double fallback) const override { if (index >= data_.size() || !data_[index]) return fallback; return data_[index]->type == JsonType::DOUBLE ? @@ -352,10 +378,30 @@ std::shared_ptr<JsonObject> JsonObject::create() { return std::make_shared<JsonObjectImpl>(); } +void JsonObject::put(const std::string& name, const char* value) { + if (value) { + put(name, std::string(value)); + } else { + put(name, nullptr); + } +} + +void JsonArray::put(size_t index, const char* value) { + if (value) { + put(index, std::string(value)); + } else { + put(index, nullptr); + } +} + void JsonArray::add(const std::string& value) { put(size(), value); } +void JsonArray::add(const char* value) { + put(size(), value); +} + void JsonArray::add(double value) { put(size(), value); } @@ -380,6 +426,10 @@ void JsonArray::add(std::shared_ptr<JsonArray> arr) { put(size(), arr); } +void JsonArray::clear() { + resize(0); +} + // static std::shared_ptr<JsonArray> JsonArray::create() { return std::make_shared<JsonArrayImpl>(); |
