From c3a8db6d6c7ab1abd78d9c1b5ba3de16842a65d1 Mon Sep 17 00:00:00 2001 From: Matt Bennett Date: Mon, 2 Jan 2017 20:28:07 +0000 Subject: [PATCH] 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 --- modules/core/src/persistence.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index ccdb135ec5..bf16c2f398 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -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 ); } }