Skip to content

Commit 08f394e

Browse files
authored
Merge pull request OSGeo#3866 from OSGeo/backport-3864-to-9.3
[Backport 9.3] JSON export: avoid non-significant decimal digits in version field (fixes OSGeo#3863)
2 parents 4f0376b + 847423f commit 08f394e

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

src/iso19111/io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5981,7 +5981,7 @@ IdentifierNNPtr JSONParser::buildId(const json &j, bool removeInverseOf) {
59815981
static_cast<int>(dblVersion) == dblVersion) {
59825982
version = internal::toString(static_cast<int>(dblVersion));
59835983
} else {
5984-
version = internal::toString(dblVersion);
5984+
version = internal::toString(dblVersion, /*precision=*/15);
59855985
}
59865986
} else {
59875987
throw ParsingException("Unexpected type for value of \"version\"");

src/iso19111/metadata.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,10 +1088,11 @@ void Identifier::_exportToWKT(WKTFormatter *formatter) const {
10881088
formatter->addQuotedString(l_code);
10891089
}
10901090
if (!l_version.empty()) {
1091-
try {
1092-
(void)c_locale_stod(l_version);
1091+
bool isDouble = false;
1092+
(void)c_locale_stod(l_version, isDouble);
1093+
if (isDouble) {
10931094
formatter->add(l_version);
1094-
} catch (const std::exception &) {
1095+
} else {
10951096
formatter->addQuotedString(l_version);
10961097
}
10971098
}
@@ -1140,16 +1141,11 @@ void Identifier::_exportToJSON(JSONFormatter *formatter) const {
11401141

11411142
if (!l_version.empty()) {
11421143
writer->AddObjKey("version");
1143-
try {
1144-
const double dblVersion = c_locale_stod(l_version);
1145-
if (dblVersion >= std::numeric_limits<int>::min() &&
1146-
dblVersion <= std::numeric_limits<int>::max() &&
1147-
static_cast<int>(dblVersion) == dblVersion) {
1148-
writer->Add(static_cast<int>(dblVersion));
1149-
} else {
1150-
writer->Add(dblVersion);
1151-
}
1152-
} catch (const std::exception &) {
1144+
bool isDouble = false;
1145+
(void)c_locale_stod(l_version, isDouble);
1146+
if (isDouble) {
1147+
writer->AddUnquoted(l_version.c_str());
1148+
} else {
11531149
writer->Add(l_version);
11541150
}
11551151
}

src/proj_json_streaming_writer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ void CPLJSonStreamingWriter::Add(const char *pszStr) {
219219
Print(FormatString(pszStr));
220220
}
221221

222+
void CPLJSonStreamingWriter::AddUnquoted(const char *pszStr) {
223+
EmitCommaIfNeeded();
224+
Print(pszStr);
225+
}
226+
222227
void CPLJSonStreamingWriter::Add(GIntBig nVal) {
223228
EmitCommaIfNeeded();
224229
Print(CPLSPrintf(CPL_FRMT_GIB, nVal));

src/proj_json_streaming_writer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class CPL_DLL CPLJSonStreamingWriter {
8686

8787
void Add(const std::string &str);
8888
void Add(const char *pszStr);
89+
void AddUnquoted(const char *pszStr);
8990
void Add(bool bVal);
9091
void Add(int nVal) { Add(static_cast<GIntBig>(nVal)); }
9192
void Add(unsigned int nVal) { Add(static_cast<GIntBig>(nVal)); }

0 commit comments

Comments
 (0)