spa: move json parser to utils

Add unit test
Add escape function
This commit is contained in:
Wim Taymans 2020-11-28 15:34:01 +01:00
parent 77791bd427
commit 62cdec8448
8 changed files with 211 additions and 5 deletions

View file

@ -100,6 +100,7 @@ spa_utils_headers = [
'utils/defs.h',
'utils/dict.h',
'utils/hook.h',
'utils/json.h',
'utils/keys.h',
'utils/list.h',
'utils/names.h',

View file

@ -0,0 +1,331 @@
/* Simple Plugin API
*
* Copyright © 2020 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef SPA_UTILS_JSON_H
#define SPA_UTILS_JSON_H
#ifdef __cplusplus
extern "C" {
#else
#include <stdbool.h>
#endif
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
/* a simple JSON compatible tokenizer */
struct spa_json {
const char *cur;
const char *end;
struct spa_json *parent;
uint32_t state;
uint32_t depth;
};
#define SPA_JSON_INIT(data,size) (struct spa_json) { (data), (data)+(size), }
static inline void spa_json_init(struct spa_json * iter, const char *data, size_t size)
{
*iter = SPA_JSON_INIT(data, size);
}
#define SPA_JSON_ENTER(iter) (struct spa_json) { (iter)->cur, (iter)->end, (iter), }
static inline void spa_json_enter(struct spa_json * iter, struct spa_json * sub)
{
*sub = SPA_JSON_ENTER(iter);
}
/** Get the next token. \a value points to the token and the return value
* is the length. */
static inline int spa_json_next(struct spa_json * iter, const char **value)
{
int utf8_remain = 0;
enum { __NONE, __STRUCT, __BARE, __STRING, __UTF8, __ESC };
for (; iter->cur < iter->end; iter->cur++) {
unsigned char cur = (unsigned char)*iter->cur;
again:
switch (iter->state) {
case __NONE:
iter->state = __STRUCT;
iter->depth = 0;
goto again;
case __STRUCT:
switch (cur) {
case '\t': case ' ': case '\r': case '\n': case ':': case ',':
continue;
case '"':
*value = iter->cur;
iter->state = __STRING;
continue;
case '[': case '{':
*value = iter->cur;
if (++iter->depth > 1)
continue;
iter->cur++;
return 1;
case '}': case ']':
if (iter->depth == 0) {
if (iter->parent)
iter->parent->cur = iter->cur;
return 0;
}
--iter->depth;
continue;
case '-': case '+': case 'a' ... 'z': case 'A' ... 'Z': case '0' ... '9':
*value = iter->cur;
iter->state = __BARE;
continue;
}
return -1;
case __BARE:
switch (cur) {
case '\t': case ' ': case '\r': case '\n':
case ':': case ',': case ']': case '}':
iter->state = __STRUCT;
if (iter->depth > 0)
goto again;
return iter->cur - *value;
default:
if (cur >= 32 && cur <= 126)
continue;
}
return -1;
case __STRING:
switch (cur) {
case '\\':
iter->state = __ESC;
continue;
case '"':
iter->state = __STRUCT;
if (iter->depth > 0)
continue;
iter->cur++;
return iter->cur - *value;
case 240 ... 247:
utf8_remain++;
SPA_FALLTHROUGH;
case 224 ... 239:
utf8_remain++;
SPA_FALLTHROUGH;
case 192 ... 223:
utf8_remain++;
iter->state = __UTF8;
continue;
default:
if (cur >= 32 && cur <= 126)
continue;
}
return -1;
case __UTF8:
switch (cur) {
case 128 ... 191:
if (--utf8_remain == 0)
iter->state = __STRING;
continue;
}
return -1;
case __ESC:
switch (cur) {
case '"': case '\\': case '/': case 'b': case 'f':
case 'n': case 'r': case 't': case 'u':
iter->state = __STRING;
continue;
}
return -1;
}
}
return (iter->depth == 0 ? (iter->state == __BARE ? iter->cur - *value : 0) : -1);
}
static inline int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type)
{
const char *value;
if (spa_json_next(iter, &value) < 0 || *value != type)
return -1;
spa_json_enter(iter, sub);
return 1;
}
/* object */
static inline int spa_json_is_object(const char *val, int len)
{
return len > 0 && *val == '{';
}
static inline int spa_json_enter_object(struct spa_json *iter, struct spa_json *sub)
{
return spa_json_enter_container(iter, sub, '{');
}
/* array */
static inline bool spa_json_is_array(const char *val, int len)
{
return len > 0 && *val == '[';
}
static inline int spa_json_enter_array(struct spa_json *iter, struct spa_json *sub)
{
return spa_json_enter_container(iter, sub, '[');
}
/* null */
static inline bool spa_json_is_null(const char *val, int len)
{
return len == 4 && strncmp(val, "null", 4) == 0;
}
/* float */
static inline int spa_json_parse_float(const char *val, int len, float *result)
{
char *end;
*result = strtof(val, &end);
return end == val + len;
}
static inline bool spa_json_is_float(const char *val, int len)
{
float dummy;
return spa_json_parse_float(val, len, &dummy);
}
static inline int spa_json_get_float(struct spa_json *iter, float *res)
{
const char *value;
int len;
if ((len = spa_json_next(iter, &value)) <= 0)
return -1;
return spa_json_parse_float(value, len, res);
}
/* bool */
static inline bool spa_json_is_true(const char *val, int len)
{
return len == 4 && strncmp(val, "true", 4) == 0;
}
static inline bool spa_json_is_false(const char *val, int len)
{
return len == 5 && strncmp(val, "false", 5) == 0;
}
static inline bool spa_json_is_bool(const char *val, int len)
{
return spa_json_is_true(val, len) || spa_json_is_false(val, len);
}
static inline int spa_json_parse_bool(const char *val, int len, bool *result)
{
if ((*result = spa_json_is_true(val, len)))
return 1;
if (!(*result = !spa_json_is_false(val, len)))
return 1;
return -1;
}
static inline int spa_json_get_bool(struct spa_json *iter, bool *res)
{
const char *value;
int len;
if ((len = spa_json_next(iter, &value)) <= 0)
return -1;
return spa_json_parse_bool(value, len, res);
}
/* string */
static inline bool spa_json_is_string(const char *val, int len)
{
return len > 1 && *val == '"';
}
static inline int spa_json_parse_string(const char *val, int len, char *result)
{
const char *p;
if (!spa_json_is_string(val, len))
return -1;
for (p = val+1; p < val + len-1; p++) {
if (*p == '\\') {
p++;
if (*p == 'n')
*result++ = '\n';
else if (*p == 'r')
*result++ = '\r';
else if (*p == 'b')
*result++ = '\b';
else if (*p == 't')
*result++ = '\t';
else if (*p == 'f')
*result++ = '\f';
else
*result++ = *p;
} else
*result++ = *p;
}
*result++ = '\0';
return 1;
}
static inline int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
{
const char *value;
int len;
if ((len = spa_json_next(iter, &value)) <= 0 || maxlen < len)
return -1;
return spa_json_parse_string(value, len, res);
}
static inline int spa_json_encode_string(char *str, int size, const char *val)
{
int len = 0;
#define __PUT(c) { if (len < size) *str++ = c; len++; }
__PUT('"');
while (*val) {
switch (*val) {
case '\n':
__PUT('\\'); __PUT('n');
break;
case '\r':
__PUT('\\'); __PUT('r');
break;
case '\b':
__PUT('\\'); __PUT('b');
break;
case '\t':
__PUT('\\'); __PUT('t');
break;
case '\f':
__PUT('\\'); __PUT('f');
break;
default:
__PUT(*val);
break;
}
val++;
}
__PUT('"');
__PUT('\0');
return len-1;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_UTILS_JSON_H */

View file

@ -3,6 +3,7 @@ test_apps = [
'test-node',
'test-pod',
'test-utils',
'test-json',
]
foreach a : test_apps

169
spa/tests/test-json.c Normal file
View file

@ -0,0 +1,169 @@
/* Simple Plugin API
*
* Copyright © 2020 Wim Taymans <wim.taymans@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <spa/utils/defs.h>
#include <spa/utils/json.h>
static void test_abi(void)
{
spa_assert(sizeof(struct spa_json) == 32);
}
#define TYPE_OBJECT 0
#define TYPE_ARRAY 1
#define TYPE_STRING 2
#define TYPE_BOOL 3
#define TYPE_NULL 4
#define TYPE_TRUE 5
#define TYPE_FALSE 6
#define TYPE_FLOAT 7
static void check_type(int type, const char *value, int len)
{
spa_assert(spa_json_is_object(value, len) == (type == TYPE_OBJECT));
spa_assert(spa_json_is_array(value, len) == (type == TYPE_ARRAY));
spa_assert(spa_json_is_string(value, len) == (type == TYPE_STRING));
spa_assert(spa_json_is_bool(value, len) ==
(type == TYPE_BOOL || type == TYPE_TRUE || type == TYPE_FALSE));
spa_assert(spa_json_is_null(value, len) == (type == TYPE_NULL));
spa_assert(spa_json_is_true(value, len) == (type == TYPE_TRUE || type == TYPE_BOOL));
spa_assert(spa_json_is_false(value, len) == (type == TYPE_FALSE || type == TYPE_BOOL));
spa_assert(spa_json_is_float(value, len) == (type == TYPE_FLOAT));
}
static void expect_type(struct spa_json *it, int type)
{
const char *value;
int len;
spa_assert((len = spa_json_next(it, &value)) > 0);
check_type(type, value, len);
}
static void expect_string(struct spa_json *it, const char *str)
{
const char *value;
int len;
char *s;
spa_assert((len = spa_json_next(it, &value)) > 0);
check_type(TYPE_STRING, value, len);
s = alloca(len);
spa_json_parse_string(value, len, s);
spa_assert(strcmp(s, str) == 0);
}
static void expect_float(struct spa_json *it, float val)
{
const char *value;
int len;
float f;
spa_assert((len = spa_json_next(it, &value)) > 0);
check_type(TYPE_FLOAT, value, len);
spa_assert(spa_json_parse_float(value, len, &f) > 0);
spa_assert(f == val);
}
static void test_parse(void)
{
struct spa_json it[5];
const char *json = " { "
"\"foo\": \"bar\","
"\"foo\\\" \": true, "
"\"foo \\n\\r\\t\": false,"
" \" arr\": [ true, false, null, 5, 5.7, \"str]\"],"
"\"foo 2\": null,"
"\"foo 3\": 1,"
" \"obj\": { \"ba } z\": false, \"empty\": [], \"foo\": { }, \"1.9\", 1.9 },"
"\"foo 4\" : 1.8, "
"\"foo 5\": -1.8 , "
"\"foo 6\": +2.8 ,"
" } ", *value;
spa_json_init(&it[0], json, strlen(json));
expect_type(&it[0], TYPE_OBJECT);
spa_json_enter(&it[0], &it[1]);
expect_string(&it[1], "foo");
expect_string(&it[1], "bar");
expect_string(&it[1], "foo\" ");
expect_type(&it[1], TYPE_TRUE);
expect_string(&it[1], "foo \n\r\t");
expect_type(&it[1], TYPE_FALSE);
expect_string(&it[1], " arr");
expect_type(&it[1], TYPE_ARRAY);
spa_json_enter(&it[1], &it[2]);
expect_string(&it[1], "foo 2");
expect_type(&it[1], TYPE_NULL);
expect_string(&it[1], "foo 3");
expect_float(&it[1], 1.f);
expect_string(&it[1], "obj");
expect_type(&it[1], TYPE_OBJECT);
spa_json_enter(&it[1], &it[3]);
expect_string(&it[1], "foo 4");
expect_float(&it[1], 1.8f);
expect_string(&it[1], "foo 5");
expect_float(&it[1], -1.8f);
expect_string(&it[1], "foo 6");
expect_float(&it[1], +2.8f);
/* in the array */
expect_type(&it[2], TYPE_TRUE);
expect_type(&it[2], TYPE_FALSE);
expect_type(&it[2], TYPE_NULL);
expect_float(&it[2], 5.f);
expect_float(&it[2], 5.7f);
expect_string(&it[2], "str]");
/* in the object */
expect_string(&it[3], "ba } z");
expect_type(&it[3], TYPE_FALSE);
expect_string(&it[3], "empty");
expect_type(&it[3], TYPE_ARRAY);
spa_json_enter(&it[3], &it[4]);
spa_assert(spa_json_next(&it[4], &value) == 0);
expect_string(&it[3], "foo");
expect_type(&it[3], TYPE_OBJECT);
spa_json_enter(&it[3], &it[4]);
expect_string(&it[3], "1.9");
expect_float(&it[3], 1.9f);
}
static void test_encode(void)
{
char dst[1024];
char dst4[4];
char dst6[6];
spa_assert(spa_json_encode_string(dst, sizeof(dst), "test") == 6);
spa_assert(strcmp(dst, "\"test\"") == 0);
spa_assert(spa_json_encode_string(dst4, sizeof(dst4), "test") == 6);
spa_assert(strncmp(dst4, "\"tes", 4) == 0);
spa_assert(spa_json_encode_string(dst6, sizeof(dst6), "test") == 6);
spa_assert(strncmp(dst6, "\"test\"", 6) == 0);
spa_assert(spa_json_encode_string(dst, sizeof(dst), "test\"\n\r \t\b\f\'") == 19);
spa_assert(strcmp(dst, "\"test\"\\n\\r \\t\\b\\f'\"") == 0);
}
int main(int argc, char *argv[])
{
test_abi();
test_parse();
test_encode();
return 0;
}