@@ -51,6 +51,12 @@ typedef union
51
51
( ( ( x ) == ' ' ) || ( ( x ) == '\t' ) || \
52
52
( ( x ) == '\n' ) || ( ( x ) == '\r' ) )
53
53
54
+ #define isOpenBracket_ ( x ) ( ( ( x ) == '{' ) || ( ( x ) == '[' ) )
55
+ #define isCloseBracket_ ( x ) ( ( ( x ) == '}' ) || ( ( x ) == ']' ) )
56
+ /* NB. The numeric values of the open and close bracket pairs differ by 2. */
57
+ #define isMatchingBracket_ ( x , y ) \
58
+ ( isOpenBracket_( x ) && isCloseBracket_( y ) && ( ( x ) == ( ( y ) - 2 ) ) )
59
+
54
60
/**
55
61
* @brief Advance buffer index beyond whitespace.
56
62
*
@@ -852,7 +858,7 @@ static bool_ skipSpaceAndComma( const char * buf,
852
858
i ++ ;
853
859
skipSpace ( buf , & i , max );
854
860
855
- if ( ( i < max ) && ( buf [ i ] != '}' ) && ( buf [ i ] != ']' ) )
861
+ if ( ( i < max ) && ! isCloseBracket_ ( buf [ i ] ) )
856
862
{
857
863
ret = true;
858
864
* start = i ;
@@ -903,7 +909,8 @@ static void skipArrayScalars( const char * buf,
903
909
*
904
910
* In JSON, objects consist of comma-separated key-value pairs.
905
911
* A key is always a string (a scalar) while a value may be a
906
- * scalar, an object, or an array.
912
+ * scalar, an object, or an array. A colon must appear between
913
+ * each key and value.
907
914
*
908
915
* @param[in] buf The buffer to parse.
909
916
* @param[in,out] start The index at which to begin.
@@ -916,6 +923,7 @@ static void skipObjectScalars( const char * buf,
916
923
size_t max )
917
924
{
918
925
size_t i ;
926
+ bool_ comma ;
919
927
920
928
assert ( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
921
929
@@ -938,18 +946,25 @@ static void skipObjectScalars( const char * buf,
938
946
i ++ ;
939
947
skipSpace ( buf , & i , max );
940
948
949
+ if ( ( i < max ) && isOpenBracket_ ( buf [ i ] ) )
950
+ {
951
+ * start = i ;
952
+ break ;
953
+ }
954
+
941
955
if ( skipAnyScalar ( buf , & i , max ) != true )
942
956
{
943
957
break ;
944
958
}
945
959
946
- if ( skipSpaceAndComma ( buf , & i , max ) != true )
960
+ comma = skipSpaceAndComma ( buf , & i , max );
961
+ * start = i ;
962
+
963
+ if ( comma != true )
947
964
{
948
965
break ;
949
966
}
950
967
}
951
-
952
- * start = i ;
953
968
}
954
969
955
970
/**
@@ -965,7 +980,7 @@ static void skipScalars( const char * buf,
965
980
size_t max ,
966
981
char mode )
967
982
{
968
- assert ( ( mode == '[' ) || ( mode == '{' ) );
983
+ assert ( isOpenBracket_ ( mode ) );
969
984
970
985
skipSpace ( buf , start , max );
971
986
@@ -1028,15 +1043,21 @@ static JSONStatus_t skipCollection( const char * buf,
1028
1043
}
1029
1044
1030
1045
stack [ depth ] = c ;
1046
+ skipScalars ( buf , & i , max , stack [ depth ] );
1031
1047
break ;
1032
1048
1033
1049
case '}' :
1034
1050
case ']' :
1035
1051
1036
- if ( depth > 0 )
1052
+ if ( ( depth > 0 ) && isMatchingBracket_ ( stack [ depth ], c ) )
1037
1053
{
1038
1054
depth -- ;
1039
- ( void ) skipSpaceAndComma ( buf , & i , max );
1055
+
1056
+ if ( skipSpaceAndComma ( buf , & i , max ) == true )
1057
+ {
1058
+ skipScalars ( buf , & i , max , stack [ depth ] );
1059
+ }
1060
+
1040
1061
break ;
1041
1062
}
1042
1063
@@ -1052,8 +1073,6 @@ static JSONStatus_t skipCollection( const char * buf,
1052
1073
{
1053
1074
break ;
1054
1075
}
1055
-
1056
- skipScalars ( buf , & i , max , stack [ depth ] );
1057
1076
}
1058
1077
1059
1078
if ( ret == JSONSuccess )
0 commit comments