| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- //
- // json_binder.h
- // json
- //
- // Created by Sam Jaffe on 1/31/16.
- // Copyright © 2016 Sam Jaffe. All rights reserved.
- //
- #ifndef json_binder_h
- #define json_binder_h
- #pragma once
- namespace json {
- namespace binder {
- template <typename T>
- class binder_impl {
- public:
- virtual binder_impl<T>* clone() const = 0;
- virtual ~binder_impl() {}
- virtual void parse(T&, char const*&) const = 0;
- };
-
- template <typename T>
- class binder {
- public:
- binder() : impl(nullptr), owned(false) {}
- binder(binder const& other) : impl(other.impl->clone()), owned(true) {}
- binder(binder_impl<T> const* p) : impl(p), owned(true) {}
- binder(binder_impl<T> const& r) : impl(&r), owned(false) {}
- ~binder() { if (impl && owned) delete impl; }
-
- void parse(T& object, char const*& data) const {
- if (!impl) return;
- impl->parse(object, data);
- }
- private:
- binder_impl<T> const* impl;
- bool owned;
- };
-
- template <typename T, typename E>
- class direct_binder : public binder_impl<T> {
- public:
- direct_binder(E T::*p, binder<E> const& i) : ptr(p), impl(i) {}
- virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
-
- virtual void parse(T& val, char const*& data) const override {
- impl.parse(val.*ptr, data);
- }
- private:
- E T::*ptr;
- binder<E> impl;
- };
-
- template <typename T>
- class direct_binder<T, bool> : public binder_impl<T> {
- public:
- direct_binder(bool T::*p) : ptr(p) {}
- virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
-
- virtual void parse(T& val, char const*& data) override {
- if (!strncmp(data, "true", 4)) {
- val.*ptr = true;
- } else if (!strncmp(data, "false", 5)) {
- val.*ptr = false;
- } else {
- throw json::malformed_json_exception("Expected a boolean type here");
- }
- }
-
- private:
- bool T::*ptr;
- };
-
- template <typename T>
- class direct_binder<T, int> : public binder_impl<T> {
- public:
- direct_binder(int T::*p) : ptr(p) {}
- virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
- virtual void parse(T& val, char const*& data) const override {
- // if (false) {
- json::helper::parse_numeric(val.*ptr, data);
- // } else {
- // throw json::malformed_json_exception("Expected an integral type here");
- // }
- }
- private:
- int T::*ptr;
- };
-
- template <typename T>
- class direct_binder<T, double> : public binder_impl<T> {
- public:
- direct_binder(double T::*p) : ptr(p) {}
- virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
- virtual void parse(T& val, char const*& data) const override {
- // if (false) {
- json::helper::parse_numeric(val.*ptr, data);
- // } else {
- // throw json::malformed_json_exception("Expected a floating point type here");
- // }
- }
- private:
- double T::*ptr;
- };
-
- template <typename T>
- class direct_binder<T, std::string> : public binder_impl<T> {
- public:
- direct_binder(std::string T::*p) : ptr(p) {}
- virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
- virtual void parse(T& val, char const*& data) const override {
- json::helper::parse_string(val.*ptr, data);
- }
- private:
- std::string T::*ptr;
- };
-
- template <typename T, typename V>
- class direct_binder<T, std::vector<V> > : public binder_impl<T> {
- public:
- direct_binder(std::vector<V> T::*p, binder<V> const&i);
- virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
-
- virtual void parse(T& val, char const*& data) const override {
- const char ch = json::helper::get_next_element(data);
- if (ch != '[') {
- throw json::malformed_json_exception("Expected an array type");
- }
- ++data;
- V to_make;
- std::vector<V>& vec = val.*ptr;
- while (*data && *data != ']') {
- impl.parse(to_make, data);
- vec.emplace_back(to_make);
- json::helper::advance_to_boundary<']'>(data);
- }
- if (*data) ++data;
- else throw json::malformed_json_exception("Reached end of parse string without finding array end");
- }
- private:
- std::vector<V> T::*ptr;
- binder<V> impl;
- };
-
- template <typename T, typename V>
- class direct_binder<T, std::map<std::string, V> > : public binder_impl<T> {
- public:
- direct_binder(std::vector<V> T::*p, binder<V> const&i);
- virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
-
- virtual void parse(T& val, char const*& data) const override {
- const char ch = json::helper::get_next_element(data);
- if (ch != '{') {
- throw json::malformed_json_exception("Expected an array type");
- }
- ++data;
-
- V to_make;
- std::map<std::string, V>& vec = val.*ptr;
- std::string key;
- while (*data && *data != '}') {
- json::helper::parse_string(key, data);
- if (json::helper::get_next_element(data) != ':') {
- throw json::malformed_json_exception(std::string("Expected key:value pair delimited by ':', got '") + *data + "' instead");
- }
- impl.parse(to_make, ++data);
- vec.emplace(key, to_make);
- json::helper::advance_to_boundary<'}'>(data);
- }
- if (*data) ++data;
- else throw json::malformed_json_exception("Reached end of parse string without finding object end");
- }
- private:
- std::map<std::string, V> T::*ptr;
- binder<V> impl;
- };
-
- template <typename T>
- class object_binder : public binder_impl<T> {
- public:
- object_binder() {}
- virtual binder_impl<T>* clone() const override { return new object_binder(*this); }
- template <typename V>
- object_binder& operator()(std::string const&k, V T::*ptr, binder_impl<V> const&v) {
- return (*this)(k, binder<T>(new direct_binder<T, V>(ptr, binder<V>(v) )));
- }
-
- object_binder& operator()(std::string const&k, binder<T> const&v) {
- mapping.emplace(k, v);
- return *this;
- }
-
- virtual void parse(T& object, char const*& data) const override {
- const char ch = json::helper::get_next_element(data);
- if (ch == '{') {
- parse_object(object, ++data);
- } else {
- throw json::malformed_json_exception(std::string("Expected an object type for binding to ") + typeid(T).name());
- }
- }
-
- void parse_object(T& object, char const*& data) const {
- std::string key;
- while (*data && *data != '}') {
- json::helper::parse_string(key, data);
- if (json::helper::get_next_element(data) != ':') {
- throw json::malformed_json_exception(std::string("Expected key:value pair delimited by ':', got '") + *data + "' instead");
- }
- auto it = mapping.find(key);
- if (it != mapping.end()) {
- it->second.parse(object, ++data);
- } else {
- throw json::malformed_json_exception("Unexpected key " + key);
- }
- json::helper::advance_to_boundary<'}'>(data);
- }
- if (*data) ++data;
- else throw json::malformed_json_exception("Reached end of parse string without finding object end");
- }
- template <typename E>
- object_binder& operator()(std::string const& s, E T::*p) {
- return operator()(s, binder<T>(new direct_binder<T, E>(p)));
- }
- private:
- std::map<std::string, binder<T>> mapping;
- };
-
- template <typename T>
- class tuple_binder : public binder_impl<T> {
- public:
- virtual binder_impl<T>* clone() const override { return new tuple_binder(*this); }
- tuple_binder& operator()(binder<T> const&b) {
- members.push_back(b);
- return *this;
- }
-
- virtual void parse(T& object, char const*& data) const override {
- const char ch = json::helper::get_next_element(data);
- if (ch == '[') {
- parse_tuple(object, ++data);
- } else {
- throw json::malformed_json_exception(std::string("Expected an object type for binding to ") + typeid(T).name());
- }
- }
-
- void parse_tuple(T& object, char const*& data) const {
- auto it = members.begin();
- while (*data && *data != ']' && it != members.end()) {
- it->parse(object, data);
- json::helper::advance_to_boundary<']'>(data);
- ++it;
- }
- if (it != members.end()) {
- throw json::malformed_json_exception("Failed to parse every member of tuple");
- }
- if (*data) ++data;
- else throw json::malformed_json_exception("Reached end of parse string without finding array end");
- }
- template <typename E>
- tuple_binder& operator()(E T::*p) {
- return operator()(binder<T>(new direct_binder<T, E>(p)));
- }
-
- private:
- std::vector<binder<T>> members;
- };
-
- template <typename T>
- class visitor {
- public:
- visitor(T& o, binder_impl<T>& b) : obj(o), b(b) {}
-
- void parse(char const* data) {
- b.parse(obj, data);
- }
-
- private:
- T& obj;
- binder_impl<T>& b;
- };
-
- template <typename T>
- visitor<T> bind(T& object, binder_impl<T>& b) {
- return visitor<T>{object, b};
- }
- }
-
- namespace parser {
- template <typename T>
- void parse(binder::visitor<T>& visitor, char const* data) {
- visitor.parse(data);
- }
- }
-
- }
- #endif /* json_binder_h */
|