Skip to content

Commit

Permalink
Treat empty JSON object as valid
Browse files Browse the repository at this point in the history
Without this change, the following valid JSON document will be evaluated
as invalid:
```
{
    "foo": {}
}
```

This issue was reported here - #168

Signed-off-by: Gaurav Aggarwal <[email protected]>
  • Loading branch information
aggarg committed Jul 18, 2024
1 parent 2bb6294 commit 170d87c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion source/core_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -980,13 +980,14 @@ static bool skipObjectScalars( const char * buf,
size_t * start,
size_t max )
{
size_t i = 0U;
size_t i = 0U, origStart = 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 @@ -1028,6 +1029,12 @@ static bool skipObjectScalars( const char * buf,
}
}

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

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 @@ -146,6 +146,9 @@
"\":{\"" SECOND_QUERY_KEY "\" : \"" COMPLETE_QUERY_KEY_ANSWER "\"}} "
#define JSON_DOC_LEGAL_TRAILING_SPACE_LENGTH ( sizeof( JSON_DOC_LEGAL_TRAILING_SPACE ) - 1 )

#define JSON_DOC_LEGAL_EMPTY_OBJECT "{\"foo\":{}}"
#define JSON_DOC_LEGAL_EMPTY_OBJECT_LENGTH ( sizeof( JSON_DOC_LEGAL_EMPTY_OBJECT ) - 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 @@ -564,6 +567,10 @@ void test_JSON_Validate_Legal_Documents( void )
JSON_DOC_LEGAL_TRAILING_SPACE_LENGTH );
TEST_ASSERT_EQUAL( JSONSuccess, jsonStatus );

jsonStatus = JSON_Validate( JSON_DOC_LEGAL_EMPTY_OBJECT,
JSON_DOC_LEGAL_EMPTY_OBJECT_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 170d87c

Please sign in to comment.