--- libelwix/src/json.c 2025/08/25 10:05:52 1.12.2.1 +++ libelwix/src/json.c 2025/08/25 13:00:37 1.13 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: json.c,v 1.12.2.1 2025/08/25 10:05:52 misho Exp $ +* $Id: json.c,v 1.13 2025/08/25 13:00:37 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -811,7 +811,24 @@ json_token2array(const char *jstr, jtok_t * __restrict return arr; } +/* + * json_freearray() - Free & destroy allocated array from json_token2array function + * + * @parr = Array + * return -1 error or 0 ok + */ +int +json_freearray(array_t **parr) +{ + if (!parr) + return -1; + array_Free(*parr); + array_Destroy(parr); + return 0; +} + + /* * json_add_begin_object() - Adds begin of object { * @@ -1421,6 +1438,9 @@ json_validate(const char *jstr, int *started) { register int o = 0, a = 0, q = 0, pos = 0; + if (!jstr) + return -1; + if (started) *started = 0; @@ -1458,4 +1478,57 @@ json_validate(const char *jstr, int *started) } while (q || a || o); return pos; +} + +/* + * json_marshaling() - Prepare JSON to one continues line + * + * @jstr = JSON string + * @space = if it is 0 then spaces will be removed + * return NULL error or !=NULL ready JSON for proceeding + */ +char * +json_marshaling(char * __restrict jstr, int space) +{ + int started, ended, len; + char *str, *pos, *js; + register int q = 0; + + if (!jstr) + return NULL; + else + js = jstr; + + ended = json_validate(jstr, &started); + if (!ended) + return NULL; + len = ended - started + 1; + str = e_malloc(len); + if (!str) + return NULL; + else { + memset(str, 0, len); + pos = str; + } + + js += started; + while (*js && js < jstr + ended && --len) { + if (*js < 0x20) { + js++; + continue; + } + if (space && !q && *js == 0x20) { + js++; + continue; + } + if (*js == '"') + q = ~q; + *pos++ = *js++; + } + *pos = 0; + + len = strlen(jstr) + 1; + strlcpy(jstr, str, len); + e_free(str); + return jstr; }