From 19cda6a061a8276baa4866bbfbf0fdaf74232d6d Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 28 Aug 2023 12:38:30 +0200 Subject: [PATCH] JSON export: avoid non-significant decimal digits in version field (fixes #3863) --- src/iso19111/metadata.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/iso19111/metadata.cpp b/src/iso19111/metadata.cpp index 1b4f499add..ffccf2e357 100644 --- a/src/iso19111/metadata.cpp +++ b/src/iso19111/metadata.cpp @@ -1088,10 +1088,11 @@ void Identifier::_exportToWKT(WKTFormatter *formatter) const { formatter->addQuotedString(l_code); } if (!l_version.empty()) { - try { - (void)c_locale_stod(l_version); + bool isDouble = false; + (void)c_locale_stod(l_version, isDouble); + if (isDouble) { formatter->add(l_version); - } catch (const std::exception &) { + } else { formatter->addQuotedString(l_version); } } @@ -1140,16 +1141,17 @@ void Identifier::_exportToJSON(JSONFormatter *formatter) const { if (!l_version.empty()) { writer->AddObjKey("version"); - try { - const double dblVersion = c_locale_stod(l_version); + bool isDouble = false; + const double dblVersion = c_locale_stod(l_version, isDouble); + if (isDouble) { if (dblVersion >= std::numeric_limits::min() && dblVersion <= std::numeric_limits::max() && static_cast(dblVersion) == dblVersion) { writer->Add(static_cast(dblVersion)); } else { - writer->Add(dblVersion); + writer->Add(dblVersion, /*precision=*/15); } - } catch (const std::exception &) { + } else { writer->Add(l_version); } }