2016-10-12 21:08:38 +00:00
|
|
|
|
|
|
|
/*
|
2018-08-29 15:53:04 +00:00
|
|
|
|
|
|
|
<https://github.com/rafagafe/tiny-json>
|
2020-02-28 21:50:15 +00:00
|
|
|
|
2018-08-29 15:53:04 +00:00
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
|
|
|
SPDX-License-Identifier: MIT
|
2018-08-31 23:25:10 +00:00
|
|
|
Copyright (c) 2016-2018 Rafa Garcia <rafagarcia77@gmail.com>.
|
2018-08-29 15:53:04 +00:00
|
|
|
|
|
|
|
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 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.
|
2020-02-28 21:50:15 +00:00
|
|
|
|
2018-08-29 15:53:04 +00:00
|
|
|
*/
|
2016-10-12 21:08:38 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2018-09-23 22:42:03 +00:00
|
|
|
#include <ctype.h>
|
2016-10-12 21:08:38 +00:00
|
|
|
#include "tiny-json.h"
|
|
|
|
|
|
|
|
/** Structure to handle a heap of JSON properties. */
|
2018-11-10 18:37:33 +00:00
|
|
|
typedef struct jsonStaticPool_s {
|
2020-03-28 18:35:44 +00:00
|
|
|
json_t* mem; /**< Pointer to array of json properties. */
|
|
|
|
unsigned int qty; /**< Length of the array of json properties. */
|
2016-10-12 21:08:38 +00:00
|
|
|
unsigned int nextFree; /**< The index of the next free json property. */
|
2018-11-10 18:37:33 +00:00
|
|
|
jsonPool_t pool;
|
|
|
|
} jsonStaticPool_t;
|
2016-10-12 21:08:38 +00:00
|
|
|
|
|
|
|
/* Search a property by its name in a JSON object. */
|
|
|
|
json_t const* json_getProperty( json_t const* obj, char const* property ) {
|
|
|
|
json_t const* sibling;
|
2018-04-26 10:47:54 +00:00
|
|
|
for( sibling = obj->u.c.child; sibling; sibling = sibling->sibling )
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( sibling->name && !strcmp( sibling->name, property ) )
|
|
|
|
return sibling;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-03 08:12:11 +00:00
|
|
|
/* Search a property by its name in a JSON object and return its value. */
|
|
|
|
char const* json_getPropertyValue( json_t const* obj, char const* property ) {
|
|
|
|
json_t const* field = json_getProperty( obj, property );
|
|
|
|
if ( !field ) return 0;
|
2017-04-05 00:25:17 +00:00
|
|
|
jsonType_t type = json_getType( field );
|
|
|
|
if ( JSON_ARRAY >= type ) return 0;
|
2017-04-03 08:12:11 +00:00
|
|
|
return json_getValue( field );
|
|
|
|
}
|
|
|
|
|
2016-10-12 21:08:38 +00:00
|
|
|
/* Internal prototypes: */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* goBlank( char* str );
|
|
|
|
static char* goNum( char* str );
|
|
|
|
static json_t* poolInit( jsonPool_t* pool );
|
2018-12-26 21:03:45 +00:00
|
|
|
static json_t* poolAlloc( jsonPool_t* pool );
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool );
|
|
|
|
static char* setToNull( char* ch );
|
|
|
|
static bool isEndOfPrimitive( char ch );
|
2016-10-12 21:08:38 +00:00
|
|
|
|
|
|
|
/* Parse a string to get a json. */
|
2018-11-14 23:43:30 +00:00
|
|
|
json_t const* json_createWithPool( char *str, jsonPool_t *pool ) {
|
2017-04-03 23:37:49 +00:00
|
|
|
char* ptr = goBlank( str );
|
2020-12-01 20:31:24 +00:00
|
|
|
if ( !ptr || (*ptr != '{' && *ptr != '[') ) return 0;
|
2018-11-10 18:37:33 +00:00
|
|
|
json_t* obj = pool->init( pool );
|
2016-10-12 22:19:55 +00:00
|
|
|
obj->name = 0;
|
|
|
|
obj->sibling = 0;
|
2018-04-26 10:47:54 +00:00
|
|
|
obj->u.c.child = 0;
|
2018-11-10 18:37:33 +00:00
|
|
|
ptr = objValue( ptr, obj, pool );
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( !ptr ) return 0;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2018-11-10 18:37:33 +00:00
|
|
|
/* Parse a string to get a json. */
|
|
|
|
json_t const* json_create( char* str, json_t mem[], unsigned int qty ) {
|
2020-03-28 18:35:44 +00:00
|
|
|
jsonStaticPool_t spool;
|
|
|
|
spool.mem = mem;
|
|
|
|
spool.qty = qty;
|
|
|
|
spool.pool.init = poolInit;
|
|
|
|
spool.pool.alloc = poolAlloc;
|
2018-11-14 23:43:30 +00:00
|
|
|
return json_createWithPool( str, &spool.pool );
|
2018-11-10 18:37:33 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Get a special character with its escape character. Examples:
|
2020-12-01 20:32:15 +00:00
|
|
|
* 'b' -> '\\b', 'n' -> '\\n', 't' -> '\\t'
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param ch The escape character.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @retval The character code. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char getEscape( char ch ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
static struct { char ch; char code; } const pair[] = {
|
2017-05-19 22:21:27 +00:00
|
|
|
{ '\"', '\"' }, { '\\', '\\' },
|
|
|
|
{ '/', '/' }, { 'b', '\b' },
|
|
|
|
{ 'f', '\f' }, { 'n', '\n' },
|
|
|
|
{ 'r', '\r' }, { 't', '\t' },
|
2016-10-12 21:08:38 +00:00
|
|
|
};
|
|
|
|
unsigned int i;
|
|
|
|
for( i = 0; i < sizeof pair / sizeof *pair; ++i )
|
|
|
|
if ( pair[i].ch == ch )
|
|
|
|
return pair[i].code;
|
|
|
|
return '\0';
|
|
|
|
}
|
|
|
|
|
2017-04-05 07:43:01 +00:00
|
|
|
/** Parse 4 characters.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param str Pointer to first digit.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @retval '?' If the four characters are hexadecimal digits.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @retval '\0' In other cases. */
|
2018-05-13 23:32:02 +00:00
|
|
|
static unsigned char getCharFromUnicode( unsigned char const* str ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
unsigned int i;
|
|
|
|
for( i = 0; i < 4; ++i )
|
2018-09-23 22:42:03 +00:00
|
|
|
if ( !isxdigit( str[i] ) )
|
2016-10-12 21:08:38 +00:00
|
|
|
return '\0';
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse a string and replace the scape characters by their meaning characters.
|
|
|
|
* This parser stops when finds the character '\"'. Then replaces '\"' by '\0'.
|
2016-10-12 22:19:55 +00:00
|
|
|
* @param str Pointer to first character.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @retval Pointer to first non white space after the string. If success.
|
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* parseString( char* str ) {
|
2018-05-13 23:32:02 +00:00
|
|
|
unsigned char* head = (unsigned char*)str;
|
|
|
|
unsigned char* tail = (unsigned char*)str;
|
2020-02-27 20:01:25 +00:00
|
|
|
for( ; *head; ++head, ++tail ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( *head == '\"' ) {
|
|
|
|
*tail = '\0';
|
2018-05-13 23:32:02 +00:00
|
|
|
return (char*)++head;
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
if ( *head == '\\' ) {
|
|
|
|
if ( *++head == 'u' ) {
|
2017-04-03 23:37:49 +00:00
|
|
|
char const ch = getCharFromUnicode( ++head );
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( ch == '\0' ) return 0;
|
|
|
|
*tail = ch;
|
|
|
|
head += 3;
|
|
|
|
}
|
|
|
|
else {
|
2017-04-03 23:37:49 +00:00
|
|
|
char const esc = getEscape( *head );
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( esc == '\0' ) return 0;
|
|
|
|
*tail = esc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else *tail = *head;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Parse a string to get the name of a property.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param property The property to assign the name.
|
|
|
|
* @retval Pointer to first of property value. If success.
|
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* propertyName( char* ptr, json_t* property ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
property->name = ++ptr;
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = parseString( ptr );
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( !ptr ) return 0;
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = goBlank( ptr );
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( !ptr ) return 0;
|
|
|
|
if ( *ptr++ != ':' ) return 0;
|
2017-04-03 23:37:49 +00:00
|
|
|
return goBlank( ptr );
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Parse a string to get the value of a property when its type is JSON_TEXT.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character ('\"').
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param property The property to assign the name.
|
|
|
|
* @retval Pointer to first non white space after the string. If success.
|
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* textValue( char* ptr, json_t* property ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
++property->u.value;
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = parseString( ++ptr );
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( !ptr ) return 0;
|
2016-10-12 22:19:55 +00:00
|
|
|
property->type = JSON_TEXT;
|
2016-10-12 21:08:38 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Compare two strings until get the null character in the second one.
|
2016-10-12 22:19:55 +00:00
|
|
|
* @param ptr sub string
|
|
|
|
* @param str main string
|
2017-04-05 07:43:01 +00:00
|
|
|
* @retval Pointer to next character.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* checkStr( char* ptr, char const* str ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
while( *str )
|
|
|
|
if ( *ptr++ != *str++ )
|
|
|
|
return 0;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Parser a string to get a primitive value.
|
2017-04-05 07:43:01 +00:00
|
|
|
* If the first character after the value is different of '}' or ']' is set to '\0'.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param property Property handler to set the value and the type, (true, false or null).
|
|
|
|
* @param value String with the primitive literal.
|
|
|
|
* @param type The code of the type. ( JSON_BOOLEAN or JSON_NULL )
|
|
|
|
* @retval Pointer to first non white space after the string. If success.
|
2016-10-12 22:19:55 +00:00
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* primitiveValue( char* ptr, json_t* property, char const* value, jsonType_t type ) {
|
|
|
|
ptr = checkStr( ptr, value );
|
|
|
|
if ( !ptr || !isEndOfPrimitive( *ptr ) ) return 0;
|
|
|
|
ptr = setToNull( ptr );
|
2016-10-12 22:19:55 +00:00
|
|
|
property->type = type;
|
2016-10-12 21:08:38 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Parser a string to get a true value.
|
2017-04-05 07:43:01 +00:00
|
|
|
* If the first character after the value is different of '}' or ']' is set to '\0'.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param property Property handler to set the value and the type, (true, false or null).
|
|
|
|
* @retval Pointer to first non white space after the string. If success.
|
2016-10-12 22:19:55 +00:00
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* trueValue( char* ptr, json_t* property ) {
|
|
|
|
return primitiveValue( ptr, property, "true", JSON_BOOLEAN );
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Parser a string to get a false value.
|
2017-04-05 07:43:01 +00:00
|
|
|
* If the first character after the value is different of '}' or ']' is set to '\0'.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param property Property handler to set the value and the type, (true, false or null).
|
|
|
|
* @retval Pointer to first non white space after the string. If success.
|
2016-10-12 22:19:55 +00:00
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* falseValue( char* ptr, json_t* property ) {
|
|
|
|
return primitiveValue( ptr, property, "false", JSON_BOOLEAN );
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Parser a string to get a null value.
|
2017-04-05 07:43:01 +00:00
|
|
|
* If the first character after the value is different of '}' or ']' is set to '\0'.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param property Property handler to set the value and the type, (true, false or null).
|
|
|
|
* @retval Pointer to first non white space after the string. If success.
|
2016-10-12 22:19:55 +00:00
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* nullValue( char* ptr, json_t* property ) {
|
|
|
|
return primitiveValue( ptr, property, "null", JSON_NULL );
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 23:37:49 +00:00
|
|
|
/** Analyze the exponential part of a real number.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character.
|
2017-04-03 23:37:49 +00:00
|
|
|
* @retval Pointer to first non numerical after the string. If success.
|
|
|
|
* @retval Null pointer if any error occur. */
|
|
|
|
static char* expValue( char* ptr ) {
|
2016-10-18 23:17:01 +00:00
|
|
|
if ( *ptr == '-' || *ptr == '+' ) ++ptr;
|
2020-02-28 21:50:15 +00:00
|
|
|
if ( !isdigit( (int)(*ptr) ) ) return 0;
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = goNum( ++ptr );
|
2016-10-18 23:17:01 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2017-04-03 23:37:49 +00:00
|
|
|
/** Analyze the decimal part of a real number.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character.
|
2017-04-03 23:37:49 +00:00
|
|
|
* @retval Pointer to first non numerical after the string. If success.
|
|
|
|
* @retval Null pointer if any error occur. */
|
|
|
|
static char* fraqValue( char* ptr ) {
|
2020-02-28 21:50:15 +00:00
|
|
|
if ( !isdigit( (int)(*ptr) ) ) return 0;
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = goNum( ++ptr );
|
2016-10-18 23:17:01 +00:00
|
|
|
if ( !ptr ) return 0;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2017-04-03 23:37:49 +00:00
|
|
|
/** Parser a string to get a numerical value.
|
2017-04-05 07:43:01 +00:00
|
|
|
* If the first character after the value is different of '}' or ']' is set to '\0'.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character.
|
2017-04-03 21:22:15 +00:00
|
|
|
* @param property Property handler to set the value and the type: JSON_REAL or JSON_INTEGER.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @retval Pointer to first non white space after the string. If success.
|
2016-10-12 22:19:55 +00:00
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* numValue( char* ptr, json_t* property ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( *ptr == '-' ) ++ptr;
|
2020-02-28 21:50:15 +00:00
|
|
|
if ( !isdigit( (int)(*ptr) ) ) return 0;
|
2016-10-18 23:17:01 +00:00
|
|
|
if ( *ptr != '0' ) {
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = goNum( ptr );
|
2016-10-18 23:17:01 +00:00
|
|
|
if ( !ptr ) return 0;
|
|
|
|
}
|
2020-02-28 21:50:15 +00:00
|
|
|
else if ( isdigit( (int)(*++ptr) ) ) return 0;
|
2017-04-03 23:37:49 +00:00
|
|
|
property->type = JSON_INTEGER;
|
2016-10-18 23:17:01 +00:00
|
|
|
if ( *ptr == '.' ) {
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = fraqValue( ++ptr );
|
2016-10-18 23:17:01 +00:00
|
|
|
if ( !ptr ) return 0;
|
2017-04-05 07:10:24 +00:00
|
|
|
property->type = JSON_REAL;
|
2016-10-18 23:17:01 +00:00
|
|
|
}
|
2017-04-03 23:37:49 +00:00
|
|
|
if ( *ptr == 'e' || *ptr == 'E' ) {
|
|
|
|
ptr = expValue( ++ptr );
|
2016-10-18 23:17:01 +00:00
|
|
|
if ( !ptr ) return 0;
|
2017-04-05 07:10:24 +00:00
|
|
|
property->type = JSON_REAL;
|
2017-04-03 23:37:49 +00:00
|
|
|
}
|
2017-04-05 07:10:24 +00:00
|
|
|
if ( !isEndOfPrimitive( *ptr ) ) return 0;
|
2017-04-03 23:37:49 +00:00
|
|
|
if ( JSON_INTEGER == property->type ) {
|
|
|
|
char const* value = property->u.value;
|
|
|
|
bool const negative = *value == '-';
|
2023-10-25 08:54:56 +00:00
|
|
|
static char const min[] = "-9223372036854775808"; // min int64_t
|
|
|
|
static char const max[] = "18446744073709551615"; // max uint64_t
|
2017-05-19 22:21:27 +00:00
|
|
|
unsigned int const maxdigits = ( negative? sizeof min: sizeof max ) - 1;
|
2020-03-28 18:35:44 +00:00
|
|
|
unsigned int const len = ( unsigned int const ) ( ptr - value );
|
2017-04-03 23:37:49 +00:00
|
|
|
if ( len > maxdigits ) return 0;
|
2023-10-25 08:54:56 +00:00
|
|
|
if ( negative == 0 )
|
|
|
|
property->type = JSON_UINTEGER;
|
2017-04-03 23:37:49 +00:00
|
|
|
if ( len == maxdigits ) {
|
|
|
|
char const tmp = *ptr;
|
2017-04-05 07:10:24 +00:00
|
|
|
*ptr = '\0';
|
2017-04-03 23:37:49 +00:00
|
|
|
char const* const threshold = negative ? min: max;
|
2017-04-05 07:10:24 +00:00
|
|
|
if ( 0 > strcmp( threshold, value ) ) return 0;
|
2017-04-03 23:37:49 +00:00
|
|
|
*ptr = tmp;
|
|
|
|
}
|
2016-10-18 23:17:01 +00:00
|
|
|
}
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = setToNull( ptr );
|
2016-10-12 22:19:55 +00:00
|
|
|
return ptr;
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
2017-05-19 22:21:27 +00:00
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Add a property to a JSON object or array.
|
|
|
|
* @param obj The handler of the JSON object or array.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param property The handler of the property to be added. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static void add( json_t* obj, json_t* property ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
property->sibling = 0;
|
2018-04-26 10:47:54 +00:00
|
|
|
if ( !obj->u.c.child ){
|
|
|
|
obj->u.c.child = property;
|
|
|
|
obj->u.c.last_child = property;
|
2018-03-29 20:33:51 +00:00
|
|
|
} else {
|
2018-04-26 10:47:54 +00:00
|
|
|
obj->u.c.last_child->sibling = property;
|
|
|
|
obj->u.c.last_child = property;
|
2016-10-12 22:19:55 +00:00
|
|
|
}
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Parser a string to get a json object value.
|
2020-12-01 20:32:15 +00:00
|
|
|
* @param ptr Pointer to first character.
|
|
|
|
* @param obj The handler of the JSON root object or array.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param pool The handler of a json pool for creating json instances.
|
|
|
|
* @retval Pointer to first character after the value. If success.
|
2016-10-12 22:19:55 +00:00
|
|
|
* @retval Null pointer if any error occur. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool ) {
|
2020-12-01 20:31:24 +00:00
|
|
|
obj->type = *ptr == '{' ? JSON_OBJ : JSON_ARRAY;
|
2018-04-26 10:47:54 +00:00
|
|
|
obj->u.c.child = 0;
|
2016-10-12 22:19:55 +00:00
|
|
|
obj->sibling = 0;
|
2016-10-12 21:08:38 +00:00
|
|
|
ptr++;
|
|
|
|
for(;;) {
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = goBlank( ptr );
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( !ptr ) return 0;
|
2017-04-05 00:25:17 +00:00
|
|
|
if ( *ptr == ',' ) {
|
|
|
|
++ptr;
|
|
|
|
continue;
|
2017-04-05 07:10:24 +00:00
|
|
|
}
|
2016-10-12 21:08:38 +00:00
|
|
|
char const endchar = ( obj->type == JSON_OBJ )? '}': ']';
|
|
|
|
if ( *ptr == endchar ) {
|
|
|
|
*ptr = '\0';
|
|
|
|
json_t* parentObj = obj->sibling;
|
|
|
|
if ( !parentObj ) return ++ptr;
|
|
|
|
obj->sibling = 0;
|
2016-10-12 22:19:55 +00:00
|
|
|
obj = parentObj;
|
2016-10-12 21:08:38 +00:00
|
|
|
++ptr;
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-26 21:03:45 +00:00
|
|
|
json_t* property = pool->alloc( pool );
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( !property ) return 0;
|
|
|
|
if( obj->type != JSON_ARRAY ) {
|
2016-10-12 22:19:55 +00:00
|
|
|
if ( *ptr != '\"' ) return 0;
|
2017-04-03 23:37:49 +00:00
|
|
|
ptr = propertyName( ptr, property );
|
2016-10-12 21:08:38 +00:00
|
|
|
if ( !ptr ) return 0;
|
2016-10-12 22:19:55 +00:00
|
|
|
}
|
2016-10-12 21:08:38 +00:00
|
|
|
else property->name = 0;
|
2017-04-03 23:37:49 +00:00
|
|
|
add( obj, property );
|
2016-10-12 21:08:38 +00:00
|
|
|
property->u.value = ptr;
|
|
|
|
switch( *ptr ) {
|
|
|
|
case '{':
|
2016-10-12 22:19:55 +00:00
|
|
|
property->type = JSON_OBJ;
|
2018-04-26 10:47:54 +00:00
|
|
|
property->u.c.child = 0;
|
2016-10-12 21:08:38 +00:00
|
|
|
property->sibling = obj;
|
|
|
|
obj = property;
|
|
|
|
++ptr;
|
|
|
|
break;
|
|
|
|
case '[':
|
2016-10-12 22:19:55 +00:00
|
|
|
property->type = JSON_ARRAY;
|
2018-04-26 10:47:54 +00:00
|
|
|
property->u.c.child = 0;
|
2016-10-12 21:08:38 +00:00
|
|
|
property->sibling = obj;
|
|
|
|
obj = property;
|
|
|
|
++ptr;
|
2016-10-12 22:19:55 +00:00
|
|
|
break;
|
2017-04-05 07:10:24 +00:00
|
|
|
case '\"': ptr = textValue( ptr, property ); break;
|
|
|
|
case 't': ptr = trueValue( ptr, property ); break;
|
|
|
|
case 'f': ptr = falseValue( ptr, property ); break;
|
|
|
|
case 'n': ptr = nullValue( ptr, property ); break;
|
|
|
|
default: ptr = numValue( ptr, property ); break;
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
if ( !ptr ) return 0;
|
2016-10-12 22:19:55 +00:00
|
|
|
}
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize a json pool.
|
|
|
|
* @param pool The handler of the pool.
|
2017-04-05 07:43:01 +00:00
|
|
|
* @return a instance of a json. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static json_t* poolInit( jsonPool_t* pool ) {
|
2018-11-14 23:43:30 +00:00
|
|
|
jsonStaticPool_t *spool = json_containerOf( pool, jsonStaticPool_t, pool );
|
2018-11-10 18:37:33 +00:00
|
|
|
spool->nextFree = 1;
|
2018-11-14 23:43:30 +00:00
|
|
|
return spool->mem;
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Create an instance of a json from a pool.
|
|
|
|
* @param pool The handler of the pool.
|
|
|
|
* @retval The handler of the new instance if success.
|
|
|
|
* @retval Null pointer if the pool was empty. */
|
2018-12-26 21:03:45 +00:00
|
|
|
static json_t* poolAlloc( jsonPool_t* pool ) {
|
2018-11-14 23:43:30 +00:00
|
|
|
jsonStaticPool_t *spool = json_containerOf( pool, jsonStaticPool_t, pool );
|
2018-11-10 18:37:33 +00:00
|
|
|
if ( spool->nextFree >= spool->qty ) return 0;
|
2018-11-14 23:43:30 +00:00
|
|
|
return spool->mem + spool->nextFree++;
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Checks whether an character belongs to set.
|
|
|
|
* @param ch Character value to be checked.
|
|
|
|
* @param set Set of characters. It is just a null-terminated string.
|
|
|
|
* @return true or false there is membership or not. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static bool isOneOfThem( char ch, char const* set ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
while( *set != '\0' )
|
|
|
|
if ( ch == *set++ )
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-12 22:19:55 +00:00
|
|
|
/** Increases a pointer while it points to a character that belongs to a set.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param str The initial pointer value.
|
|
|
|
* @param set Set of characters. It is just a null-terminated string.
|
|
|
|
* @return The final pointer value or null pointer if the null character was found. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* goWhile( char* str, char const* set ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
for(; *str != '\0'; ++str ) {
|
2017-04-03 23:37:49 +00:00
|
|
|
if ( !isOneOfThem( *str, set ) )
|
2016-10-12 21:08:38 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-03 23:37:49 +00:00
|
|
|
/** Set of characters that defines a blank. */
|
|
|
|
static char const* const blank = " \n\r\t\f";
|
2017-04-03 21:22:15 +00:00
|
|
|
|
2016-10-12 21:08:38 +00:00
|
|
|
/** Increases a pointer while it points to a white space character.
|
|
|
|
* @param str The initial pointer value.
|
|
|
|
* @return The final pointer value or null pointer if the null character was found. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* goBlank( char* str ) {
|
|
|
|
return goWhile( str, blank );
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Increases a pointer while it points to a decimal digit character.
|
|
|
|
* @param str The initial pointer value.
|
|
|
|
* @return The final pointer value or null pointer if the null character was found. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* goNum( char* str ) {
|
2016-10-12 21:08:38 +00:00
|
|
|
for( ; *str != '\0'; ++str ) {
|
2020-02-28 21:50:15 +00:00
|
|
|
if ( !isdigit( (int)(*str) ) )
|
2016-10-12 21:08:38 +00:00
|
|
|
return str;
|
|
|
|
}
|
2016-10-12 22:19:55 +00:00
|
|
|
return 0;
|
2016-10-12 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 23:37:49 +00:00
|
|
|
/** Set of characters that defines the end of an array or a JSON object. */
|
2017-04-03 21:22:15 +00:00
|
|
|
static char const* const endofblock = "}]";
|
|
|
|
|
2017-04-05 07:43:01 +00:00
|
|
|
/** Set a char to '\0' and increase its pointer if the char is different to '}' or ']'.
|
2016-10-12 21:08:38 +00:00
|
|
|
* @param ch Pointer to character.
|
|
|
|
* @return Final value pointer. */
|
2017-04-03 23:37:49 +00:00
|
|
|
static char* setToNull( char* ch ) {
|
|
|
|
if ( !isOneOfThem( *ch, endofblock ) ) *ch++ = '\0';
|
2016-10-12 21:08:38 +00:00
|
|
|
return ch;
|
|
|
|
}
|
2017-04-03 21:22:15 +00:00
|
|
|
|
2017-04-03 23:37:49 +00:00
|
|
|
/** Indicate if a character is the end of a primitive value. */
|
|
|
|
static bool isEndOfPrimitive( char ch ) {
|
2017-04-05 07:10:24 +00:00
|
|
|
return ch == ',' || isOneOfThem( ch, blank ) || isOneOfThem( ch, endofblock );
|
2017-05-19 22:21:27 +00:00
|
|
|
}
|