-
Notifications
You must be signed in to change notification settings - Fork 23
/
json.c
108 lines (83 loc) · 3.03 KB
/
json.c
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
106
107
108
#include <assert.h>
#include <jansson.h>
#include <libspotify/api.h>
#include <string.h>
#include <stdbool.h>
#include "constants.h"
json_t *playlist_to_json_set_collaborative(sp_playlist *playlist,
json_t *object) {
bool collaborative = sp_playlist_is_collaborative(playlist);
json_object_set_new(object, "collaborative",
collaborative ? json_true() : json_false());
return object;
}
json_t *playlist_to_json(sp_playlist *playlist, json_t *object) {
assert(sp_playlist_is_loaded(playlist));
// Owner
sp_user *owner = sp_playlist_owner(playlist);
const char *username = sp_user_display_name(owner);
sp_user_release(owner);
json_object_set_new_nocheck(object, "creator",
json_string_nocheck(username));
// URI
char playlist_uri[kPlaylistLinkLength];
sp_link *playlist_link = sp_link_create_from_playlist(playlist);
if (playlist_link == NULL) // Shouldn't happen; playlist is loaded (?)
return object;
sp_link_as_string(playlist_link, playlist_uri, kPlaylistLinkLength);
sp_link_release(playlist_link);
json_object_set_new(object, "uri",
json_string_nocheck(playlist_uri));
// Title
const char *title = sp_playlist_name(playlist);
json_object_set_new(object, "title",
json_string_nocheck(title));
// Collaborative
playlist_to_json_set_collaborative(playlist, object);
// Description
const char *description = sp_playlist_get_description(playlist);
if (description != NULL) {
json_object_set_new(object, "description",
json_string_nocheck(description));
}
// Number of subscribers
int num_subscribers = sp_playlist_num_subscribers(playlist);
json_object_set_new(object, "subscriberCount",
json_integer(num_subscribers));
// Tracks
json_t *tracks = json_array();
json_object_set_new(object, "tracks", tracks);
char track_uri[kTrackLinkLength];
for (int i = 0; i < sp_playlist_num_tracks(playlist); i++) {
sp_track *track = sp_playlist_track(playlist, i);
sp_link *track_link = sp_link_create_from_track(track, 0);
sp_link_as_string(track_link, track_uri, kTrackLinkLength);
json_array_append_new(tracks, json_string_nocheck(track_uri));
sp_link_release(track_link);
}
return object;
}
bool json_to_track(json_t *json, sp_track **track) {
if (!json_is_string(json))
return false;
sp_link *link = sp_link_create_from_string(json_string_value(json));
if (link == NULL)
return false;
if (sp_link_type(link) != SP_LINKTYPE_TRACK) {
sp_link_release(link);
return false;
}
*track = sp_link_as_track(link);
return *track != NULL;
}
int json_to_tracks(json_t *json, sp_track **tracks, int num_tracks) {
if (!json_is_array(json))
return 0;
int num_valid_tracks = 0;
for (int i = 0; i < num_tracks; i++) {
json_t *item = json_array_get(json, i);
if (json_to_track(item, &tracks[num_valid_tracks]))
num_valid_tracks++;
}
return num_valid_tracks;
}