Skip to content

Commit

Permalink
Merge pull request #3866 from OSGeo/backport-3864-to-9.3
Browse files Browse the repository at this point in the history
[Backport 9.3] JSON export: avoid non-significant decimal digits in version field (fixes #3863)
  • Loading branch information
rouault authored Aug 29, 2023
2 parents 4f0376b + 847423f commit 08f394e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/iso19111/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5981,7 +5981,7 @@ IdentifierNNPtr JSONParser::buildId(const json &j, bool removeInverseOf) {
static_cast<int>(dblVersion) == dblVersion) {
version = internal::toString(static_cast<int>(dblVersion));
} else {
version = internal::toString(dblVersion);
version = internal::toString(dblVersion, /*precision=*/15);
}
} else {
throw ParsingException("Unexpected type for value of \"version\"");
Expand Down
22 changes: 9 additions & 13 deletions src/iso19111/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -1140,16 +1141,11 @@ void Identifier::_exportToJSON(JSONFormatter *formatter) const {

if (!l_version.empty()) {
writer->AddObjKey("version");
try {
const double dblVersion = c_locale_stod(l_version);
if (dblVersion >= std::numeric_limits<int>::min() &&
dblVersion <= std::numeric_limits<int>::max() &&
static_cast<int>(dblVersion) == dblVersion) {
writer->Add(static_cast<int>(dblVersion));
} else {
writer->Add(dblVersion);
}
} catch (const std::exception &) {
bool isDouble = false;
(void)c_locale_stod(l_version, isDouble);
if (isDouble) {
writer->AddUnquoted(l_version.c_str());
} else {
writer->Add(l_version);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/proj_json_streaming_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ void CPLJSonStreamingWriter::Add(const char *pszStr) {
Print(FormatString(pszStr));
}

void CPLJSonStreamingWriter::AddUnquoted(const char *pszStr) {
EmitCommaIfNeeded();
Print(pszStr);
}

void CPLJSonStreamingWriter::Add(GIntBig nVal) {
EmitCommaIfNeeded();
Print(CPLSPrintf(CPL_FRMT_GIB, nVal));
Expand Down
1 change: 1 addition & 0 deletions src/proj_json_streaming_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class CPL_DLL CPLJSonStreamingWriter {

void Add(const std::string &str);
void Add(const char *pszStr);
void AddUnquoted(const char *pszStr);
void Add(bool bVal);
void Add(int nVal) { Add(static_cast<GIntBig>(nVal)); }
void Add(unsigned int nVal) { Add(static_cast<GIntBig>(nVal)); }
Expand Down

0 comments on commit 08f394e

Please sign in to comment.