read.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2015 Sebastian Redl
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see www.boost.org
  9. // ----------------------------------------------------------------------------
  10. #ifndef BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_READ_HPP
  11. #define BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_READ_HPP
  12. #include <boost/property_tree/json_parser/detail/parser.hpp>
  13. #include <boost/property_tree/json_parser/detail/narrow_encoding.hpp>
  14. #include <boost/property_tree/json_parser/detail/wide_encoding.hpp>
  15. #include <boost/property_tree/json_parser/detail/standard_callbacks.hpp>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/type_traits/is_same.hpp>
  18. #include <istream>
  19. #include <iterator>
  20. #include <string>
  21. namespace boost { namespace property_tree {
  22. namespace json_parser { namespace detail
  23. {
  24. template <typename Iterator, typename Sentinel>
  25. class minirange
  26. {
  27. public:
  28. minirange(Iterator first, Sentinel last) : first(first), last(last) {}
  29. Iterator begin() const { return first; }
  30. Sentinel end() const { return last; }
  31. private:
  32. Iterator first;
  33. Sentinel last;
  34. };
  35. template <typename Iterator, typename Sentinel>
  36. minirange<Iterator, Sentinel> make_minirange(Iterator first, Sentinel last)
  37. {
  38. return minirange<Iterator, Sentinel>(first, last);
  39. }
  40. template <typename Iterator, typename Sentinel,
  41. typename Encoding, typename Callbacks>
  42. void read_json_internal(Iterator first, Sentinel last, Encoding& encoding,
  43. Callbacks& callbacks, const std::string& filename)
  44. {
  45. BOOST_STATIC_ASSERT_MSG((boost::is_same<
  46. typename std::iterator_traits<Iterator>::value_type,
  47. typename Encoding::external_char>::value),
  48. "Encoding is not capable of using the iterator's value type.");
  49. BOOST_STATIC_ASSERT_MSG((boost::is_same<
  50. typename Callbacks::char_type,
  51. typename Encoding::internal_char>::value),
  52. "Encoding is not capable of producing the needed character type.");
  53. detail::parser<Callbacks, Encoding, Iterator, Sentinel>
  54. parser(callbacks, encoding);
  55. parser.set_input(filename, make_minirange(first, last));
  56. parser.parse_value();
  57. parser.finish();
  58. }
  59. template <typename Ch> struct encoding;
  60. template <> struct encoding<char> : utf8_utf8_encoding {};
  61. template <> struct encoding<wchar_t> : wide_wide_encoding {};
  62. template <typename Ptree>
  63. void read_json_internal(
  64. std::basic_istream<typename Ptree::key_type::value_type> &stream,
  65. Ptree &pt, const std::string &filename)
  66. {
  67. typedef typename Ptree::key_type::value_type char_type;
  68. typedef standard_callbacks<Ptree> callbacks_type;
  69. typedef detail::encoding<char_type> encoding_type;
  70. typedef std::istreambuf_iterator<char_type> iterator;
  71. callbacks_type callbacks;
  72. encoding_type encoding;
  73. read_json_internal(iterator(stream), iterator(),
  74. encoding, callbacks, filename);
  75. pt.swap(callbacks.output());
  76. }
  77. }}}}
  78. #endif