diff src/deezer.c @ 12:dd427b7cc459 default tip

json: replace with nxjson library more lightweight, reduces the binary size by about 40 kb
author Paper <paper@paper.us.eu.org>
date Fri, 15 Mar 2024 20:46:18 -0400
parents e6a594f16403
children
line wrap: on
line diff
--- a/src/deezer.c	Thu Mar 14 20:25:37 2024 -0400
+++ b/src/deezer.c	Fri Mar 15 20:46:18 2024 -0400
@@ -69,19 +69,21 @@
 	return final;
 }
 
-static int deezer_album_object_get_cover(cJSON* restrict album, const char* restrict name, char** restrict url) {
+static int deezer_album_object_get_cover(const nx_json* album, const char* name, char** url) {
 	if (!url) return 0;
 
-	cJSON* cover = cJSON_GetObjectItemCaseSensitive(album, name);
-	if (cover) {
-		if (*url)
-			free(*url);
-		size_t len = strlen(cover->valuestring);
-		*url = malloc((len + 1) * sizeof(char));
-		(*url)[len] = '\0';
-		strncpy(*url, cover->valuestring, len);
-		return 1;
-	} else return 0;
+	const nx_json* cover = nx_json_get(album, name);
+	if (!cover)
+		return 0;
+
+	free(*url);
+
+	size_t len = strlen(cover->text_value);
+	*url = malloc((len + 1) * sizeof(char));
+	(*url)[len] = '\0';
+
+	strncpy(*url, cover->text_value, len);
+	return 1;
 }
 
 
@@ -146,24 +148,27 @@
 	return 0;
 }
 
+/* note: please prefix these */
 enum deezer_thumbnail_state {
 	ERROR = 0,
 	ALBUM,
 	ARTIST,
 };
 
-static enum deezer_thumbnail_state deezer_get_thumbnail_parse_search_result(cJSON* json, char** restrict cover_url, int get_artist) {
-	cJSON* album = cJSON_GetObjectItemCaseSensitive(json, "album");
+static enum deezer_thumbnail_state deezer_get_thumbnail_parse_search_result(const nx_json* json, char** cover_url, int get_artist) {
+	const nx_json* album = nx_json_get(json, "album");
 
-	if (cJSON_IsObject(album)) {
+	if (album->type == NX_JSON_OBJECT) {
 		if (deezer_album_object_get_cover(album, "cover_medium", cover_url)
 			|| deezer_album_object_get_cover(album, "cover_large", cover_url)
 			|| deezer_album_object_get_cover(album, "cover_small", cover_url)
 			|| deezer_album_object_get_cover(album, "cover_xl", cover_url))
 			return ALBUM;
-	} else if (get_artist) {
-		cJSON* artist = cJSON_GetObjectItemCaseSensitive(json, "artist");
-		if (!cJSON_IsObject(artist))
+	}
+
+	if (get_artist) {
+		const nx_json* artist = nx_json_get(json, "artist");
+		if (artist->type != NX_JSON_OBJECT)
 			return ERROR;
 
 		/* treat artist pictures as a fallback and only retrieve them once */
@@ -177,25 +182,20 @@
 	return ERROR;
 }
 
-static int deezer_get_thumbnail_parse_json(char** restrict cover_url, const char* restrict data, size_t size) {
-	cJSON* json = cJSON_ParseWithLength(data, size);
-	if (!json) {
-		const char* err_ptr = cJSON_GetErrorPtr();
-		if (err_ptr)
-			MessageBoxA(NULL, err_ptr, "wgsdk: Error parsing Deezer JSON!", MB_ICONERROR | MB_OK);
-		cJSON_Delete(json);
+/* THIS MODIFIES `data` IN PLACE!! */
+static int deezer_get_thumbnail_parse_json(char** cover_url, char* data) {
+	const nx_json* json = nx_json_parse(data, NULL);
+	if (!json)
 		return -1;
-	}
 
-	cJSON* json_data = cJSON_GetObjectItemCaseSensitive(json, "data"); 
-	size_t json_data_size;
-	if (!cJSON_IsArray(json_data) || !(json_data_size = cJSON_GetArraySize(json_data)))
+	const nx_json* json_data = nx_json_get(json, "data"); 
+	if (json_data->type != NX_JSON_ARRAY || !json_data->children.length)
 		return -1;
 
 	int have_artist = 0;
-	for (size_t i = 0; i < json_data_size; i++) {
-		cJSON* result = cJSON_GetArrayItem(json_data, i);
-		if (!cJSON_IsObject(result))
+	for (size_t i = 0; i < json_data->children.length; i++) {
+		const nx_json* result = nx_json_item(json_data, i);
+		if (result->type != NX_JSON_OBJECT)
 			return -1;
 
 		enum deezer_thumbnail_state state = deezer_get_thumbnail_parse_search_result(result, cover_url, have_artist);
@@ -206,7 +206,8 @@
 		}
 	}
 
-	cJSON_Delete(json);
+	nx_json_free(json);
+
 	return !have_artist;
 }
 
@@ -227,8 +228,10 @@
 	if (deezer_get_thumbnail_download_url(query, &response_data, &response_size))
 		return NULL;
 
-	if (deezer_get_thumbnail_parse_json(&cover_url, response_data, response_size))
+	if (deezer_get_thumbnail_parse_json(&cover_url, response_data))
 		return NULL;
 
+	free(response_data);
+
 	return cover_url;
 }