Ticket #9495: boost_property_tree.patch
File boost_property_tree.patch, 4.8 KB (added by , 9 years ago) |
---|
-
boost/property_tree/json_parser.hpp
46 46 } 47 47 48 48 /** 49 * Read JSON from a the given begin and end iterator and translate it to 50 * a property tree. 51 * @note Clears existing contents of property tree. In case of error the 52 * property tree unmodified. 53 * @note Items of JSON arrays are translated into ptree keys with empty 54 * names. Members of objects are translated into named keys. 55 * @note JSON data can be a string, a numeric value, or one of literals 56 * "null", "true" and "false". During parse, any of the above is 57 * copied verbatim into ptree data string. 58 * @throw json_parser_error In case of error deserializing the property 59 * tree. 60 * @param begin Iterator from which to start reading the property tree. 61 * @param end Iterator until which to read the property tree. 62 * @param[out] pt The property tree to populate. 63 */ 64 template<typename Iterator, class Ptree> 65 void read_json(Iterator begin, 66 Iterator end, 67 Ptree &pt) 68 { 69 read_json_internal(begin, end, pt, std::string()); 70 } 71 72 /** 49 73 * Read JSON from a the given file and translate it to a property tree. 50 74 * @note Clears existing contents of property tree. In case of error the 51 75 * property tree unmodified. -
boost/property_tree/detail/json_parser_read.hpp
29 29 /////////////////////////////////////////////////////////////////////// 30 30 // Json parser context 31 31 32 template< class Ptree>32 template<typename It, class Ptree> 33 33 struct context 34 34 { 35 35 36 36 typedef typename Ptree::key_type::value_type Ch; 37 37 typedef std::basic_string<Ch> Str; 38 typedef typename std::vector<Ch>::iterator It;39 38 40 39 Str string; 41 40 Str name; … … 156 155 /////////////////////////////////////////////////////////////////////// 157 156 // Json grammar 158 157 159 template< class Ptree>158 template<typename Iterator, class Ptree> 160 159 struct json_grammar : 161 public boost::spirit::classic::grammar<json_grammar< Ptree> >160 public boost::spirit::classic::grammar<json_grammar<Iterator, Ptree> > 162 161 { 163 162 164 typedef context< Ptree> Context;163 typedef context<Iterator, Ptree> Context; 165 164 typedef typename Ptree::key_type::value_type Ch; 166 165 167 166 mutable Context c; … … 296 295 const std::string &filename) 297 296 { 298 297 299 using namespace boost::spirit::classic;300 298 typedef typename Ptree::key_type::value_type Ch; 301 typedef typename std::vector<Ch>::iterator It;302 299 303 300 // Load data into vector 304 301 std::vector<Ch> v(std::istreambuf_iterator<Ch>(stream.rdbuf()), 305 302 std::istreambuf_iterator<Ch>()); 306 303 if (!stream.good()) 307 304 BOOST_PROPERTY_TREE_THROW(json_parser_error("read error", filename, 0)); 305 306 read_json_internal(v.begin(), v.end(), pt, filename); 307 } 308 308 309 template<typename Iterator, class Ptree> 310 void read_json_internal(Iterator begin, 311 Iterator end, 312 Ptree &pt, 313 const std::string &filename) 314 { 315 316 using namespace boost::spirit::classic; 317 typedef typename Ptree::key_type::value_type Ch; 318 309 319 // Prepare grammar 310 json_grammar< Ptree> g;320 json_grammar<Iterator, Ptree> g; 311 321 312 322 // Parse 313 323 try 314 324 { 315 parse_info<It > pi = parse(v.begin(), v.end(), g,325 parse_info<Iterator> pi = parse(begin, end, g, 316 326 space_p | comment_p("//") | comment_p("/*", "*/")); 317 327 if (!pi.hit || !pi.full) 318 BOOST_PROPERTY_TREE_THROW((parser_error<std::string, It >(v.begin(), "syntax error")));328 BOOST_PROPERTY_TREE_THROW((parser_error<std::string, Iterator>(begin, "syntax error"))); 319 329 } 320 catch (parser_error<std::string, It > &e)330 catch (parser_error<std::string, Iterator> &e) 321 331 { 322 BOOST_PROPERTY_TREE_THROW(json_parser_error(e.descriptor, filename, count_lines<It , Ch>(v.begin(), e.where)));332 BOOST_PROPERTY_TREE_THROW(json_parser_error(e.descriptor, filename, count_lines<Iterator, Ch>(begin, e.where))); 323 333 } 324 334 325 335 // Swap grammar context root and pt