myMPD
Internal API documentation
Loading...
Searching...
No Matches
json_query.c File Reference

Jsonrpc implementation. More...

#include "compile_time.h"
#include "src/lib/json/json_query.h"
#include "dist/mongoose/mongoose.h"
#include "src/lib/convert.h"
#include "src/lib/log.h"
#include "src/lib/mem.h"
#include "src/lib/sds_extras.h"
#include "src/lib/sticker.h"
#include <string.h>
Include dependency graph for json_query.c:

Functions

static enum json_vtype get_vtype (char p)
 
static bool icb_json_get_field (const char *path, sds key, sds value, enum json_vtype vtype, validate_callback vcb, void *userdata, struct t_json_parse_error *error)
 
static bool json_get_string_unescape (sds s, const char *path, size_t min, size_t max, sds *result, validate_callback vcb, struct t_json_parse_error *error)
 
static void set_parse_error (struct t_json_parse_error *error, const char *path, const char *key, const char *fmt,...)
 
void json_parse_error_init (struct t_json_parse_error *parse_error)
 
void json_parse_error_clear (struct t_json_parse_error *parse_error)
 
bool json_get_fields_as_string (sds s, sds *fields, struct t_json_parse_error *error)
 
bool json_get_bool (sds s, const char *path, bool *result, struct t_json_parse_error *error)
 
bool json_get_int_max (sds s, const char *path, int *result, struct t_json_parse_error *error)
 
bool json_get_int (sds s, const char *path, int min, int max, int *result, struct t_json_parse_error *error)
 
bool json_get_time_max (sds s, const char *path, time_t *result, struct t_json_parse_error *error)
 
bool json_get_int64_max (sds s, const char *path, int64_t *result, struct t_json_parse_error *error)
 
bool json_get_int64 (sds s, const char *path, int64_t min, int64_t max, int64_t *result, struct t_json_parse_error *error)
 
bool json_get_uint_max (sds s, const char *path, unsigned *result, struct t_json_parse_error *error)
 
bool json_get_uint (sds s, const char *path, unsigned min, unsigned max, unsigned *result, struct t_json_parse_error *error)
 
bool json_get_string_max (sds s, const char *path, sds *result, validate_callback vcb, struct t_json_parse_error *error)
 
bool json_get_string_cmp (sds s, const char *path, size_t min, size_t max, const char *cmp, sds *result, struct t_json_parse_error *error)
 
bool json_get_string (sds s, const char *path, size_t min, size_t max, sds *result, validate_callback vcb, struct t_json_parse_error *error)
 
bool json_iterate_object (sds s, const char *path, iterate_callback icb, void *icb_userdata, validate_callback vcb_key, validate_callback vcb_value, int max_elements, struct t_json_parse_error *error)
 
static bool icb_json_get_array_string (const char *path, sds key, sds value, enum json_vtype vtype, validate_callback vcb, void *userdata, struct t_json_parse_error *error)
 
bool json_get_array_string (sds s, const char *path, struct t_list *l, validate_callback vcb, int max_elements, struct t_json_parse_error *error)
 
static bool icb_json_get_array_int64 (const char *path, sds key, sds value, enum json_vtype vtype, validate_callback vcb, void *userdata, struct t_json_parse_error *error)
 
bool json_get_array_int64 (sds s, const char *path, struct t_list *l, int max_elements, struct t_json_parse_error *error)
 
static bool icb_json_get_object_string (const char *path, sds key, sds value, enum json_vtype vtype, validate_callback vcb, void *userdata, struct t_json_parse_error *error)
 
bool json_get_object_string (sds s, const char *path, struct t_list *l, validate_callback vcb_key, validate_callback vcb_value, int max_elements, struct t_json_parse_error *error)
 
bool json_get_fields (sds s, const char *path, struct t_fields *tags, int max_elements, struct t_json_parse_error *error)
 
bool json_find_key (sds s, const char *path)
 
sds json_get_key_as_sds (sds s, const char *path)
 
const char * get_mjson_toktype_name (enum json_vtype vtype)
 

Detailed Description

Jsonrpc implementation.

Function Documentation

◆ get_mjson_toktype_name()

const char * get_mjson_toktype_name ( enum json_vtype  vtype)

Returns the name of a mjson token type

Parameters
vtypetoken type
Returns
token type as string

◆ get_vtype()

static enum json_vtype get_vtype ( char  p)
static

This unit provides functions for json parsing Json parsing is done by mjson private definitions

Detects the json value type by first char of value

Parameters
pFirst char of value
Returns
bool

◆ icb_json_get_array_int64()

static bool icb_json_get_array_int64 ( const char *  path,
sds  key,
sds  value,
enum json_vtype  vtype,
validate_callback  vcb,
void *  userdata,
struct t_json_parse_error error 
)
static

Iteration callback to populate a list with json array of int64_t

Parameters
pathjson path
keyjson key
valuejson value
vtypemjson value type
vcbvalidation callback - not used
userdatapointer to a t_list struct to populate
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ icb_json_get_array_string()

static bool icb_json_get_array_string ( const char *  path,
sds  key,
sds  value,
enum json_vtype  vtype,
validate_callback  vcb,
void *  userdata,
struct t_json_parse_error error 
)
static

Iteration callback to populate a list with json array of strings

Parameters
pathjson path
keyjson key
valuejson value
vtypemjson value type
vcbvalidation callback
userdatapointer to a t_list struct to populate
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ icb_json_get_field()

static bool icb_json_get_field ( const char *  path,
sds  key,
sds  value,
enum json_vtype  vtype,
validate_callback  vcb,
void *  userdata,
struct t_json_parse_error error 
)
static

Iteration callback to populate a t_fields struct

Parameters
pathjson path
keynot used
valuevalue to parse as mpd tag
vtypemjson value type
vcbnot used - we validate directly
userdatavoid pointer to t_fields struct
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ icb_json_get_object_string()

static bool icb_json_get_object_string ( const char *  path,
sds  key,
sds  value,
enum json_vtype  vtype,
validate_callback  vcb,
void *  userdata,
struct t_json_parse_error error 
)
static

Iteration callback to populate a list with json object key/values

Parameters
pathjson path
keyjson key
valuejson value
vtypemjson value type
vcbvalidation callback
userdatapointer to a t_list struct to populate
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_find_key()

bool json_find_key ( sds  s,
const char *  path 
)

Searches for a key in json object

Parameters
sjson object to search
pathmjson path expression
Returns
true on success, else false

◆ json_get_array_int64()

bool json_get_array_int64 ( sds  s,
const char *  path,
struct t_list l,
int  max_elements,
struct t_json_parse_error error 
)

Converts a json array of int64_t to a t_list struct Shortcut for json_iterate_object with icb_json_get_array_int64

Parameters
sjson object to parse
pathmjson path expression
lt_list struct to populate
max_elementsmaximum of elements
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_array_string()

bool json_get_array_string ( sds  s,
const char *  path,
struct t_list l,
validate_callback  vcb,
int  max_elements,
struct t_json_parse_error error 
)

Converts a json array of strings to a t_list struct Shortcut for json_iterate_object with icb_json_get_array_string

Parameters
sjson object to parse
pathmjson path expression
lt_list struct to populate
vcbvalidation callback
max_elementsmaximum of elements
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_bool()

bool json_get_bool ( sds  s,
const char *  path,
bool *  result,
struct t_json_parse_error error 
)

Gets a bool value by json path

Parameters
sjson object to parse
pathmjson path expression
resultpointer to bool with the result
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_fields()

bool json_get_fields ( sds  s,
const char *  path,
struct t_fields tags,
int  max_elements,
struct t_json_parse_error error 
)

Converts a json array to a struct t_tags Shortcut for json_iterate_object with icb_json_get_tag

Parameters
sjson object to parse
pathmjson path expression
tagst_tags struct to populate
max_elementsmaximum of elements
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_fields_as_string()

bool json_get_fields_as_string ( sds  s,
sds *  fields,
struct t_json_parse_error error 
)

Helper function to get myMPD fields out of a jsonrpc request and return a validated json array

Parameters
ssds string to parse
fieldssds string to append the fields
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_int()

bool json_get_int ( sds  s,
const char *  path,
int  min,
int  max,
int *  result,
struct t_json_parse_error error 
)

Gets a int value by json path

Parameters
sjson object to parse
pathmjson path expression
minminimum value (including)
maxmaximum value (including)
resultpointer to int with the result
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_int64()

bool json_get_int64 ( sds  s,
const char *  path,
int64_t  min,
int64_t  max,
int64_t *  result,
struct t_json_parse_error error 
)

Gets an int64_t value by json path

Parameters
sjson object to parse
pathmjson path expression
minminimum value (including)
maxmaximum value (including)
resultpointer to int64_t with the result
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_int64_max()

bool json_get_int64_max ( sds  s,
const char *  path,
int64_t *  result,
struct t_json_parse_error error 
)

Gets an int64_t value by json path

Parameters
sjson object to parse
pathmjson path expression
resultpointer to int64_t with the result
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_int_max()

bool json_get_int_max ( sds  s,
const char *  path,
int *  result,
struct t_json_parse_error error 
)

Gets a int value by json path

Parameters
sjson object to parse
pathmjson path expression
resultpointer to int with the result
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_key_as_sds()

sds json_get_key_as_sds ( sds  s,
const char *  path 
)

Searches for a key in json object and returns its value as sds string

Parameters
sjson object to search
pathmjson path expression
Returns
Key value as sds or NULL on error

◆ json_get_object_string()

bool json_get_object_string ( sds  s,
const char *  path,
struct t_list l,
validate_callback  vcb_key,
validate_callback  vcb_value,
int  max_elements,
struct t_json_parse_error error 
)

Converts a json object key/values to a t_list struct Shortcut for json_iterate_object with icb_json_get_object_string

Parameters
sjson object to parse
pathmjson path expression
lt_list struct to populate
vcb_keyvalidation callback for key
vcb_valuevalidation callback for value
max_elementsmaximum of elements
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_string()

bool json_get_string ( sds  s,
const char *  path,
size_t  min,
size_t  max,
sds *  result,
validate_callback  vcb,
struct t_json_parse_error error 
)

Gets a string value by json path

Parameters
sjson object to parse
pathmjson path expression
resultpointer to int with the result
minminimum length (inclusive)
maxmaximum length (inclusive)
vcbvalidation callback
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_string_cmp()

bool json_get_string_cmp ( sds  s,
const char *  path,
size_t  min,
size_t  max,
const char *  cmp,
sds *  result,
struct t_json_parse_error error 
)

Gets a string value by json path

Parameters
sjson object to parse
pathmjson path expression
resultpointer to sds with the result
minminimum length (inclusive)
maxmaximum length (inclusive)
cmpcompare result against this string
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_string_max()

bool json_get_string_max ( sds  s,
const char *  path,
sds *  result,
validate_callback  vcb,
struct t_json_parse_error error 
)

Gets a string value by json path

Parameters
sjson object to parse
pathmjson path expression
resultpointer to sds with the result
vcbvalidation callback
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_string_unescape()

static bool json_get_string_unescape ( sds  s,
const char *  path,
size_t  min,
size_t  max,
sds *  result,
validate_callback  vcb,
struct t_json_parse_error error 
)
static

Helper function to get a string from a json object Enclosing quotes are removed and string is unescaped

Parameters
sjson object to parse
pathpath to the string to extract
minminimum length
maxmaximum length
resultnewly allocated sds string with the result
vcbvalidation callback
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_time_max()

bool json_get_time_max ( sds  s,
const char *  path,
time_t *  result,
struct t_json_parse_error error 
)

Gets a time_t value by json path

Parameters
sjson object to parse
pathmjson path expression
resultpointer to time_t with the result
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_uint()

bool json_get_uint ( sds  s,
const char *  path,
unsigned  min,
unsigned  max,
unsigned *  result,
struct t_json_parse_error error 
)

Gets a unsigned int value by json path

Parameters
sjson object to parse
pathmjson path expression
minminimum value (including)
maxmaximum value (including)
resultpointer to unsigned int with the result
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_get_uint_max()

bool json_get_uint_max ( sds  s,
const char *  path,
unsigned *  result,
struct t_json_parse_error error 
)

Gets a unsigned int value by json path

Parameters
sjson object to parse
pathmjson path expression
resultpointer to unsigned int with the result
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_iterate_object()

bool json_iterate_object ( sds  s,
const char *  path,
iterate_callback  icb,
void *  icb_userdata,
validate_callback  vcb_key,
validate_callback  vcb_value,
int  max_elements,
struct t_json_parse_error error 
)

Iterates through object/array found by path

Parameters
sjson object to parse
pathmjson path expression
icbiteration callback
icb_userdatacustom data for iteration callback
vcb_keyvalidation callback for the key
vcb_valuevalidation callback for the value
max_elementsmaximum of elements
errorpointer to t_json_parse_error
Returns
true on success, else false

◆ json_parse_error_clear()

void json_parse_error_clear ( struct t_json_parse_error parse_error)

Clears the sds strings

Parameters
parse_errort_json_parse_error struct to clear

◆ json_parse_error_init()

void json_parse_error_init ( struct t_json_parse_error parse_error)

public functions Initializes the sds strings t_json_parse_error struct to NULL

Parameters
parse_errort_json_parse_error struct to initialize

◆ set_parse_error()

static void set_parse_error ( struct t_json_parse_error error,
const char *  path,
const char *  key,
const char *  fmt,
  ... 
)
static

Helper function to set parsing errors

Parameters
erroralready initialized t_json_parse_error struct to propagate the error or NULL for log only
pathjson path where the error occurred
keyoptional json key inside the path where the error occurred set this to a blank string for none object paths
fmtprintf format string
...arguments for the format string