mirror of
https://github.com/rafagafe/tiny-json.git
synced 2025-03-12 19:25:30 +00:00
Defensive programming is added.
This commit is contained in:
parent
f77812a288
commit
37937178bc
12
tiny-json.c
12
tiny-json.c
@ -40,6 +40,8 @@ json_t const* json_getProperty( json_t const* obj, char const* property ) {
|
||||
char const* json_getPropertyValue( json_t const* obj, char const* property ) {
|
||||
json_t const* field = json_getProperty( obj, property );
|
||||
if ( !field ) return 0;
|
||||
jsonType_t type = json_getType( field );
|
||||
if ( JSON_ARRAY >= type ) return 0;
|
||||
return json_getValue( field );
|
||||
}
|
||||
|
||||
@ -322,6 +324,10 @@ static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool ) {
|
||||
for(;;) {
|
||||
ptr = goBlank( ptr );
|
||||
if ( !ptr ) return 0;
|
||||
if ( *ptr == ',' ) {
|
||||
++ptr;
|
||||
continue;
|
||||
}
|
||||
char const endchar = ( obj->type == JSON_OBJ )? '}': ']';
|
||||
if ( *ptr == endchar ) {
|
||||
*ptr = '\0';
|
||||
@ -332,10 +338,6 @@ static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool ) {
|
||||
++ptr;
|
||||
continue;
|
||||
}
|
||||
if ( *ptr == ',' ) {
|
||||
ptr = goBlank( ++ptr );
|
||||
if ( !ptr ) return 0;
|
||||
}
|
||||
json_t* property = poolNew( pool );
|
||||
if ( !property ) return 0;
|
||||
if( obj->type != JSON_ARRAY ) {
|
||||
@ -365,7 +367,7 @@ static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool ) {
|
||||
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;
|
||||
default: ptr = numValue( ptr, property ); break;
|
||||
}
|
||||
if ( !ptr ) return 0;
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ json_t const* json_getProperty( json_t const* obj, char const* property );
|
||||
* @param obj A valid handler of a json object. Its type must be JSON_OBJ.
|
||||
* @param property The name of property to get.
|
||||
* @retval If found a pointer to null-terminated string with the value.
|
||||
* @retval Null pointer if not found. */
|
||||
* @retval Null pointer if not found or it is an array or an object. */
|
||||
char const* json_getPropertyValue( json_t const* obj, char const* property );
|
||||
|
||||
/** Get the first property of a JSON object or array.
|
||||
|
Loading…
Reference in New Issue
Block a user