| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- //
- // 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
- #include "json_common.hpp"
- #include <map>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <list>
- 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*&, parser::options) const = 0;
- virtual void write(T const&, std::ostream &) const = 0;
- };
- template <typename T>
- class binder {
- public:
- binder() : impl(nullptr) {}
- binder(binder const& other) : impl(other.impl->clone()) {}
- binder(binder_impl<T> const* p) : impl(p) {}
- binder(binder_impl<T> const& r) : impl(r.clone()) {}
-
- ~binder() { delete impl; }
-
- void parse(T& object, char const*& data, parser::options opts) const {
- if (!impl) return; // error?
- impl->parse(object, data, opts);
- }
-
- void write(T const& object, std::ostream & data) const {
- if (!impl) return; // error?
- impl->write(object, data);
- }
- private:
- binder_impl<T> const* impl;
- };
-
- template <typename T, typename S = T>
- class visitor {
- public:
- visitor(S& o, binder_impl<T>& b) : obj(o), b(b) {}
-
- void parse(char const* data, parser::options opts) {
- b.parse(obj, data, opts);
- if ( json::helper::get_next_element(data) && opts & parser::disable_concatenated_json_bodies ) {
- throw malformed_json_exception("Config set to require json input be terminated");
- }
- }
-
- void write(std::ostream & data) const {
- b.write(obj, data);
- }
-
- private:
- S& obj;
- binder_impl<T>& b;
- };
-
- template <typename T, typename S>
- visitor<T, S> bind(S& object, binder_impl<T>& b) {
- return {object, b};
- }
- }
-
- namespace parser {
- template <typename T>
- void parse(binder::visitor<T>& visitor, char const* data, options opts = allow_all) {
- visitor.parse(data, opts);
- }
-
- template <typename T, typename S>
- void write(binder::visitor<T, S> const & visitor, std::ostream & out) {
- visitor.write(out);
- }
-
- template <typename T, typename S>
- void write(binder::visitor<T, S> const & visitor, std::string & data) {
- std::stringstream ss;
- visitor.write(ss);
- data = ss.str();
- }
- }
- }
- #include "binder/json_binder_parser.hpp"
- #include "binder/json_direct_binder.hpp"
- #include "binder/json_tuple_binder.hpp"
- #include "binder/json_object_binder.hpp"
- #include "binder/json_direct_get_binder.hpp"
- #include "binder/json_direct_map_binder.hpp"
- #include "binder/json_direct_scalar_binder.hpp"
- #include "binder/json_direct_vector_binder.hpp"
- #include "binder/json_pointer_binder.hpp"
- #include "binder/json_polymorphic_binder.hpp"
- #endif /* json_binder_h */
|