Diff for /libelwix/src/json.c between versions 1.12.2.1 and 1.14

version 1.12.2.1, 2025/08/25 10:05:52 version 1.14, 2026/03/31 15:29:12
Line 12  terms: Line 12  terms:
 All of the documentation and software included in the ELWIX and AITNET  All of the documentation and software included in the ELWIX and AITNET
 Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>  Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   
Copyright 2004 - 2025Copyright 2004 - 2026
         by Michael Pounov <misho@elwix.org>.  All rights reserved.          by Michael Pounov <misho@elwix.org>.  All rights reserved.
   
 Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
Line 436  json_token2val(const char *jstr, jtok_t * __restrict t Line 436  json_token2val(const char *jstr, jtok_t * __restrict t
 }  }
   
 /*  /*
    * json_token2rstr() - Return token to raw string
    *
    * @jstr = JSON string
    * @tok = Token for convert
    * @return =NULL error or !=NULL allocated str, after use should be json_freestr()|e_free()
    */
   char *
   json_token2rstr(const char *jstr, jtok_t * __restrict tok)
   {
           char *str = NULL;
           size_t len;
   
           if (!jstr || !tok)
                   return NULL;
   
           len = json_toklen(tok);
           str = e_malloc(len + 1);
           if (!str)
                   return NULL;
           else {
                   memcpy(str, json_tokstr(jstr, tok), len);
                   str[len] = 0;
           }
   
           return str;
   }
   
   /*
  * json_token2str() - Return token to string   * json_token2str() - Return token to string
  *   *
  * @jstr = JSON string   * @jstr = JSON string
Line 482  json_token2num(const char *jstr, jtok_t * __restrict t Line 510  json_token2num(const char *jstr, jtok_t * __restrict t
         long ret = 0;          long ret = 0;
         char *str;          char *str;
   
        str = json_token2str(jstr, tok);        str = json_token2rstr(jstr, tok);
         if (!str)          if (!str)
                 return 0;                  return 0;
   
Line 504  json_token2dbl(const char *jstr, jtok_t * __restrict t Line 532  json_token2dbl(const char *jstr, jtok_t * __restrict t
         double ret = 0;          double ret = 0;
         char *str;          char *str;
   
        str = json_token2str(jstr, tok);        str = json_token2rstr(jstr, tok);
         if (!str)          if (!str)
                 return 0;                  return 0;
   
Line 526  json_token2bool(const char *jstr, jtok_t * __restrict  Line 554  json_token2bool(const char *jstr, jtok_t * __restrict 
         double ret = 0;          double ret = 0;
         char *str;          char *str;
   
        str = json_token2str(jstr, tok);        str = json_token2rstr(jstr, tok);
         if (!str)          if (!str)
                 return 0;                  return 0;
   
Line 811  json_token2array(const char *jstr, jtok_t * __restrict Line 839  json_token2array(const char *jstr, jtok_t * __restrict
         return arr;          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 {   * json_add_begin_object() - Adds begin of object {
  *   *
Line 1421  json_validate(const char *jstr, int *started) Line 1466  json_validate(const char *jstr, int *started)
 {  {
         register int o = 0, a = 0, q = 0, pos = 0;          register int o = 0, a = 0, q = 0, pos = 0;
   
           if (!jstr)
                   return -1;
   
         if (started)          if (started)
                 *started = 0;                  *started = 0;
   
Line 1458  json_validate(const char *jstr, int *started) Line 1506  json_validate(const char *jstr, int *started)
         } while (q || a || o);          } while (q || a || o);
   
         return pos;          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;
 }  }

Removed from v.1.12.2.1  
changed lines
  Added in v.1.14


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>