-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperson_example.cpp
More file actions
105 lines (87 loc) · 2.76 KB
/
person_example.cpp
File metadata and controls
105 lines (87 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2023 0CCH. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#ifdef LAZY_PARSING
#include "lazy_person.h"
#else
#include "person.h"
#endif
int main() {
// Create a person object using default values
person p1;
// Print default values
std::cout << "Default person:" << std::endl;
std::cout << "Name: " << p1.name() << std::endl;
std::cout << "Age: " << p1.age() << std::endl;
std::cout << "Salary: " << p1.salary() << std::endl;
std::cout << "Active: " << (p1.active() ? "Yes" : "No") << std::endl;
// Print skills
std::cout << "Skills: ";
for (const auto& skill : p1.skill()) {
std::cout << skill << " ";
}
std::cout << std::endl;
// Print scores
std::cout << "Math score: " << p1.scores().Math() << std::endl;
std::cout << "English score: " << p1.scores().English() << std::endl;
// Serialize to JSON
json j1 = p1.ToJson();
std::cout << "\nSerialized to JSON:\n" << j1.dump(2) << std::endl;
// Save to file
std::ofstream out_file("person_default.json");
out_file << j1.dump(2);
out_file.close();
// Create a new person with modified values
person p2;
p2.set_name("John Doe");
p2.set_age(30);
p2.set_salary(2500.75);
p2.set_active(false);
// Modify skills
std::vector<std::string> new_skills = {"C++", "Python", "JavaScript"};
p2.set_skill(new_skills);
// Modify scores
p2.scores().set_Math(98);
p2.scores().set_English(85);
// Serialize modified person to JSON
json j2 = p2.ToJson();
std::cout << "\nModified person serialized to JSON:\n"
<< j2.dump(2) << std::endl;
// Save to file
std::ofstream out_file2("person_modified.json");
out_file2 << j2.dump(2);
out_file2.close();
// Deserialize from JSON string
std::string json_str = R"({
"name": "Alice Smith",
"age": 28,
"salary": 3000.0,
"active": true,
"skill": ["Java", "SQL", "Management"],
"scores": {
"Math": 92,
"English": 96
}
})";
json j3 = json::parse(json_str);
person p3(j3); // Using constructor with JSON
// Print deserialized values
std::cout << "\nDeserialized person:" << std::endl;
std::cout << "Name: " << p3.name() << std::endl;
std::cout << "Age: " << p3.age() << std::endl;
std::cout << "Salary: " << p3.salary() << std::endl;
std::cout << "Active: " << (p3.active() ? "Yes" : "No") << std::endl;
// Print skills
std::cout << "Skills: ";
for (const auto& skill : p3.skill()) {
std::cout << skill << " ";
}
std::cout << std::endl;
// Print scores
std::cout << "Math score: " << p3.scores().Math() << std::endl;
std::cout << "English score: " << p3.scores().English() << std::endl;
return 0;
}