C++ nolhmann/json

#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

int main()
{
    // create JSON values
    json object = {{"one", 1}, {"two", 2}};
    json null;

    // print values
    std::cout << object << '\n'; // expect {"one":1,"two":2}

    std::cout << null << '\n'; // expect null

    // emplace member function add values, 默认对key进行字典排序
    auto res1 = object.emplace("three", 3);
    null.emplace("A", "a");
    null.emplace("B", "b");

    // the following call will not add an object, because there is already
    // a value stored at key "B"
    auto res2 = null.emplace("B", "c");

    // print values
    std::cout << object << '\n'; // expect {"one":1,"three":3,"two":2}

    // 添加失败,返回值res1.second为false,成功则为true
    std::cout << *res1.first << " " << std::boolalpha << res1.second << '\n'; // 3 true

    std::cout << null << '\n';// expect {"A":"a","B":"b"}
    std::cout << *res2.first << " " << std::boolalpha << res2.second << '\n';// "b" false
}
#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

class ReqSelector;

typedef std::shared_ptr<ReqSelector> ReqSelectorPtr;
struct ReqSelector{
    int left;
    int top;
};

template<typename T, typename R> 
struct JSONOperator {
    void To_Json(T &t, R& r) {
    }

    void From_Json(T& t, R& r) {
    }
};

template<typename T> 
struct JSONOperator<T, ReqSelectorPtr> {
    void To_Json(T& t, ReqSelectorPtr& r) {
        t = json{{"left", r->left}, {"top", r->top}};
    }

    void From_Json(T& t, ReqSelectorPtr& r) {
        t["left"].get_to(r->left);
        t["top"].get_to(r->top);
    }
};

template<typename T, typename Q>
void From_Json(T& t, Q& q) {
    JSONOperator<T, Q>().From_Json(t, q);
}

template<typename T, typename Q>
void To_Json(T& t, Q& q) {
    JSONOperator<T, Q>().To_Json(t, q);
}




int main()
{
    json resArr = json::array({});
    resArr.push_back({{"left",1}, {"top", 1}}); 

    std::cout<<resArr<<std::endl; // [{"left":1,"top":1}]


    json js = {{"left", 2}, {"top", 2}};
    resArr.insert(resArr.end(), js);

    std::cout<<resArr<<std::endl; // [{"left":1,"top":1},{"left":2,"top":2}]


    // json obj = {{"selector", resArr}};
    json obj = json::object({{"selector", resArr}});

    std::cout<<obj<<std::endl;  // {"selector":[{"left":1,"top":1},{"left":2,"top":2}]}


    std::cout<<obj.dump()<<std::endl; // {"selector":[{"left":1,"top":1},{"left":2,"top":2}]}


    std::cout<<obj["selector"]<<std::endl; // [{"left":1,"top":1},{"left":2,"top":2}]

    
    // from json to type
    ReqSelectorPtr req = std::make_shared<ReqSelector>();

    From_Json<json, ReqSelectorPtr>(obj["selector"][0], req);

    std::cout<<"left:"<<req->left<<","<<"top:"<<req->top<<std::endl;// left:1,top:1


    // from type to json
    json resp;
    To_Json<json, ReqSelectorPtr>(resp, req);
    std::cout<<resp<<std::endl; // {"left":1,"top":1}


    

    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值