Index: boost/property_tree/json_parser.hpp =================================================================== --- boost/property_tree/json_parser.hpp (revision 86799) +++ boost/property_tree/json_parser.hpp (working copy) @@ -46,6 +46,30 @@ } /** + * Read JSON from a the given begin and end iterator and translate it to + * a property tree. + * @note Clears existing contents of property tree. In case of error the + * property tree unmodified. + * @note Items of JSON arrays are translated into ptree keys with empty + * names. Members of objects are translated into named keys. + * @note JSON data can be a string, a numeric value, or one of literals + * "null", "true" and "false". During parse, any of the above is + * copied verbatim into ptree data string. + * @throw json_parser_error In case of error deserializing the property + * tree. + * @param begin Iterator from which to start reading the property tree. + * @param end Iterator until which to read the property tree. + * @param[out] pt The property tree to populate. + */ + template + void read_json(Iterator begin, + Iterator end, + Ptree &pt) + { + read_json_internal(begin, end, pt, std::string()); + } + + /** * Read JSON from a the given file and translate it to a property tree. * @note Clears existing contents of property tree. In case of error the * property tree unmodified. Index: boost/property_tree/detail/json_parser_read.hpp =================================================================== --- boost/property_tree/detail/json_parser_read.hpp (revision 86799) +++ boost/property_tree/detail/json_parser_read.hpp (working copy) @@ -29,13 +29,12 @@ /////////////////////////////////////////////////////////////////////// // Json parser context - template + template struct context { typedef typename Ptree::key_type::value_type Ch; typedef std::basic_string Str; - typedef typename std::vector::iterator It; Str string; Str name; @@ -156,12 +155,12 @@ /////////////////////////////////////////////////////////////////////// // Json grammar - template + template struct json_grammar : - public boost::spirit::classic::grammar > + public boost::spirit::classic::grammar > { - typedef context Context; + typedef context Context; typedef typename Ptree::key_type::value_type Ch; mutable Context c; @@ -296,30 +295,41 @@ const std::string &filename) { - using namespace boost::spirit::classic; typedef typename Ptree::key_type::value_type Ch; - typedef typename std::vector::iterator It; // Load data into vector std::vector v(std::istreambuf_iterator(stream.rdbuf()), std::istreambuf_iterator()); if (!stream.good()) BOOST_PROPERTY_TREE_THROW(json_parser_error("read error", filename, 0)); + + read_json_internal(v.begin(), v.end(), pt, filename); + } + template + void read_json_internal(Iterator begin, + Iterator end, + Ptree &pt, + const std::string &filename) + { + + using namespace boost::spirit::classic; + typedef typename Ptree::key_type::value_type Ch; + // Prepare grammar - json_grammar g; + json_grammar g; // Parse try { - parse_info pi = parse(v.begin(), v.end(), g, + parse_info pi = parse(begin, end, g, space_p | comment_p("//") | comment_p("/*", "*/")); if (!pi.hit || !pi.full) - BOOST_PROPERTY_TREE_THROW((parser_error(v.begin(), "syntax error"))); + BOOST_PROPERTY_TREE_THROW((parser_error(begin, "syntax error"))); } - catch (parser_error &e) + catch (parser_error &e) { - BOOST_PROPERTY_TREE_THROW(json_parser_error(e.descriptor, filename, count_lines(v.begin(), e.where))); + BOOST_PROPERTY_TREE_THROW(json_parser_error(e.descriptor, filename, count_lines(begin, e.where))); } // Swap grammar context root and pt