json_direct_scalar_binder.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // json_direct_scalar_binder.hpp
  3. // json
  4. //
  5. // Created by Sam Jaffe on 4/23/16.
  6. //
  7. #pragma once
  8. namespace json { namespace binder {
  9. template <typename T>
  10. class direct_binder<T, bool> : public binder_impl<T> {
  11. public:
  12. direct_binder(bool T::*p) : ptr(p) {}
  13. virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
  14. virtual void parse(T& val, char const*& data) const override {
  15. if (!strncmp(data, "true", 4)) {
  16. val.*ptr = true;
  17. } else if (!strncmp(data, "false", 5)) {
  18. val.*ptr = false;
  19. } else {
  20. throw json::malformed_json_exception("Expected a boolean type here");
  21. }
  22. }
  23. virtual void write(T const& val, std::string & data) const override {
  24. data += (val.*ptr ? "true" : "false");
  25. }
  26. private:
  27. bool T::*ptr;
  28. };
  29. template <typename T>
  30. class direct_binder<T, int> : public binder_impl<T> {
  31. public:
  32. direct_binder(int T::*p) : ptr(p) {}
  33. virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
  34. virtual void parse(T& val, char const*& data) const override {
  35. // if (false) {
  36. json::helper::parse_numeric(val.*ptr, data);
  37. // } else {
  38. // throw json::malformed_json_exception("Expected an integral type here");
  39. // }
  40. }
  41. virtual void write(T const& val, std::string & data) const override {
  42. char buffer[16] = { '\0' };
  43. snprintf(buffer, sizeof(buffer), "%d", val.*ptr);
  44. data += buffer;
  45. }
  46. private:
  47. int T::*ptr;
  48. };
  49. template <typename T>
  50. class direct_binder<T, double> : public binder_impl<T> {
  51. public:
  52. direct_binder(double T::*p) : ptr(p) {}
  53. virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
  54. virtual void parse(T& val, char const*& data) const override {
  55. // if (false) {
  56. json::helper::parse_numeric(val.*ptr, data);
  57. // } else {
  58. // throw json::malformed_json_exception("Expected a floating point type here");
  59. // }
  60. }
  61. virtual void write(T const& val, std::string & data) const override {
  62. char buffer[32] = { '\0' };
  63. snprintf(buffer, sizeof(buffer), "%lf", val.*ptr);
  64. data += buffer;
  65. }
  66. private:
  67. double T::*ptr;
  68. };
  69. template <typename T>
  70. class direct_binder<T, std::string> : public binder_impl<T> {
  71. public:
  72. direct_binder(std::string T::*p) : ptr(p) {}
  73. virtual binder_impl<T>* clone() const override { return new direct_binder(*this); }
  74. virtual void parse(T& val, char const*& data) const override {
  75. json::helper::parse_string(val.*ptr, data);
  76. }
  77. virtual void write(T const& val, std::string & data) const override {
  78. data += "\"" + val.*ptr + "\"";
  79. }
  80. private:
  81. std::string T::*ptr;
  82. };
  83. } }