Of Horses gift: some discussions about the boost property_tree

I recently work to use c ++ json analytic methods, it is checked on the net a lot of articles to learn.

Starting point to complain: find a lot of articles, but the feeling of the majority of similar content, depth, lack their own analysis and slightly profound insights.

Then the following authors would like to thank these two articles:

http://www.voidcn.com/article/p-smrzeuyb-bud.html

http://einverne.github.io/post/2016/01/boost-learning-note-7.html

It took about 2-3 hours of time to carefully study a bit, rewrite the following test code:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>

using namespace boost::property_tree;
using namespace std;

void test1_2(){
    //http://www.voidcn.com/article/p-smrzeuyb-bud.html
    std::string const sample = R"(
    {
       "background": {
          "scripts": [ "name1.js", "name2.js", "name3.js" ]
       },
       "default_popup": "popup.html",
       "default_title": "__MSG_name__",
       "content_scripts": [ {
          "all_frames": true,
          "js": [ "name4.js", "name5.js", "name6.js" ],
          "match_about_blank": true,
          "matches": [ "http://*/*", "https://*/*" ],
          "run_at": "document_start"
       }, {
          "all_frames": true,
          "js": [ "include.postload.js" ],
          "match_about_blank": true,
          "matches": [ "http://*/*", "https://*/*" ],
          "run_at": "document_end"
       } ]
    })";    
    //printf("sample=%s\n",sample.c_str());
    
    ptree pt;
    stringstream stream1(sample);
    read_json(stream1,pt);
    
    struct temp_fxn1 {
        //ref: basic_ptree public member functions
        static int ptree_type(const ptree &pt){
            if(pt.size()>0){
                if(pt.begin()->first==""){
                    return 1;//array
                }else{
                    return 2;//object
                }
            }else{
                return 0;//leaf   
            }
        }
    };
    
    //遍历
    //for(auto &e : pt){
    //故意这样写:ref http://einverne.github.io/post/2016/01/boost-learning-note-7.html
    for(boost::property_tree::ptree::iterator it = pt.begin(); it != pt.end(); ++it){
        auto &e=*it;
        printf("===>\n");
        std::cout << e.first << ',' << e.second.get_value<std::string>()<<","<<temp_fxn1::ptree_type(e.second)<<"\n";
        printf("~~~~>\n");
        for(auto &c : e.second){
            std::cout << c.first << "\n";
        }
    }
}

Parsing code realizes the functions of any json string (not including error handling), wherein temp_fxn1 :: ptree_type () function for determining a node type ptree.

Note 1: parsing json str, as long as str itself in line with the pre-press json can generate a ptree, does not need to know json str inside which specific fields. This article is most unresolved issues.

Note 2: I do not like for (auto & e: pt) {...} and the wording BOOST_FOREACH.

Induced c ++ beginners: It is recommended commuting carefully about this program, although I level is not high, but still spent a lot of thought to. This code contains a lot of inside knowledge of c ++.

I have engaged in c ++ coding more than 20 years, do not even know R "..." such wording, really ashamed. It seems everyone together to exchange is very important.

Guess you like

Origin www.cnblogs.com/oldfan1974/p/11391506.html