json_object_binder.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // json_object_binder.hpp
  3. // json
  4. //
  5. // Created by Sam Jaffe on 4/23/16.
  6. //
  7. #pragma once
  8. #include "json_binder.hpp"
  9. #include "json_binder_discard.hpp"
  10. #include "json_direct_binder.hpp"
  11. #include <map>
  12. #include <set>
  13. #include <string>
  14. namespace json { namespace binder {
  15. template <typename T>
  16. class object_binder : public binder_impl<T> {
  17. public:
  18. object_binder() {}
  19. virtual binder_impl<T>* clone() const override {
  20. return new object_binder(*this);
  21. }
  22. template <typename V>
  23. object_binder& operator()(std::string const&k, V T::*ptr,
  24. binder_impl<V> const&v) {
  25. return (*this)(k, binder<T>(new forward_binder<T, V>(ptr, binder<V>(v))));
  26. }
  27. object_binder& operator()(std::string const&k, binder<T> const&v) {
  28. mapping.emplace(k, v);
  29. return *this;
  30. }
  31. virtual void parse(T& object, char const*& data,
  32. parser::options opts) const override {
  33. const char ch = json::helper::get_next_element(data);
  34. if (ch == '{') {
  35. parse_object(object, ++data, opts);
  36. } else {
  37. throw not_an_object_exception(object);
  38. }
  39. }
  40. virtual void write(T const& val, std::ostream & data) const override {
  41. data << '{';
  42. auto it = mapping.begin(), end = mapping.end();
  43. if (it != end) {
  44. data << '"' << it->first << '"' << ':';
  45. it->second.write(val, data);
  46. for (++it; it != end; ++it) {
  47. data << ',';
  48. data << '"' << it->first << '"' << ':';
  49. it->second.write(val, data);
  50. }
  51. }
  52. data << '}';
  53. }
  54. void parse_object(T& object, char const*& data,
  55. parser::options opts) const {
  56. std::string key;
  57. std::set<std::string> unparsed_keys;
  58. for ( auto & p : mapping ) { unparsed_keys.insert(p.first); }
  59. while (*data && *data != '}') {
  60. if (json::helper::get_next_element(data) != '"') {
  61. throw malformed_object_key(*data);
  62. }
  63. json::helper::parse_string(key, data);
  64. if (json::helper::get_next_element(data) != ':') {
  65. throw malformed_object_association(*data);
  66. }
  67. auto it = mapping.find(key);
  68. if (it != mapping.end()) {
  69. unparsed_keys.erase(key);
  70. it->second.parse(object, ++data, opts);
  71. } else if (opts & parser::disable_unknown_keys) {
  72. throw json::malformed_json_exception("Unexpected key " + key);
  73. } else {
  74. parse_discard_token(++data);
  75. }
  76. json::helper::advance_to_boundary('}', data);
  77. }
  78. if (*data) ++data;
  79. else throw unterminated_json_object();
  80. if ( !unparsed_keys.empty() && opts & parser::disable_missing_keys ) {
  81. throw json::malformed_json_exception{
  82. "missing certain keys from object construction TODO"
  83. };
  84. }
  85. }
  86. template <typename E>
  87. object_binder& operator()(std::string const& s, E T::*p) {
  88. return operator()(s, binder<T>(new direct_binder<T, E>(p)));
  89. }
  90. private:
  91. std::map<std::string, binder<T>> mapping;
  92. };
  93. } }