File indexing completed on 2025-01-18 09:50:18
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_READ_HPP
0011 #define BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_READ_HPP
0012
0013 #include <boost/property_tree/json_parser/detail/parser.hpp>
0014 #include <boost/property_tree/json_parser/detail/narrow_encoding.hpp>
0015 #include <boost/property_tree/json_parser/detail/wide_encoding.hpp>
0016 #include <boost/property_tree/json_parser/detail/standard_callbacks.hpp>
0017
0018 #include <boost/static_assert.hpp>
0019 #include <boost/type_traits/is_same.hpp>
0020
0021 #include <istream>
0022 #include <iterator>
0023 #include <string>
0024
0025 namespace boost { namespace property_tree {
0026 namespace json_parser { namespace detail
0027 {
0028
0029 template <typename Iterator, typename Sentinel>
0030 class minirange
0031 {
0032 public:
0033 minirange(Iterator first, Sentinel last) : first(first), last(last) {}
0034 Iterator begin() const { return first; }
0035 Sentinel end() const { return last; }
0036
0037 private:
0038 Iterator first;
0039 Sentinel last;
0040 };
0041 template <typename Iterator, typename Sentinel>
0042 minirange<Iterator, Sentinel> make_minirange(Iterator first, Sentinel last)
0043 {
0044 return minirange<Iterator, Sentinel>(first, last);
0045 }
0046
0047 template <typename Iterator, typename Sentinel,
0048 typename Encoding, typename Callbacks>
0049 void read_json_internal(Iterator first, Sentinel last, Encoding& encoding,
0050 Callbacks& callbacks, const std::string& filename)
0051 {
0052 BOOST_STATIC_ASSERT_MSG((boost::is_same<
0053 typename std::iterator_traits<Iterator>::value_type,
0054 typename Encoding::external_char>::value),
0055 "Encoding is not capable of using the iterator's value type.");
0056 BOOST_STATIC_ASSERT_MSG((boost::is_same<
0057 typename Callbacks::char_type,
0058 typename Encoding::internal_char>::value),
0059 "Encoding is not capable of producing the needed character type.");
0060
0061 detail::parser<Callbacks, Encoding, Iterator, Sentinel>
0062 parser(callbacks, encoding);
0063 parser.set_input(filename, make_minirange(first, last));
0064 parser.parse_value();
0065 parser.finish();
0066 }
0067
0068 template <typename Ch> struct encoding;
0069 template <> struct encoding<char> : utf8_utf8_encoding {};
0070 template <> struct encoding<wchar_t> : wide_wide_encoding {};
0071
0072 template <typename Ptree>
0073 void read_json_internal(
0074 std::basic_istream<typename Ptree::key_type::value_type> &stream,
0075 Ptree &pt, const std::string &filename)
0076 {
0077 typedef typename Ptree::key_type::value_type char_type;
0078 typedef standard_callbacks<Ptree> callbacks_type;
0079 typedef detail::encoding<char_type> encoding_type;
0080 typedef std::istreambuf_iterator<char_type> iterator;
0081 callbacks_type callbacks;
0082 encoding_type encoding;
0083 read_json_internal(iterator(stream), iterator(),
0084 encoding, callbacks, filename);
0085 pt.swap(callbacks.output());
0086 }
0087
0088 }}}}
0089
0090 #endif