| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- //
- // json_direct_map_binder.hpp
- // json
- //
- // Created by Sam Jaffe on 4/23/16.
- //
- #pragma once
- namespace json { namespace binder {
- 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");
- }
-
- virtual void write(T const& val, std::string & data) const override {
- data += "{";
- std::map<std::string, V> const & map = val.*ptr;
- for (typename std::map<std::string, V>::const_iterator it = map.begin(),
- end = map.end(); it != end; ++it) {
- data += "\"" + it->first + "\":";
- impl.write(it->second, data);
- data += ",";
- }
- data.back() = '}';
- }
- private:
- std::map<std::string, V> T::*ptr;
- binder<V> impl;
- };
- } }
|