mirror of https://github.com/mpv-player/mpv
lua: add utils.format_json() function
Requested. Why not.
This commit is contained in:
parent
589533d97a
commit
daabbe3640
|
@ -639,6 +639,14 @@ strictly part of the guaranteed API.
|
|||
trailing text is returned as 3rd return value. (The 3rd return value is
|
||||
always there, but with ``trail`` set, no error is raised.)
|
||||
|
||||
``utils.format_json(v)``
|
||||
Format the given Lua table (or value) as a JSON string and return it. On
|
||||
error, returns ``nil, error``. (Errors usually only happen on value types
|
||||
incompatible with JSON.)
|
||||
|
||||
The argument value uses similar conventions as ``mp.set_property_native()``
|
||||
to distinguish empty objects and arrays.
|
||||
|
||||
``utils.to_string(v)``
|
||||
Turn the given value into a string. Formats tables and their contents. This
|
||||
doesn't do anything special; it is only needed because Lua is terrible.
|
||||
|
|
18
player/lua.c
18
player/lua.c
|
@ -1255,6 +1255,23 @@ static int script_parse_json(lua_State *L)
|
|||
return 3;
|
||||
}
|
||||
|
||||
static int script_format_json(lua_State *L)
|
||||
{
|
||||
void *tmp = mp_lua_PITA(L);
|
||||
struct mpv_node node;
|
||||
makenode(tmp, &node, L, 1);
|
||||
char *dst = talloc_strdup(tmp, "");
|
||||
if (json_write(&dst, &node) >= 0) {
|
||||
lua_pushstring(L, dst);
|
||||
lua_pushnil(L);
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "error");
|
||||
}
|
||||
talloc_free_children(tmp);
|
||||
return 2;
|
||||
}
|
||||
|
||||
#define FN_ENTRY(name) {#name, script_ ## name}
|
||||
struct fn_entry {
|
||||
const char *name;
|
||||
|
@ -1303,6 +1320,7 @@ static const struct fn_entry utils_fns[] = {
|
|||
FN_ENTRY(join_path),
|
||||
FN_ENTRY(subprocess),
|
||||
FN_ENTRY(parse_json),
|
||||
FN_ENTRY(format_json),
|
||||
{0}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue