Sat, 02 Nov 2024 19:37:59 +0100
add UCX string support to cxJsonFill()
src/cx/json.h | file | annotate | diff | comparison | revisions | |
src/json.c | file | annotate | diff | comparison | revisions | |
tests/test_json.c | file | annotate | diff | comparison | revisions |
--- a/src/cx/json.h Sat Nov 02 19:27:45 2024 +0100 +++ b/src/cx/json.h Sat Nov 02 19:37:59 2024 +0100 @@ -180,7 +180,39 @@ void cxJsonDestroy(CxJson *json); __attribute__((__nonnull__)) -void cxJsonFill(CxJson *json, const char *buf, size_t len); +int cxJsonFilln(CxJson *json, const char *buf, size_t len); + +#define cxJsonFill(prop, str) _Generic((str), \ + cxstring: cx_json_fill_cxstr, \ + cxmutstr: cx_json_fill_mutstr, \ + char*: cx_json_fill_str, \ + const char*: cx_json_fill_str) \ + (prop, str) + +__attribute__((__nonnull__)) +static inline int cx_json_fill_cxstr( + CxJson *json, + cxstring str +) { + return cxJsonFilln(json, str.ptr, str.length); +} + +__attribute__((__nonnull__)) +static inline int cx_json_fill_mutstr( + CxJson *json, + cxmutstr str +) { + return cxJsonFilln(json, str.ptr, str.length); +} + +__attribute__((__nonnull__)) +static inline int cx_json_fill_str( + CxJson *json, + const char *str +) { + return cxJsonFilln(json, str, strlen(str)); +} + __attribute__((__nonnull__)) int cxJsonNext(CxJson *json, CxJsonValue **value);
--- a/src/json.c Sat Nov 02 19:27:45 2024 +0100 +++ b/src/json.c Sat Nov 02 19:37:59 2024 +0100 @@ -599,11 +599,12 @@ free(p->value_str); } -void cxJsonFill(CxJson *p, const char *buf, size_t size) { +int cxJsonFilln(CxJson *p, const char *buf, size_t size) { // TODO: implement rescue buffer like in CxProperties to allow subsequent fills p->buffer = buf; p->size = size; p->pos = 0; + return 0; } int cxJsonNext(CxJson *p, CxJsonValue **value) {
--- a/tests/test_json.c Sat Nov 02 19:27:45 2024 +0100 +++ b/tests/test_json.c Sat Nov 02 19:37:59 2024 +0100 @@ -59,7 +59,7 @@ CxJson json; cxJsonInit(&json); - cxJsonFill(&json, text.ptr, text.length); + cxJsonFill(&json, text); // parse the big fat object CxJsonValue *obj; @@ -130,7 +130,7 @@ size_t part = 0; while(part < nparts) { - cxJsonFill(&json, parts[part].ptr, parts[part].length); + cxJsonFill(&json, parts[part]); part++; result = cxJsonNext(&json, &obj); @@ -191,7 +191,7 @@ for(int i=0;i<5;i++) { cxJsonInit(&json); - cxJsonFill(&json, tests[i].ptr, tests[i].length); + cxJsonFill(&json, tests[i]); result = cxJsonNext(&json, &obj); CX_TEST_ASSERT(result == -1); @@ -207,7 +207,7 @@ cxstring text = cx_str("{\"test\": [{},{\"foo\": [[{\"bar\":[4, 2, [null, {\"key\": 47}]]}]]}]}"); CX_TEST_DO { cxJsonInit(&json); - cxJsonFill(&json, text.ptr, text.length); + cxJsonFill(&json, text); cxJsonNext(&json, &d1); CX_TEST_ASSERT(d1 != NULL); @@ -246,6 +246,17 @@ } } +CX_TEST(test_json_number) { + CxJson json; + cxJsonInit(&json); + CX_TEST_DO { + CxJsonValue *v; + + cxJsonNext(&json, &v); + } + cxJsonDestroy(&json); +} + CxTestSuite *cx_test_suite_json(void) { CxTestSuite *suite = cx_test_suite_new("json");