json_binder.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // json_binder.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 1/31/16.
  6. // Copyright © 2016 Sam Jaffe. All rights reserved.
  7. //
  8. #ifndef json_binder_h
  9. #define json_binder_h
  10. #pragma once
  11. #include "json_common.hpp"
  12. #include <map>
  13. #include <string>
  14. #include <sstream>
  15. #include <vector>
  16. #include <list>
  17. namespace json {
  18. namespace binder {
  19. template <typename T>
  20. class binder_impl {
  21. public:
  22. virtual binder_impl<T>* clone() const = 0;
  23. virtual ~binder_impl() {}
  24. virtual void parse(T&, char const*&, parser::options) const = 0;
  25. virtual void write(T const&, std::ostream &) const = 0;
  26. };
  27. template <typename T>
  28. class binder {
  29. public:
  30. binder() : impl(nullptr) {}
  31. binder(binder const& other) : impl(other.impl->clone()) {}
  32. binder(binder_impl<T> const* p) : impl(p) {}
  33. binder(binder_impl<T> const& r) : impl(r.clone()) {}
  34. ~binder() { delete impl; }
  35. void parse(T& object, char const*& data, parser::options opts) const {
  36. if (!impl) return; // error?
  37. impl->parse(object, data, opts);
  38. }
  39. void write(T const& object, std::ostream & data) const {
  40. if (!impl) return; // error?
  41. impl->write(object, data);
  42. }
  43. private:
  44. binder_impl<T> const* impl;
  45. };
  46. template <typename T, typename S = T>
  47. class visitor {
  48. public:
  49. visitor(S& o, binder_impl<T>& b) : obj(o), b(b) {}
  50. void parse(char const* data, parser::options opts) {
  51. b.parse(obj, data, opts);
  52. if ( json::helper::get_next_element(data) && opts & parser::disable_concatenated_json_bodies ) {
  53. throw malformed_json_exception("Config set to require json input be terminated");
  54. }
  55. }
  56. void write(std::ostream & data) const {
  57. b.write(obj, data);
  58. }
  59. private:
  60. S& obj;
  61. binder_impl<T>& b;
  62. };
  63. template <typename T, typename S>
  64. visitor<T, S> bind(S& object, binder_impl<T>& b) {
  65. return {object, b};
  66. }
  67. }
  68. namespace parser {
  69. template <typename T>
  70. void parse(binder::visitor<T>& visitor, char const* data, options opts = allow_all) {
  71. visitor.parse(data, opts);
  72. }
  73. template <typename T, typename S>
  74. void write(binder::visitor<T, S> const & visitor, std::ostream & out) {
  75. visitor.write(out);
  76. }
  77. template <typename T, typename S>
  78. void write(binder::visitor<T, S> const & visitor, std::string & data) {
  79. std::stringstream ss;
  80. visitor.write(ss);
  81. data = ss.str();
  82. }
  83. }
  84. }
  85. #include "binder/json_binder_parser.hpp"
  86. #include "binder/json_direct_binder.hpp"
  87. #include "binder/json_tuple_binder.hpp"
  88. #include "binder/json_object_binder.hpp"
  89. #include "binder/json_direct_get_binder.hpp"
  90. #include "binder/json_direct_map_binder.hpp"
  91. #include "binder/json_direct_scalar_binder.hpp"
  92. #include "binder/json_direct_vector_binder.hpp"
  93. #include "binder/json_pointer_binder.hpp"
  94. #include "binder/json_polymorphic_binder.hpp"
  95. #endif /* json_binder_h */