nlohmann json,与嵌套结构相互转换

Jim*_*her 8 c++ json c++11 nlohmann-json

我在嵌入式项目中使用nlohmann json(现代 C++ 的 JSON) 。操作系统是Mongoose操作系统。Mongoose 有一个很好的配置系统,其中配置数据被处理并布置在 mos.yml 文件中。该文件在构建时被转换为结构和访问器函数。因此,我可以将配置数据作为结构获取,其中包含其他嵌套结构。我需要将其转换为 JSON。

我的理解是 nlohmann::json 能够将 JSON 与我自己的类型相互转换,我所要做的就是提供to_json()from_json()方法,如下所述:

nlohmann json 文档(类型转换示例)

这个示例代码非常简单:

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}};
}
Run Code Online (Sandbox Code Playgroud)

但给出的例子非常简单。我的类型更复杂,我无法弄清楚更复杂结构的语法,例如这个(为简洁起见摘录):

struct mgos_config_mytype {
  struct mgos_config_mytype_input input;
  struct mgos_config_mytype_speed speed;

  /* many others omitted */

};

struct mgos_config_mytype_input {
  struct mgos_config_mytype_input_wired_buttons wired_buttons;
};

struct mgos_config_mytype_input_wired_buttons {
  const char * btn1;
  const char * btn2;
  const char * btn3;
};
Run Code Online (Sandbox Code Playgroud)

如果有人可以向我展示它是如何完成的或指出正确的方向,我将不胜感激,谢谢。

Aze*_*eem 14

to_json下面是一个为合并类型Person( live ) 定义的嵌套类型的示例:

#include <iostream>
#include <string>

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

struct Name
{
    std::string first;
    std::string last;
};

struct Address
{
    std::string houseNo;
    std::string street;
    std::string city;
    std::string postalCode;
    std::string country;
};

struct Person
{
    Name    name;
    Address address;
    int     age;
};

void to_json(json& j, const Person& p)
{
    j = json{
        { "name", {
            { "first", p.name.first },
            { "last", p.name.last }
            } 
        },
        { "address", {
            { "house", p.address.houseNo },
            { "street", p.address.street },
            { "city", p.address.city },
            { "postal_code", p.address.postalCode },
            { "country", p.address.country }
            }
        },
        { "age", p.age}
    };
}

int main()
{
    const Person p { 
        { "firstname", "lastname" }, 
        { "123", "St. ABC", "XYZ", "123456", "country" }, 
        18
    };

    json j { p };
    std::cout << j.dump(4) << '\n';

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

[
    {
        "address": {
            "city": "XYZ",
            "country": "country",
            "house": "123",
            "postal_code": "123456",
            "street": "St. ABC"
        },
        "age": 18,
        "name": {
            "first": "firstname",
            "last": "lastname"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

根据嵌套类型的复杂性和可重用性,您可以为所有类型定义to_json和。from_json

以下是 Person-to-JSON 和 JSON-to-Person 示例(实时):

[
    {
        "address": {
            "city": "XYZ",
            "country": "country",
            "house": "123",
            "postal_code": "123456",
            "street": "St. ABC"
        },
        "age": 18,
        "name": {
            "first": "firstname",
            "last": "lastname"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

输出:

[
    {
        "address": {
            "city": "XYZ",
            "country": "country",
            "house": "123",
            "postalCode": "123456",
            "street": "St. ABC"
        },
        "age": 18,
        "name": {
            "first": "firstname",
            "last": "lastname"
        }
    }
]
[
    {
        "address": {
            "city": "XYZ",
            "country": "country",
            "house": "123",
            "postalCode": "123456",
            "street": "St. ABC"
        },
        "age": 18,
        "name": {
            "first": "ABC",
            "last": "XYZ"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)