Remove unnecessary JSON type.

pull/1/head
rafagafe 8 years ago
parent 2fc47a5ab5
commit baf48cb37b
  1. 15
      tests.c
  2. 24
      tiny-json.c
  3. 2
      tiny-json.h

@ -59,12 +59,12 @@ static int primitive( void ) {
unsigned const qty = sizeof pool / sizeof *pool; unsigned const qty = sizeof pool / sizeof *pool;
{ {
char str[] = "{" char str[] = "{"
"\"real\": -0.004,"
"\"boolvar0\": false," "\"boolvar0\": false,"
"\"boolvar1\": true," "\"boolvar1\": true,"
"\"nullvar\": null," "\"nullvar\": null,"
"\"max\": 9223372036854775807," "\"max\": 9223372036854775807,"
"\"min\": -9223372036854775808," "\"min\": -9223372036854775808,"
"\"real\": -0.004,"
"\"scientific\": 5368.32e-3," "\"scientific\": 5368.32e-3,"
"}"; "}";
@ -108,9 +108,10 @@ static int primitive( void ) {
json_t const* scientific = json_getProperty( json, "scientific" ); json_t const* scientific = json_getProperty( json, "scientific" );
check( scientific ); check( scientific );
check( JSON_SCIENTIFIC == json_getType( scientific ) ); check( JSON_REAL == json_getType( scientific ) );
check( !strcmp( "5368.32e-3", json_getValue( 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 ) );
} }
@ -192,6 +193,16 @@ int badformat( void ) {
check( !strcmp( "true", json_getValue( var ) ) ); check( !strcmp( "true", json_getValue( var ) ) );
check( true == json_getBoolean( 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(); done();
} }

@ -50,6 +50,7 @@ static json_t* _poolInit( jsonPool_t* pool );
static json_t* _poolNew( jsonPool_t* pool ); static json_t* _poolNew( jsonPool_t* pool );
static char* _objValue( char* ptr, json_t* obj, jsonPool_t* pool ); static char* _objValue( char* ptr, json_t* obj, jsonPool_t* pool );
static char* _setToNull( char* ch ); static char* _setToNull( char* ch );
static bool _isEndOfPrimitive( char ch );
/* Parse a string to get a json. */ /* Parse a string to get a json. */
json_t const* json_create( char* str, json_t mem[], unsigned int qty ) { 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. */ * @retval Null pointer if any error occur. */
static char* _primitiveValue( char* ptr, json_t* property, char const* value, jsonType_t type ) { static char* _primitiveValue( char* ptr, json_t* property, char const* value, jsonType_t type ) {
ptr = _checkStr( ptr, value ); ptr = _checkStr( ptr, value );
if ( !ptr ) return 0; if ( !ptr || !_isEndOfPrimitive( *ptr ) ) return 0;
ptr = _setToNull( ptr ); ptr = _setToNull( ptr );
property->type = type; property->type = type;
return ptr; return ptr;
@ -233,7 +234,7 @@ static char* _expValue( char* ptr, json_t* property ) {
if ( *ptr == '-' || *ptr == '+' ) ++ptr; if ( *ptr == '-' || *ptr == '+' ) ++ptr;
if ( !_isNum( *ptr ) ) return 0; if ( !_isNum( *ptr ) ) return 0;
ptr = _goNum( ++ptr ); ptr = _goNum( ++ptr );
property->type = JSON_SCIENTIFIC; property->type = JSON_REAL;
return ptr; return ptr;
} }
@ -249,8 +250,7 @@ static char* _fraqValue( char* ptr, json_t* property ) {
/** Parser a string to get a numerial value. /** Parser a string to get a numerial value.
* If the first character after the value is diferent of '}' or ']' is set to '\0'. * If the first character after the value is diferent of '}' or ']' is set to '\0'.
* @param str Pointer to first character. * @param str Pointer to first character.
* @param property Property handler to set the value and the type: * @param property Property handler to set the value and the type: JSON_REAL or JSON_INTEGER.
* JSON_REAL, JSON_SCIENTIFIC or JSON_INTEGER.
* @retval Pointer to first non white space after the string. If success. * @retval Pointer to first non white space after the string. If success.
* @retval Null pointer if any error occur. */ * @retval Null pointer if any error occur. */
static char* _numValue( char* ptr, json_t* property ) { static char* _numValue( char* ptr, json_t* property ) {
@ -268,7 +268,9 @@ static char* _numValue( char* ptr, json_t* property ) {
ptr = _expValue( ++ptr, property ); ptr = _expValue( ++ptr, property );
if ( !ptr ) return 0; if ( !ptr ) return 0;
} }
else property->type = JSON_INTEGER; else if ( _isEndOfPrimitive( *ptr ) )
property->type = JSON_INTEGER;
else return 0;
ptr = _setToNull( ptr ); ptr = _setToNull( ptr );
return ptr; return ptr;
} }
@ -388,11 +390,13 @@ static char* _goWhile( char* str, char const* set ) {
return 0; return 0;
} }
static char const* const whitespace = " \n\r\t\f";
/** Increases a pointer while it points to a white space character. /** Increases a pointer while it points to a white space character.
* @param str The initial pointer value. * @param str The initial pointer value.
* @return The final pointer value or null pointer if the null character was found. */ * @return The final pointer value or null pointer if the null character was found. */
static char* _goWhiteSpace( char* str ) { static char* _goWhiteSpace( char* str ) {
return _goWhile( str, " \n\r\t\f" ); return _goWhile( str, whitespace );
} }
/** Checks if a character is a decimal digit. */ /** Checks if a character is a decimal digit. */
@ -411,10 +415,16 @@ static char* _goNum( char* str ) {
return 0; return 0;
} }
static char const* const endofblock = "}]";
/** Set a char to '\0' and increase its pointer if the char is diferent to '}' or ']'. /** Set a char to '\0' and increase its pointer if the char is diferent to '}' or ']'.
* @param ch Pointer to character. * @param ch Pointer to character.
* @return Final value pointer. */ * @return Final value pointer. */
static char* _setToNull( char* ch ) { static char* _setToNull( char* ch ) {
if ( !_isOneOfThem( *ch, "}]" ) ) *ch++ = '\0'; if ( !_isOneOfThem( *ch, endofblock ) ) *ch++ = '\0';
return ch; return ch;
} }
static bool _isEndOfPrimitive( char ch ) {
return ch == ',' || _isOneOfThem( ch, whitespace ) || _isOneOfThem( ch, endofblock );
}

@ -34,7 +34,7 @@ extern "C" {
/** Enumeration of codes of suported JSON properties types. */ /** Enumeration of codes of suported JSON properties types. */
typedef enum { typedef enum {
JSON_OBJ, JSON_ARRAY, JSON_TEXT, JSON_BOOLEAN, JSON_OBJ, JSON_ARRAY, JSON_TEXT, JSON_BOOLEAN,
JSON_INTEGER, JSON_REAL, JSON_SCIENTIFIC, JSON_NULL JSON_INTEGER, JSON_REAL, JSON_NULL
} jsonType_t; } jsonType_t;
/** Structure to handle JSON properties. */ /** Structure to handle JSON properties. */

Loading…
Cancel
Save