json庫 rapidjson的簡單使用

json庫 rapidjson的簡單使用

讀取json文件到rapidjson格式

#include "rapidjson/document.h"    // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON


rapidjson::Document readJson(const char* jsonFile)
{
    std::ifstream t;
    long long length = 0;
    t.open(jsonFile); // open input file
    if (!t.is_open())
    {
        LOGGING_ERROR("Config file open error, file path : %s", jsonFile);
    }

    t.seekg(0, std::ios::end); // go to the end
    length = t.tellg(); // report location (this is the length)
    t.seekg(0, std::ios::beg);// go back to the beginning

    char* configData = new char[length];// allocate memory for a buffer of appropriate dimension
    if (!configData)
    {
        LOGGING_ERROR("Config file new memory error");
    }

    t.read(configData, length);// read the whole file into the buffer
    t.close();// close file handle

    rapidjson::Document configDocument;
    configDocument.Parse<rapidjson::kParseStopWhenDoneFlag>((const char*)configData);
    if (!configDocument.IsObject())
    {
        LOGGING_ERROR("%s content is not json type", jsonFile);
    }

    free(configData);

    return configDocument;
}

保存數據到接送文件

#include "rapidjson/document.h"    // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON

int saveToFile(std::string filename)
{
 	rapidjson::StringBuffer s;
    //rapidjson::Writer<rapidjson::StringBuffer> writer(s); //不使用格式化輸出
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(s);  //使用格式化輸出
	
	writer.StartObject();
    writer.Key("rows");
    writer.Int(3);
    writer.Key("cols");
    writer.Int(4);
   
    {
        writer.Key("arr_name"); //object中任何值都必須有名字
        writer.StartArray();
        for (size_t i = 0; i < 5; i++)
        {
        		writer.Uint((unsigned int)i);
                writer.StartObject();
                writer.Key("index");
                writer.Uint((unsigned int)i);
                writer.EndObject();
        }
        writer.EndArray();
    }

	 writer.EndObject();
	 
    std::ofstream outfile(jsonFile,std::ios::trunc);
    outfile << s.GetString();
    outfile.close(); 
 
    return 0;
}

從文件讀是Jon數據

  int readMesh(const char* mesh_json)
  {
        rapidjson::Document configDocument = readJson(mesh_path);
        if (!configDocument.HasMember("mat_cols") || !configDocument.HasMember("mat_rows"))
        {
            prinrf("ERROR: %s file no mat_cols or mat_rows", mesh_path);
            return -1;
        }
		
		int mat_cols = configDocument["mat_cols"].GetInt();
        int mat_rows = configDocument["mat_rows"].GetInt();

		rapidjson::Document::Array local_rects_document = configDocument["loc_rects"].GetArray();
        for (unsigned int i = 0; i < local_rects_document.Size(); i++)
        {
            int ndex     = local_rects_document[i]["index"].GetInt();
            double start_x   = local_rects_document[i]["x0"].GetDouble();
            double start_y   = local_rects_document[i]["y0"].GetDouble();
            double end_x     = local_rects_document[i]["x1"].GetDouble(); 
            double end_y     = local_rects_document[i]["y1"].GetDouble();
        }
        
  }

官方網站: http://rapidjson.org/md_doc_tutorial.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章