//
// Created by 阳坤 on 2021/5/20.
//
#include "json/json.h"
#include <iostream>
/**
 * \brief Parse a raw string into Value object using the CharReaderBuilder
 * class, or the legacy Reader class.
 * Example Usage:
 * $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
 * $./readFromString
 * colin
 * 20
 */
using namespace std;

int test1() {
    string rawJson = R"({"Age": 20, "Name": "DevYK"})";
    const auto rawJsonLength = static_cast<int>(rawJson.length());
    constexpr bool shouldUseOldWay = false;
    JSONCPP_STRING err;
    Json::Value root;

    if (shouldUseOldWay) {
        Json::Reader reader;
        reader.parse(rawJson, root);
    } else {
        Json::CharReaderBuilder builder;
        const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
        if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
                           &err)) {
            std::cout << "error" << std::endl;
            return EXIT_FAILURE;
        }
    }
    const std::string name = root["Name"].asString();
    const int age = root["Age"].asInt();

    std::cout << name << std::endl;
    std::cout << age << std::endl;
    return 1;
}

string writeArrayJson() {
    Json::Value root;
    Json::Value arrayObj;
    Json::Value item;
    item["nalu_type"] = "I";
    item["nalu_pos"] = 0;
    item["nalu_num"] = 0;
    item["nalu_tag"] = 0;

    Json::Value item1;
    item1["nalu_type"] = "B";
    item1["nalu_pos"] = 100;
    item1["nalu_tag"] = 0;
    item1["nalu_num"] = 1;


    Json::Value item2;
    item2["nalu_type"] = "B";
    item2["nalu_pos"] = 300;
    item2["nalu_tag"] = 0;
    item2["nalu_num"] = 2;

    Json::Value item3;
    item3["nalu_type"] = "P";
    item3["nalu_pos"] = 3;
    item3["nalu_tag"] = 0;
    item3["nalu_num"] = 3;

    Json::Value item4;
    item4["nalu_type"] = "I";
    item4["nalu_pos"] = 1000;
    item4["nalu_tag"] = 0;
    item4["nalu_num"] = 4;


    arrayObj.append(item);
    arrayObj.append(item1);
    arrayObj.append(item2);
    arrayObj.append(item3);
    arrayObj.append(item4);

    //增加对象数组
    root["h264_info"] = arrayObj;
    root["ContainI"] = true;
    root["ContainB"] = false;
    root["ContainP"] = true;
    root.toStyledString();
    string out = root.toStyledString();
    return out;
}

int readArrayJson(string &json) {
    Json::Reader reader;
    Json::Value value;

    if (reader.parse(json, value)) {

        cout << "\n\n\n\n ContainI : "  << value["ContainI"].asBool() << endl;
        cout << " ContainB : "  << value["ContainB"].asBool() << endl;
        cout << " ContainP : "  << value["ContainP"].asBool() << endl;

        //解析对象数组对象
        Json::Value arrayObj = value["h264_info"];
        cout << "读取数据 h264-info size = " << arrayObj.size() << ",\n" << endl;
        for (unsigned int i = 0; i < arrayObj.size(); i++) {
            cout << arrayObj[i] << ",\n\n";
        }
    }
    return 1;
}

int main() {
    //简单测试
//    test1();

    //写入数组
    string json = writeArrayJson();
    cout << "存入数据 json write : " << endl << json << endl;
    //读取数组
    readArrayJson(json);
    return EXIT_SUCCESS;
}