json_pointer_binder.hpp 931 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // json_pointer_binder.hpp
  3. // json
  4. //
  5. // Created by Sam Jaffe on 11/15/18.
  6. // Copyright © 2018 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include "json_binder.hpp"
  10. namespace json { namespace binder {
  11. template <typename P>
  12. class pointer_binder : public binder_impl<P> {
  13. // Maybe a reference type
  14. using Tr = decltype(*std::declval<P>());
  15. using T = typename std::remove_reference<Tr>::type;
  16. public:
  17. pointer_binder(binder<T> impl) : m_impl(impl) {}
  18. virtual binder_impl<P>* clone() const { return new pointer_binder(*this); }
  19. virtual void parse(P & v, char const*& data, parser::options opts) const {
  20. if (!strncmp(data, "null", 4)) v = nullptr;
  21. else m_impl.parse(*(v = P(new T)), data, opts);
  22. }
  23. void write(P const & v, std::ostream & os) const {
  24. if (v) m_impl.write(*v, os);
  25. else os << "null";
  26. }
  27. private:
  28. binder<T> m_impl;
  29. };
  30. } }