json_binder_parser.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // json_binder.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 1/31/16.
  6. // Copyright © 2016 Sam Jaffe. All rights reserved.
  7. //
  8. #ifndef json_parser_h
  9. #define json_parser_h
  10. #pragma once
  11. #include <iostream>
  12. #include <memory>
  13. #include "json/json_common.hpp"
  14. namespace json {
  15. class value;
  16. namespace binder { template <typename, typename> class visitor; }
  17. namespace parser {
  18. template <typename T, typename S>
  19. void parse(binder::visitor<T, S>&, char const*, options opts = allow_all);
  20. template <typename T, typename S>
  21. void parse(binder::visitor<T, S>&& v, std::string const& s,
  22. options opts = allow_all) {
  23. parse(static_cast<binder::visitor<T, S>&>(v), s.c_str(), opts);
  24. }
  25. template <typename T, typename S>
  26. void parse(binder::visitor<T, S>&& v, std::istream & in,
  27. options opts = allow_all) {
  28. if (!in) return;
  29. in.seekg(0, std::ios_base::end);
  30. std::istream::pos_type end = in.tellg();
  31. std::unique_ptr<char[]> data{new char[end]};
  32. in.seekg(0);
  33. in.read(data.get(), end);
  34. parse(static_cast<binder::visitor<T, S>&>(v), data.get(), opts);
  35. }
  36. }
  37. }
  38. #endif /* json_parser_h */