json_pointer_binder.hpp 952 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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> class pointer_binder : public binder_impl<P> {
  12. // Maybe a reference type
  13. using Tr = decltype(*std::declval<P>());
  14. using T = typename std::remove_reference<Tr>::type;
  15. public:
  16. pointer_binder(binder<T> impl) : m_impl(impl) {}
  17. virtual binder_impl<P> * clone() const { return new pointer_binder(*this); }
  18. virtual void parse(P & v, char const *& data, parser::options opts) const {
  19. if (!strncmp(data, "null", 4))
  20. v = nullptr;
  21. else
  22. m_impl.parse(*(v = P(new T)), data, opts);
  23. }
  24. void write(P const & v, std::ostream & os) const {
  25. if (v)
  26. m_impl.write(*v, os);
  27. else
  28. os << "null";
  29. }
  30. private:
  31. binder<T> m_impl;
  32. };
  33. }}