Skip to content

Commit

Permalink
Update code as per review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Aggarwal <[email protected]>
  • Loading branch information
aggarg committed Jul 19, 2024
1 parent d0130a2 commit bfb794c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
35 changes: 21 additions & 14 deletions source/core_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ typedef union
#define isMatchingBracket_( x, y ) ( isCurlyPair_( x, y ) || isSquarePair_( x, y ) )
#define isSquareOpen_( x ) ( ( x ) == '[' )
#define isSquareClose_( x ) ( ( x ) == ']' )
#define isCurlyOpen_( x ) ( ( x ) == '{' )
#define isCurlyClose_( x ) ( ( x ) == '}' )

/**
* @brief Advance buffer index beyond whitespace.
Expand Down Expand Up @@ -980,14 +982,13 @@ static bool skipObjectScalars( const char * buf,
size_t * start,
size_t max )
{
size_t i = 0U, origStart = 0U;
size_t i = 0U;
bool comma = false;
bool ret = true;

coreJSON_ASSERT( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

i = *start;
origStart = *start;

while( i < max )
{
Expand Down Expand Up @@ -1029,12 +1030,6 @@ static bool skipObjectScalars( const char * buf,
}
}

/* An empty JSON object is valid. */
if( i == origStart )
{
ret = true;
}

return ret;
}

Expand All @@ -1054,6 +1049,7 @@ static bool skipScalars( const char * buf,
size_t max,
char mode )
{
size_t i = 0U;
bool modeIsOpenBracket = ( bool ) isOpenBracket_( mode );
bool ret = true;

Expand All @@ -1067,13 +1063,24 @@ static bool skipScalars( const char * buf,

skipSpace( buf, start, max );

if( mode == '[' )
{
skipArrayScalars( buf, start, max );
}
else
i = *start;

if( i < max )
{
ret = skipObjectScalars( buf, start, max );
if( mode == '[' )
{
if( !isSquareClose_( buf[ i ] ) )
{
skipArrayScalars( buf, start, max );
}
}
else
{
if( !isCurlyClose_( buf[ i ] ) )
{
ret = skipObjectScalars( buf, start, max );
}
}
}

return ret;
Expand Down
7 changes: 7 additions & 0 deletions test/unit-test/core_json_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
#define JSON_DOC_LEGAL_EMPTY_OBJECT "{\"foo\":{}}"
#define JSON_DOC_LEGAL_EMPTY_OBJECT_LENGTH ( sizeof( JSON_DOC_LEGAL_EMPTY_OBJECT ) - 1 )

#define JSON_DOC_LEGAL_EMPTY_ARRAY "{\"foo\":[]}"
#define JSON_DOC_LEGAL_EMPTY_ARRAY_LENGTH ( sizeof( JSON_DOC_LEGAL_EMPTY_ARRAY ) - 1 )

/* A single scalar is still considered a valid JSON document. */
#define SINGLE_SCALAR "\"l33t\""
#define SINGLE_SCALAR_LENGTH ( sizeof( SINGLE_SCALAR ) - 1 )
Expand Down Expand Up @@ -571,6 +574,10 @@ void test_JSON_Validate_Legal_Documents( void )
JSON_DOC_LEGAL_EMPTY_OBJECT_LENGTH );
TEST_ASSERT_EQUAL( JSONSuccess, jsonStatus );

jsonStatus = JSON_Validate( JSON_DOC_LEGAL_EMPTY_ARRAY,
JSON_DOC_LEGAL_EMPTY_ARRAY_LENGTH );
TEST_ASSERT_EQUAL( JSONSuccess, jsonStatus );

jsonStatus = JSON_Validate( JSON_DOC_MULTIPLE_VALID_ESCAPES,
JSON_DOC_MULTIPLE_VALID_ESCAPES_LENGTH );
TEST_ASSERT_EQUAL( JSONSuccess, jsonStatus );
Expand Down

0 comments on commit bfb794c

Please sign in to comment.