Merge pull request #7952 from mattmyne:JSONWriteFixTrailingDecimalPoint

Append zero to trailing decimal place for FileStorage JSON write of a float or double value (#7952)

* Fix for FileStorage JSON write of a float or double value that has no fractional part; appends a zero character after the trailing decimal place to meet JSON standard.

* strlen return to size_t type rather than unnecessary cast to int
This commit is contained in:
Matt Bennett 2017-01-02 20:28:07 +00:00 committed by Alexander Alekhin
parent 23e53a32e5
commit c3a8db6d6c

View File

@ -4025,7 +4025,14 @@ static void
icvJSONWriteReal( CvFileStorage* fs, const char* key, double value )
{
char buf[128];
icvJSONWrite( fs, key, icvDoubleToString( buf, value ));
size_t len = strlen( icvDoubleToString( buf, value ) );
if( len > 0 && buf[len-1] == '.' )
{
// append zero if string ends with decimal place to match JSON standard
buf[len] = '0';
buf[len+1] = '\0';
}
icvJSONWrite( fs, key, buf );
}
@ -4829,6 +4836,17 @@ cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt )
}
else
{
if( elem_type == CV_32F || elem_type == CV_64F )
{
size_t buf_len = strlen(ptr);
if( buf_len > 0 && ptr[buf_len-1] == '.' )
{
// append zero if CV_32F or CV_64F string ends with decimal place to match JSON standard
// ptr will point to buf, so can write to buf given ptr is const
buf[buf_len] = '0';
buf[buf_len+1] = '\0';
}
}
icvJSONWrite( fs, 0, ptr );
}
}