Ticket #9495: boost_property_tree.patch

File boost_property_tree.patch, 4.8 KB (added by Egbert van der Wal <ewal@…>, 9 years ago)

Patch to add a iterator accepting variant of boost::property_tree::read_json

  • boost/property_tree/json_parser.hpp

     
    4646    }
    4747
    4848    /**
     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    /**
    4973     * Read JSON from a the given file and translate it to a property tree.
    5074     * @note Clears existing contents of property tree.  In case of error the
    5175     *       property tree unmodified.
  • boost/property_tree/detail/json_parser_read.hpp

     
    2929    ///////////////////////////////////////////////////////////////////////
    3030    // Json parser context
    3131       
    32     template<class Ptree>
     32    template<typename It, class Ptree>
    3333    struct context
    3434    {
    3535
    3636        typedef typename Ptree::key_type::value_type Ch;
    3737        typedef std::basic_string<Ch> Str;
    38         typedef typename std::vector<Ch>::iterator It;
    3938       
    4039        Str string;
    4140        Str name;
     
    156155    ///////////////////////////////////////////////////////////////////////
    157156    // Json grammar
    158157
    159     template<class Ptree>
     158    template<typename Iterator, class Ptree>
    160159    struct json_grammar :
    161         public boost::spirit::classic::grammar<json_grammar<Ptree> >
     160        public boost::spirit::classic::grammar<json_grammar<Iterator, Ptree> >
    162161    {
    163162       
    164         typedef context<Ptree> Context;
     163        typedef context<Iterator, Ptree> Context;
    165164        typedef typename Ptree::key_type::value_type Ch;
    166165
    167166        mutable Context c;
     
    296295                            const std::string &filename)
    297296    {
    298297
    299         using namespace boost::spirit::classic;
    300298        typedef typename Ptree::key_type::value_type Ch;
    301         typedef typename std::vector<Ch>::iterator It;
    302299
    303300        // Load data into vector
    304301        std::vector<Ch> v(std::istreambuf_iterator<Ch>(stream.rdbuf()),
    305302                          std::istreambuf_iterator<Ch>());
    306303        if (!stream.good())
    307304            BOOST_PROPERTY_TREE_THROW(json_parser_error("read error", filename, 0));
     305
     306        read_json_internal(v.begin(), v.end(), pt, filename);
     307    }
    308308       
     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
    309319        // Prepare grammar
    310         json_grammar<Ptree> g;
     320        json_grammar<Iterator, Ptree> g;
    311321
    312322        // Parse
    313323        try
    314324        {
    315             parse_info<It> pi = parse(v.begin(), v.end(), g,
     325            parse_info<Iterator> pi = parse(begin, end, g,
    316326                                      space_p | comment_p("//") | comment_p("/*", "*/"));
    317327            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")));
    319329        }
    320         catch (parser_error<std::string, It> &e)
     330        catch (parser_error<std::string, Iterator> &e)
    321331        {
    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)));
    323333        }
    324334
    325335        // Swap grammar context root and pt