json_binder_parser.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. namespace json {
  14. class value;
  15. namespace binder { template <typename, typename> class visitor; }
  16. namespace parser {
  17. template <typename T, typename S>
  18. void parse(binder::visitor<T, S>&, char const*, options opts = allow_all);
  19. template <typename T, typename S>
  20. void parse(binder::visitor<T, S>&& v, std::string const& s, options opts = allow_all) {
  21. parse(static_cast<binder::visitor<T, S>&>(v), s.c_str(), opts);
  22. }
  23. template <typename T, typename S>
  24. void parse(binder::visitor<T, S>&& v, std::istream & in, options opts = allow_all) {
  25. if (!in) return;
  26. in.seekg(0, std::ios_base::end);
  27. std::istream::pos_type end = in.tellg();
  28. std::unique_ptr<char[]> data{new char[end]};
  29. in.seekg(0);
  30. in.read(data.get(), end);
  31. parse(static_cast<binder::visitor<T, S>&>(v), data.get(), opts);
  32. }
  33. }
  34. }
  35. #endif /* json_parser_h */