// // json_direct_vector_binder.hpp // json // // Created by Sam Jaffe on 4/23/16. // #pragma once namespace json { namespace binder { template class direct_binder > : public binder_impl { public: direct_binder(std::vector T::*p, binder const&i); virtual binder_impl* clone() const override { return new direct_binder(*this); } virtual void parse(T& val, char const*& data, parser::options opts) 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& vec = val.*ptr; while (*data && *data != ']') { impl.parse(to_make, data, opts); vec.emplace_back(to_make); json::helper::advance_to_boundary(']', data); } if (*data) ++data; else throw json::unterminated_json_exception("Reached end of parse string without finding array end"); } virtual void write(T const& val, std::ostream & data) const override { data << '['; std::vector const & vec = val.*ptr; typename std::vector::const_iterator it = vec.begin(), end = vec.end(); if (it != end) { impl.write(*it, data); for (++it; it != end; ++it) { data << ","; impl.write(*it, data); } } data << ']'; } private: std::vector T::*ptr; binder impl; }; } }