json/example/main.cpp

55 lines
1.9 KiB
C++
Raw Normal View History

#include <iostream>
#include <map>
#include <vector>
2023-07-21 23:22:47 +03:00
#include <fstream>
#include <nlohmann/json.hpp>
class ExampleClass {
private:
int property1{1};
double property2{2.5};
std::string property3{"test"};
std::map<std::string, int> property4{{"x", 1}, {"y", 2}};
std::vector<double> property5{1.5, 5.4, 3.2};
public:
ExampleClass() = default;
2023-07-21 23:22:47 +03:00
NLOHMANN_DEFINE_TYPE_INTRUSIVE_ANNOTATED(ExampleClass, property1, "comment两两",
2023-07-20 12:44:44 +03:00
property2, "multiline\ncomment2",
2023-07-21 23:22:47 +03:00
property3, "comment3",
property4, "comment4",
2023-07-20 12:44:44 +03:00
property5, "comment5");
};
2023-07-20 01:19:21 +03:00
class AnotherExampleClass {
private:
int property1{1};
double property2{2.5};
std::string property3{"test"};
std::map<std::string, int> property4{{"x", 1}, {"y", 2}};
std::vector<double> property5{1.5, 5.4, 3.2};
public:
AnotherExampleClass() = default;
2023-07-21 23:22:47 +03:00
NLOHMANN_DEFINE_TYPE_INTRUSIVE_ANNOTATED(AnotherExampleClass, property1, "comment两",
2023-07-20 12:44:44 +03:00
property2, "multiline\ncomment22",
2023-07-21 23:22:47 +03:00
property3, "comment33",
property4, "comment44",
property5, "comment55");
2023-07-20 01:19:21 +03:00
};
int main() {
std::cout << "Hello, world!" << std::endl;
ExampleClass ec;
2023-07-20 01:19:21 +03:00
AnotherExampleClass aec;
2023-07-21 23:22:47 +03:00
std::ofstream example_file;
example_file.open("/Users/marekpiotrowski/private/json/example.json");
nlohmann::json j = ec;
2023-07-21 23:22:47 +03:00
example_file << j.dump_annotated<ExampleClass>(4, ' ', true) << std::endl;
2023-07-20 01:19:21 +03:00
2023-07-20 12:44:44 +03:00
// nlohmann::json j2 = aec;
// std::cout << j2.dump_annotated<AnotherExampleClass>() << std::endl;
return 0;
}