-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJson.cpp
More file actions
80 lines (69 loc) · 1.37 KB
/
Copy pathJson.cpp
File metadata and controls
80 lines (69 loc) · 1.37 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
#include "json.hpp"
#include <string>
#include <string_view>
#include <iostream>
#include "movies.h"
#include <ostream>
#include <fstream>
using namespace std;
using json = nlohmann::json;
void to_json(json& j, casting_role const& c)
{
j = json{ {"star",c.actor}, {"name", c.role} };
}
void to_json(json& j, movie const& m)
{
j = json::object({
{"id", m.id},
{"title", m.title},
{"year",m.year},
{"length", m.length},
{"cast", m.cast},
{"directors", m.directors},
{"writers", m.writers}
});
}
void serialize_json(movie_list const& movies, string_view filepath)
{
json jdata{ {"movies", movies} };
std::ofstream ofile(filepath.data());
if (ofile.is_open())
{
ofile << std::setw(2) << jdata << std::endl;
}
}
int main()
{
movie_list movies
{
{
11001,
"The Matrix",
1999,
196,
{
{"Keanu Reeves", "Neo"},
{"Laurence Fishburne", "Morpheus"},
{"Carrie-Anne Moss", "Trinity"},
{"Hugo Weaving", "Agent Smith"}
},
{"Lana Wachowski", "Lilly Wachowski"},
{"Lana Wachowski", "Lilly Wachowski"},
},
{
9871,
"Forrest Gump",
1994,
202,
{
{"Tom Hanks", "Forrest Gump"},
{"Sally Field", "Mrs. Gump"},
{"Robin Wright","Jenny Curran"},
{"Mykelti Williamson","Bubba Blue"}
},
{"Robert Zemeckis"},
{"Winston Groom", "Eric Roth"},
}
};
serialize_json(movies, "test.json");
}