mirror of
https://github.com/owntone/owntone-server.git
synced 2025-01-15 08:45:02 -05:00
[misc/spotify] Move json helper functions into its own file
This commit is contained in:
parent
a832e40bea
commit
89fc1fa5b8
@ -113,6 +113,7 @@ forked_daapd_SOURCES = main.c \
|
||||
dmap_common.c dmap_common.h \
|
||||
$(FFMPEG_SRC) \
|
||||
misc.c misc.h \
|
||||
misc_json.c misc_json.h \
|
||||
rng.c rng.h \
|
||||
rsp_query.c rsp_query.h \
|
||||
daap_query.c daap_query.h \
|
||||
|
125
src/misc_json.c
Normal file
125
src/misc_json.c
Normal file
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Christian Meffert <christian.meffert@googlemail.com>
|
||||
*
|
||||
* Some code included below is in the public domain, check comments
|
||||
* in the file.
|
||||
*
|
||||
* Pieces of code adapted from mt-daapd:
|
||||
* Copyright (C) 2003-2007 Ron Pedde (ron@pedde.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <json.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
void
|
||||
jparse_free(json_object* haystack)
|
||||
{
|
||||
if (haystack)
|
||||
{
|
||||
#ifdef HAVE_JSON_C_OLD
|
||||
json_object_put(haystack);
|
||||
#else
|
||||
if (json_object_put(haystack) != 1)
|
||||
DPRINTF(E_LOG, L_MISC, "Memleak: JSON parser did not free object\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
jparse_array_from_obj(json_object *haystack, const char *key, json_object **needle)
|
||||
{
|
||||
if (! (json_object_object_get_ex(haystack, key, needle) && json_object_get_type(*needle) == json_type_array) )
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *
|
||||
jparse_str_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
json_object *needle;
|
||||
|
||||
if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_string)
|
||||
return json_object_get_string(needle);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
jparse_int_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
json_object *needle;
|
||||
|
||||
if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_int)
|
||||
return json_object_get_int(needle);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
jparse_bool_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
json_object *needle;
|
||||
|
||||
if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_boolean)
|
||||
return json_object_get_boolean(needle);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
time_t
|
||||
jparse_time_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
const char *tmp;
|
||||
struct tm tp;
|
||||
time_t parsed_time;
|
||||
|
||||
memset(&tp, 0, sizeof(struct tm));
|
||||
|
||||
tmp = jparse_str_from_obj(haystack, key);
|
||||
if (!tmp)
|
||||
return 0;
|
||||
|
||||
strptime(tmp, "%Y-%m-%dT%H:%M:%SZ", &tp);
|
||||
parsed_time = mktime(&tp);
|
||||
if (parsed_time < 0)
|
||||
return 0;
|
||||
|
||||
return parsed_time;
|
||||
}
|
||||
|
||||
const char *
|
||||
jparse_str_from_array(json_object *array, int index, const char *key)
|
||||
{
|
||||
json_object *item;
|
||||
int count;
|
||||
|
||||
if (json_object_get_type(array) != json_type_array)
|
||||
return NULL;
|
||||
|
||||
count = json_object_array_length(array);
|
||||
if (count <= 0 || count <= index)
|
||||
return NULL;
|
||||
|
||||
item = json_object_array_get_idx(array, index);
|
||||
return jparse_str_from_obj(item, key);
|
||||
}
|
57
src/misc_json.h
Normal file
57
src/misc_json.h
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Christian Meffert <christian.meffert@googlemail.com>
|
||||
*
|
||||
* Some code included below is in the public domain, check comments
|
||||
* in the file.
|
||||
*
|
||||
* Pieces of code adapted from mt-daapd:
|
||||
* Copyright (C) 2003-2007 Ron Pedde (ron@pedde.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SRC_MISC_JSON_H_
|
||||
#define SRC_MISC_JSON_H_
|
||||
|
||||
|
||||
#include <json.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <time.h>
|
||||
|
||||
void
|
||||
jparse_free(json_object *haystack);
|
||||
|
||||
int
|
||||
jparse_array_from_obj(json_object *haystack, const char *key, json_object **needle);
|
||||
|
||||
const char *
|
||||
jparse_str_from_obj(json_object *haystack, const char *key);
|
||||
|
||||
int
|
||||
jparse_int_from_obj(json_object *haystack, const char *key);
|
||||
|
||||
int
|
||||
jparse_bool_from_obj(json_object *haystack, const char *key);
|
||||
|
||||
time_t
|
||||
jparse_time_from_obj(json_object *haystack, const char *key);
|
||||
|
||||
const char *
|
||||
jparse_str_from_array(json_object *array, int index, const char *key);
|
||||
|
||||
|
||||
#endif /* SRC_MISC_JSON_H_ */
|
@ -30,6 +30,7 @@
|
||||
#include "http.h"
|
||||
#include "library.h"
|
||||
#include "logger.h"
|
||||
#include "misc_json.h"
|
||||
|
||||
|
||||
|
||||
@ -54,101 +55,6 @@ static const char *spotify_me_uri = "https://api.spotify.com/v1/me";
|
||||
/*--------------------- HELPERS FOR SPOTIFY WEB API -------------------------*/
|
||||
/* All the below is in the httpd thread */
|
||||
|
||||
|
||||
static void
|
||||
jparse_free(json_object* haystack)
|
||||
{
|
||||
if (haystack)
|
||||
{
|
||||
#ifdef HAVE_JSON_C_OLD
|
||||
json_object_put(haystack);
|
||||
#else
|
||||
if (json_object_put(haystack) != 1)
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Memleak: JSON parser did not free object\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
jparse_array_from_obj(json_object *haystack, const char *key, json_object **needle)
|
||||
{
|
||||
if (! (json_object_object_get_ex(haystack, key, needle) && json_object_get_type(*needle) == json_type_array) )
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char *
|
||||
jparse_str_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
json_object *needle;
|
||||
|
||||
if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_string)
|
||||
return json_object_get_string(needle);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
jparse_int_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
json_object *needle;
|
||||
|
||||
if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_int)
|
||||
return json_object_get_int(needle);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
jparse_bool_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
json_object *needle;
|
||||
|
||||
if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_boolean)
|
||||
return json_object_get_boolean(needle);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
static time_t
|
||||
jparse_time_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
const char *tmp;
|
||||
struct tm tp;
|
||||
time_t parsed_time;
|
||||
|
||||
memset(&tp, 0, sizeof(struct tm));
|
||||
|
||||
tmp = jparse_str_from_obj(haystack, key);
|
||||
if (!tmp)
|
||||
return 0;
|
||||
|
||||
strptime(tmp, "%Y-%m-%dT%H:%M:%SZ", &tp);
|
||||
parsed_time = mktime(&tp);
|
||||
if (parsed_time < 0)
|
||||
return 0;
|
||||
|
||||
return parsed_time;
|
||||
}
|
||||
|
||||
static const char *
|
||||
jparse_str_from_array(json_object *array, int index, const char *key)
|
||||
{
|
||||
json_object *item;
|
||||
int count;
|
||||
|
||||
if (json_object_get_type(array) != json_type_array)
|
||||
return NULL;
|
||||
|
||||
count = json_object_array_length(array);
|
||||
if (count <= 0 || count <= index)
|
||||
return NULL;
|
||||
|
||||
item = json_object_array_get_idx(array, index);
|
||||
return jparse_str_from_obj(item, key);
|
||||
}
|
||||
|
||||
static void
|
||||
free_http_client_ctx(struct http_client_ctx *ctx)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user