Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON export: avoid non-significant decimal digits in version field (fixes #3863) #3864

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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