diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/.gitignore | 1 | ||||
| -rw-r--r-- | src/Makefile.am | 9 | ||||
| -rw-r--r-- | src/json.cc | 50 | ||||
| -rw-r--r-- | src/json.hh | 12 |
4 files changed, 69 insertions, 3 deletions
diff --git a/src/.gitignore b/src/.gitignore index 146a3bd..13d1e3c 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -4,6 +4,7 @@ /stamp-h1 /libcgi.la /libdb.la +/libjson.la /libutil.la /libsender_client.la /event diff --git a/src/Makefile.am b/src/Makefile.am index c85aca3..1f8b819 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,18 +5,21 @@ AM_CPPFLAGS = @DEFINES@ -DLOCALSTATEDIR='"@localstatedir@/stuff"' \ bin_PROGRAMS = event sender noinst_PROGRAMS = test-sender -noinst_LTLIBRARIES = libdb.la libcgi.la libutil.la libsender_client.la +noinst_LTLIBRARIES = libdb.la libcgi.la libutil.la libsender_client.la \ + libjson.la event_SOURCES = event.cc event.hh event_main.cc common.hh cgi.hh db.hh event_LDADD = libdb.la libcgi.la libsender_client.la -sender_SOURCES = common.hh sender.cc json.hh json.cc +sender_SOURCES = common.hh sender.cc sender_CPPFLAGS = $(AM_CPPFLAGS) @CURL_CFLAGS@ -sender_LDADD = libutil.la @CURL_LIBS@ +sender_LDADD = libjson.la libutil.la @CURL_LIBS@ test_sender_SOURCES = common.hh test_sender.cc sender_client.hh test_sender_LDADD = libsender_client.la +libjson_la_SOURCES = json.hh json.cc common.hh + libcgi_la_SOURCES = cgi.hh common.hh cgi.cc \ query_parser.hh query_parser.cc \ header_parser.hh header_parser.cc \ 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>(); diff --git a/src/json.hh b/src/json.hh index e14982e..5d5aa38 100644 --- a/src/json.hh +++ b/src/json.hh @@ -12,6 +12,7 @@ class JsonObject { public: virtual ~JsonObject() {} virtual void put(const std::string& name, const std::string& value) = 0; + void put(const std::string& name, const char* value); virtual void put(const std::string& name, double value) = 0; virtual void put(const std::string& name, int64_t value) = 0; virtual void put(const std::string& name, bool value) = 0; @@ -21,11 +22,16 @@ public: virtual void put(const std::string& name, std::shared_ptr<JsonArray> arr) = 0; + virtual bool erase(const std::string& name) = 0; + virtual void clear() = 0; + virtual bool contains(const std::string& name) const = 0; virtual bool is_null(const std::string& name) const = 0; virtual const std::string& get(const std::string& name, const std::string& fallback = std::string()) const = 0; + virtual const char* get(const std::string& name, + const char* fallback) const = 0; virtual double get(const std::string& name, double fallback = 0.0) const = 0; virtual int64_t get(const std::string& name, @@ -54,6 +60,7 @@ public: virtual void resize(size_t size) = 0; virtual void put(size_t index, const std::string& value) = 0; + void put(size_t index, const char* value); virtual void put(size_t index, double value) = 0; virtual void put(size_t index, int64_t value) = 0; virtual void put(size_t index, bool value) = 0; @@ -63,7 +70,11 @@ public: virtual void put(size_t index, std::shared_ptr<JsonArray> arr) = 0; + virtual void erase(size_t index) = 0; + void clear(); + void add(const std::string& value); + void add(const char* value); void add(double value); void add(int64_t value); void add(bool value); @@ -75,6 +86,7 @@ public: virtual const std::string& get(size_t index, const std::string& fallback = std::string()) const = 0; + virtual const char* get(size_t index, const char* fallback) const = 0; virtual double get(size_t index, double fallback = 0.0) const = 0; virtual int64_t get(size_t index, |
