@ -102,9 +102,13 @@ static bool jsondec_isvalue(const upb_FieldDef* f) {
jsondec_isnullvalue ( f ) ;
}
UPB_NORETURN static void jsondec_err ( jsondec * d , const char * msg ) {
static void jsondec_set errmsg ( jsondec * d , const char * msg ) {
upb_Status_SetErrorFormat ( d - > status , " Error parsing JSON @%d:%d: %s " , d - > line ,
( int ) ( d - > ptr - d - > line_begin ) , msg ) ;
}
UPB_NORETURN static void jsondec_err ( jsondec * d , const char * msg ) {
jsondec_seterrmsg ( d , msg ) ;
UPB_LONGJMP ( d - > err , 1 ) ;
}
@ -119,7 +123,9 @@ UPB_NORETURN static void jsondec_errf(jsondec* d, const char* fmt, ...) {
UPB_LONGJMP ( d - > err , 1 ) ;
}
static void jsondec_skipws ( jsondec * d ) {
// Advances d->ptr until the next non-whitespace character or to the end of
// the buffer.
static void jsondec_consumews ( jsondec * d ) {
while ( d - > ptr ! = d - > end ) {
switch ( * d - > ptr ) {
case ' \n ' :
@ -135,7 +141,16 @@ static void jsondec_skipws(jsondec* d) {
return ;
}
}
jsondec_err ( d , " Unexpected EOF " ) ;
}
// Advances d->ptr until the next non-whitespace character. Postcondition that
// d->ptr is pointing at a valid non-whitespace character (will err if end of
// buffer is reached).
static void jsondec_skipws ( jsondec * d ) {
jsondec_consumews ( d ) ;
if ( d - > ptr = = d - > end ) {
jsondec_err ( d , " Unexpected EOF " ) ;
}
}
static bool jsondec_tryparsech ( jsondec * d , char ch ) {
@ -1481,7 +1496,17 @@ static bool upb_JsonDecoder_Decode(jsondec* const d, upb_Message* const msg,
if ( UPB_SETJMP ( d - > err ) ) return false ;
jsondec_tomsg ( d , msg , m ) ;
return true ;
// Consume any trailing whitespace before checking if we read the entire
// input.
jsondec_consumews ( d ) ;
if ( d - > ptr = = d - > end ) {
return true ;
} else {
jsondec_seterrmsg ( d , " unexpected trailing characters " ) ;
return false ;
}
}
bool upb_JsonDecode ( const char * buf , size_t size , upb_Message * msg ,