json_object_binder.hpp 3.1 KB

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