1 #include "lib/spotify/album.hpp"
2
to_json(nlohmann::json & j,const album & a)3 void lib::spt::to_json(nlohmann::json &j, const album &a)
4 {
5 j = nlohmann::json{
6 {"id", a.id},
7 {"image", a.image},
8 {"name", a.name},
9 {"artist", a.artist},
10 {"release_date", a.release_date},
11 };
12
13 if (a.album_group != lib::album_group::none)
14 {
15 std::string album_group;
16
17 switch (a.album_group)
18 {
19 case album_group::album:
20 album_group = "album";
21 break;
22
23 case album_group::single:
24 album_group = "single";
25 break;
26
27 case album_group::compilation:
28 album_group = "compilation";
29 break;
30
31 case album_group::appears_on:
32 album_group = "appears_on";
33 break;
34
35 case album_group::none:
36 album_group = "none";
37 break;
38 }
39
40 j.at("album_group") = album_group;
41 }
42 }
43
from_json(const nlohmann::json & j,album & a)44 void lib::spt::from_json(const nlohmann::json &j, album &a)
45 {
46 if (!j.is_object())
47 return;
48
49 j.at("id").get_to(a.id);
50 j.at("name").get_to(a.name);
51 j.at("release_date").get_to(a.release_date);
52
53 if (j.contains("album_group"))
54 {
55 auto album_group = j.at("album_group").get<std::string>();
56 a.album_group = album_group == "album"
57 ? lib::album_group::album
58 : album_group == "single"
59 ? lib::album_group::single
60 : album_group == "compilation"
61 ? lib::album_group::compilation
62 : album_group == "appears_on"
63 ? lib::album_group::appears_on
64 : lib::album_group::none;
65 }
66
67 if (j.contains("images"))
68 {
69 const auto &images = j.at("images");
70 if (images.is_array() && !images.empty())
71 {
72 images.back().at("url").get_to(a.image);
73 }
74 }
75 else if (j.contains("image"))
76 {
77 j.at("image").get_to(a.image);
78 }
79
80 if (j.contains("artists"))
81 j.at("artists").front().at("name").get_to(a.artist);
82 else if (j.contains("artist"))
83 j.at("artist").get_to(a.artist);
84 }