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 13 #include <boost/property_tree/json_parser/detail/parser.hpp> 14 #include <boost/property_tree/json_parser/detail/narrow_encoding.hpp> 15 #include <boost/property_tree/json_parser/detail/wide_encoding.hpp> 16 #include <boost/property_tree/json_parser/detail/standard_callbacks.hpp> 17 18 #include <boost/static_assert.hpp> 19 #include <boost/type_traits/is_same.hpp> 20 21 #include <istream> 22 #include <iterator> 23 #include <string> 24 25 namespace boost { namespace property_tree { 26 namespace json_parser { namespace detail 27 { 28 29 template <typename Iterator, typename Sentinel> 30 class minirange 31 { 32 public: minirange(Iterator first,Sentinel last)33 minirange(Iterator first, Sentinel last) : first(first), last(last) {} begin() const34 Iterator begin() const { return first; } end() const35 Sentinel end() const { return last; } 36 37 private: 38 Iterator first; 39 Sentinel last; 40 }; 41 template <typename Iterator, typename Sentinel> make_minirange(Iterator first,Sentinel last)42 minirange<Iterator, Sentinel> make_minirange(Iterator first, Sentinel last) 43 { 44 return minirange<Iterator, Sentinel>(first, last); 45 } 46 47 template <typename Iterator, typename Sentinel, 48 typename Encoding, typename Callbacks> read_json_internal(Iterator first,Sentinel last,Encoding & encoding,Callbacks & callbacks,const std::string & filename)49 void read_json_internal(Iterator first, Sentinel last, Encoding& encoding, 50 Callbacks& callbacks, const std::string& filename) 51 { 52 BOOST_STATIC_ASSERT_MSG((boost::is_same< 53 typename std::iterator_traits<Iterator>::value_type, 54 typename Encoding::external_char>::value), 55 "Encoding is not capable of using the iterator's value type."); 56 BOOST_STATIC_ASSERT_MSG((boost::is_same< 57 typename Callbacks::char_type, 58 typename Encoding::internal_char>::value), 59 "Encoding is not capable of producing the needed character type."); 60 61 detail::parser<Callbacks, Encoding, Iterator, Sentinel> 62 parser(callbacks, encoding); 63 parser.set_input(filename, make_minirange(first, last)); 64 parser.parse_value(); 65 parser.finish(); 66 } 67 68 template <typename Ch> struct encoding; 69 template <> struct encoding<char> : utf8_utf8_encoding {}; 70 template <> struct encoding<wchar_t> : wide_wide_encoding {}; 71 72 template <typename Ptree> read_json_internal(std::basic_istream<typename Ptree::key_type::value_type> & stream,Ptree & pt,const std::string & filename)73 void read_json_internal( 74 std::basic_istream<typename Ptree::key_type::value_type> &stream, 75 Ptree &pt, const std::string &filename) 76 { 77 typedef typename Ptree::key_type::value_type char_type; 78 typedef standard_callbacks<Ptree> callbacks_type; 79 typedef detail::encoding<char_type> encoding_type; 80 typedef std::istreambuf_iterator<char_type> iterator; 81 callbacks_type callbacks; 82 encoding_type encoding; 83 read_json_internal(iterator(stream), iterator(), 84 encoding, callbacks, filename); 85 pt.swap(callbacks.output()); 86 } 87 88 }}}} 89 90 #endif 91