nlohmann::json 示例代码


#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <cassert>
#include "json.hpp"

using json = nlohmann::json;


int demo01()
{
    json j;

    // construct
    j["pi"] = 3.141;
    j["happy"] = true;
    j["name"] = "Niels";
    j["nothing"] = nullptr;
    j["answer"]["everything"] = 42;
    j["list"] = { 1, 0, 2 };
    j["object"] = { {"currency", "USD"}, {"value", 42.99} };

    // read
    float pi = j.at("pi");
    std::string name = j.at("name");
    int everything = j.at("answer").at("everything");
    std::cout << pi << std::endl;
    std::cout << name << std::endl;
    std::cout << everything << std::endl;
    for(int i=0; i<3; i++)
    {
        std::cout << j.at("list").at(i) << std::endl;
    }
    std::cout << j.at("object").at("currency") << std::endl;
    std::cout << j.at("object").at("value") << std::endl;

    // write to file
    std::ofstream file("pretty.json");
    file << j << std::endl;

    return 0;
}

int demo02()
{
    json j1 = "{\"happy\":true,\"pi\":3.14}"_json;

    auto j2 = R"({"happy":true,"pi":3.141})"_json;

    std::string s = "{\"happy\":true,\"pi\":3.1415}";
    auto j3 = json::parse(s);
    // auto j3 = json::parse(s.c_str());

    std::cout << j1.at("pi") << std::endl;
    std::cout << j2.at("pi") << std::endl;
    std::cout << j3.at("pi") << std::endl;

    std::string out = j1.dump();
    std::cout << out << std::endl;
    return 0;
}

int demo03()
{
    json j1;
    std::cin >> j1;
    std::cout << j1 << std::endl;

    std::ifstream in("file.json");
    json j2;
    in >> j2;

    std::ofstream out("pretty.json");
    out << std::setw(4) << j2 << std::endl; //std::setw(4) for pretty output

    return 0;
}


namespace ns
{
    struct person {
        std::string name;
        std::string address;
        int age;
    };

    void to_json(json& j, const person& p) {
        j = json{{"name", p.name}, {"address", p.address}, {"age", p.age}};
    }

    void from_json(const json& j, person& p) {
        j.at("name").get_to(p.name);
        j.at("address").get_to(p.address);
        j.at("age").get_to(p.age);
    }
}

int demo04()
{
    ns::person p1 {"Ned Flanders", "744 Evergreen Terrace", 60};

    // struct ---->  json
    json j = p1;
    std::cout << j << std::endl;
    // {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}

    // json ---->  struct
    auto p2 = j.get<ns::person>();

    // that's it
    assert(p1.address == p2.address);
    assert(p1.age == p2.age);
    assert(p1.name == p2.name);
    return 0;
}


int demo05()
{
    // create a JSON value
    json j = R"({"compact": true, "schema": 0})"_json;

    // serialize to BSON
    std::vector<std::uint8_t> v_bson = json::to_bson(j);

    // 0x1B, 0x00, 0x00, 0x00, 0x08, 0x63, 0x6F, 0x6D, 0x70, 0x61, 0x63, 0x74, 0x00, 0x01, 0x10, 0x73, 0x63, 0x68, 0x65, 0x6D, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

    // roundtrip
    json j_from_bson = json::from_bson(v_bson);

    // serialize to CBOR
    std::vector<std::uint8_t> v_cbor = json::to_cbor(j);

    // 0xA2, 0x67, 0x63, 0x6F, 0x6D, 0x70, 0x61, 0x63, 0x74, 0xF5, 0x66, 0x73, 0x63, 0x68, 0x65, 0x6D, 0x61, 0x00

    // roundtrip
    json j_from_cbor = json::from_cbor(v_cbor);

    // serialize to MessagePack
    std::vector<std::uint8_t> v_msgpack = json::to_msgpack(j);

    // 0x82, 0xA7, 0x63, 0x6F, 0x6D, 0x70, 0x61, 0x63, 0x74, 0xC3, 0xA6, 0x73, 0x63, 0x68, 0x65, 0x6D, 0x61, 0x00

    // roundtrip
    json j_from_msgpack = json::from_msgpack(v_msgpack);

    // serialize to UBJSON
    std::vector<std::uint8_t> v_ubjson = json::to_ubjson(j);

    // 0x7B, 0x69, 0x07, 0x63, 0x6F, 0x6D, 0x70, 0x61, 0x63, 0x74, 0x54, 0x69, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6D, 0x61, 0x69, 0x00, 0x7D

    // roundtrip
    json j_from_ubjson = json::from_ubjson(v_ubjson);
    return 0;

}


int demo6()
{
    json j = R"({
        "output": {
            "width": 720,
            "height": 1080,
            "frameRate": 20,
            "crf": 31
        },
        "tracks": [
            {
                "name": "t1",
                "pieces": [
                    {
                    "file": "x.mp4",
                    "startTime": 2,
                    "endTime": 6
                    },
                    {
                    "file": "y.mp4",
                    "startTime": 9,
                    "endTime": 13
                    }
                ]
            },
            {
                "name": "t2",
                "pieces": [
                    {
                    "file": "z.mp4",
                    "startTime": 0,
                    "endTime": 10
                    }
                ]
            }
        ]
    })"_json;
    // std::cout << j["output"].at("width") << std::endl;
    std::cout << j.at("output").at("width") << std::endl;
    std::cout << j.at("output").at("height") << std::endl;
    std::cout << j.at("output").at("frameRate") << std::endl;
    std::cout << j.at("output").at("crf") << std::endl;

    for(int i=0; i<j["tracks"].size(); i++) {
        std::cout << j["tracks"][i].at("name") << std::endl;

        json j2 = j["tracks"][i].at("pieces");
        for(int k=0; k<j2.size(); k++) {
            std::cout << j2[k].at("file") << std::endl;
            std::cout << j2[k].at("startTime") << std::endl;
            std::cout << j2[k].at("endTime") << std::endl;
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值