From baf48cb37bac6d15b448764960139115d06aab96 Mon Sep 17 00:00:00 2001 From: rafagafe Date: Mon, 3 Apr 2017 23:22:15 +0200 Subject: [PATCH] Remove unnecessary JSON type. --- tests.c | 19 +++++++++++++++---- tiny-json.c | 24 +++++++++++++++++------- tiny-json.h | 2 +- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/tests.c b/tests.c index 1fae31b..a1517e0 100644 --- a/tests.c +++ b/tests.c @@ -59,12 +59,12 @@ static int primitive( void ) { unsigned const qty = sizeof pool / sizeof *pool; { char str[] = "{" + "\"real\": -0.004," "\"boolvar0\": false," "\"boolvar1\": true," "\"nullvar\": null," "\"max\": 9223372036854775807," "\"min\": -9223372036854775808," - "\"real\": -0.004," "\"scientific\": 5368.32e-3," "}"; @@ -108,9 +108,10 @@ static int primitive( void ) { json_t const* scientific = json_getProperty( json, "scientific" ); check( scientific ); - check( JSON_SCIENTIFIC == json_getType( scientific ) ); + check( JSON_REAL == json_getType( scientific ) ); check( !strcmp( "5368.32e-3", json_getValue( scientific ) ) ); - check( 5368.32e-3 == json_getReal( scientific ) ); + check( 5368.32e-3 == json_getReal( scientific ) ); + printf( "%s - %f\n", json_getValue( scientific ), json_getReal( scientific ) ); } @@ -191,7 +192,17 @@ int badformat( void ) { check( JSON_BOOLEAN == json_getType( var ) ); check( !strcmp( "true", json_getValue( var ) ) ); check( true == json_getBoolean( var ) ); - } + } + { + char str[] = "{\"var\":truep}"; + json_t const* json = json_create( str, pool, qty ); + check( !json ); + } + { + char str[] = "{\"var\":0s}"; + json_t const* json = json_create( str, pool, qty ); + check( !json ); + } done(); } diff --git a/tiny-json.c b/tiny-json.c index 97dc533..0324bc5 100644 --- a/tiny-json.c +++ b/tiny-json.c @@ -50,6 +50,7 @@ static json_t* _poolInit( jsonPool_t* pool ); static json_t* _poolNew( jsonPool_t* pool ); static char* _objValue( char* ptr, json_t* obj, jsonPool_t* pool ); static char* _setToNull( char* ch ); +static bool _isEndOfPrimitive( char ch ); /* Parse a string to get a json. */ json_t const* json_create( char* str, json_t mem[], unsigned int qty ) { @@ -191,7 +192,7 @@ static char* _checkStr( char* ptr, char const* str ) { * @retval Null pointer if any error occur. */ static char* _primitiveValue( char* ptr, json_t* property, char const* value, jsonType_t type ) { ptr = _checkStr( ptr, value ); - if ( !ptr ) return 0; + if ( !ptr || !_isEndOfPrimitive( *ptr ) ) return 0; ptr = _setToNull( ptr ); property->type = type; return ptr; @@ -233,7 +234,7 @@ static char* _expValue( char* ptr, json_t* property ) { if ( *ptr == '-' || *ptr == '+' ) ++ptr; if ( !_isNum( *ptr ) ) return 0; ptr = _goNum( ++ptr ); - property->type = JSON_SCIENTIFIC; + property->type = JSON_REAL; return ptr; } @@ -249,8 +250,7 @@ static char* _fraqValue( char* ptr, json_t* property ) { /** Parser a string to get a numerial value. * If the first character after the value is diferent of '}' or ']' is set to '\0'. * @param str Pointer to first character. - * @param property Property handler to set the value and the type: - * JSON_REAL, JSON_SCIENTIFIC or JSON_INTEGER. + * @param property Property handler to set the value and the type: JSON_REAL or JSON_INTEGER. * @retval Pointer to first non white space after the string. If success. * @retval Null pointer if any error occur. */ static char* _numValue( char* ptr, json_t* property ) { @@ -268,7 +268,9 @@ static char* _numValue( char* ptr, json_t* property ) { ptr = _expValue( ++ptr, property ); if ( !ptr ) return 0; } - else property->type = JSON_INTEGER; + else if ( _isEndOfPrimitive( *ptr ) ) + property->type = JSON_INTEGER; + else return 0; ptr = _setToNull( ptr ); return ptr; } @@ -388,11 +390,13 @@ static char* _goWhile( char* str, char const* set ) { return 0; } +static char const* const whitespace = " \n\r\t\f"; + /** 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. */ static char* _goWhiteSpace( char* str ) { - return _goWhile( str, " \n\r\t\f" ); + return _goWhile( str, whitespace ); } /** Checks if a character is a decimal digit. */ @@ -411,10 +415,16 @@ static char* _goNum( char* str ) { return 0; } +static char const* const endofblock = "}]"; + /** Set a char to '\0' and increase its pointer if the char is diferent to '}' or ']'. * @param ch Pointer to character. * @return Final value pointer. */ static char* _setToNull( char* ch ) { - if ( !_isOneOfThem( *ch, "}]" ) ) *ch++ = '\0'; + if ( !_isOneOfThem( *ch, endofblock ) ) *ch++ = '\0'; return ch; } + +static bool _isEndOfPrimitive( char ch ) { + return ch == ',' || _isOneOfThem( ch, whitespace ) || _isOneOfThem( ch, endofblock ); +} \ No newline at end of file diff --git a/tiny-json.h b/tiny-json.h index cb70bd8..20253ad 100644 --- a/tiny-json.h +++ b/tiny-json.h @@ -34,7 +34,7 @@ extern "C" { /** Enumeration of codes of suported JSON properties types. */ typedef enum { JSON_OBJ, JSON_ARRAY, JSON_TEXT, JSON_BOOLEAN, - JSON_INTEGER, JSON_REAL, JSON_SCIENTIFIC, JSON_NULL + JSON_INTEGER, JSON_REAL, JSON_NULL } jsonType_t; /** Structure to handle JSON properties. */