--- libelwix/src/json.c 2024/12/04 17:47:28 1.11 +++ libelwix/src/json.c 2025/08/21 15:41:26 1.11.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: json.c,v 1.11 2024/12/04 17:47:28 misho Exp $ +* $Id: json.c,v 1.11.2.2 2025/08/21 15:41:26 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2024 +Copyright 2004 - 2025 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -597,7 +597,7 @@ json_findbykey(const char *jstr, const char *key, jtyp * @type = Search key for particular token type, if is J_UNDEF this mean any type * @toks = Parsed tokens * @toksnum = Number of parsed tokens - * return: =NULL error or !=NULL data token found + * return: =NULL error or !=NULL data token found */ jtok_t * json_findbykeyatscope(long scope, const char *jstr, const char *key, jtype_t type, jtok_t * __restrict toks, int toksnum) @@ -1325,4 +1325,40 @@ json_objscope(const char *key, const char *jstr, jtok_ } return scope; +} + +/* + * json_validate() - Validate JSON + * + * @jstr = JSON string + * return: -1 error or >=0 where valid JSON ends + */ +int +json_validate(const char *jstr) +{ + register int o = 0, a = 0, pos = 0; + + while (isspace(jstr[pos]) || jstr[pos] != '{') + pos++; + + do { + switch (jstr[pos++]) { + case '{': + o++; + break; + case '}': + o--; + break; + case '[': + a++; + break; + case ']': + a--; + break; + case 0: /* string ends without valid JSON */ + return 0; + } + } while (a || o); + + return pos; }