Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#6291 closed defect (fixed)

libjson converts doubles to strings with low precision

Reported by: molnar Owned by: warmerdam
Priority: low Milestone: 2.1.0
Component: OGR_SF Version: svn-trunk
Severity: minor Keywords:


See ogr\ogrsf_frmts\geojson\libjson\json_object.c

json_object_double_to_json_string() uses "%f" as conversion format for snprintf(). This defaults to a precision of 6 digits, which can result in loss of precision for many doubles.

This situation has been improved in the most recent release of json-c (In case it's interesting, here is the commit: and the discussion:

For my own purposes, it is easy to patch around this issue by incorporating a few lines of code from v0.12 json-c. I just thought I'd bring it to the attention of the GDAL/OGR maintainers.

PS. In case it is of interest: this issue was discovered while round-tripping data to/from Elasticsearch.

Change History (3)

comment:1 by Even Rouault, 8 years ago

Milestone: 2.1.0
Resolution: fixed
Status: newclosed

trunk r32952 "GeoJSON: use '%.17g' formatting by default for floating-point numbers and add SIGNIFICANT_FIGURES layer creation option (#6291)"

comment:2 by Jukka Rahkonen, 8 years ago

I think that @goatbar has been fixing typos everywhere but I am not sure about comments. There is one in r32952 on line 2387:

# then it alos applies to coordinates

Comments in and drv_geojson.html claim:

If explicitly specified, and COORDINATE_PRECISION is not, this will also apply to coordinates

However, in ogrgeojsonwriter.cpp it is written:

  • If SIGNIFICANT_FIGURES is defined, it will override COORDINATE_PRECISION.

Last comment makes to think that SIGNIFICANT_FIGURES is overriding COORDINATE_PRECISION even if the latter had been explicitly set but I believe that it does not.

comment:3 by Even Rouault, 8 years ago

trunk r32960 "GeoJSON: fix description of behaviour in comment of OGR_G_ExportToJsonEx(), fix logic in json_object_new_coord() to make COORDINATE_PRECISION win over SIGNIFICANT_FIGURES, fix typo in test and extend test to test behaviour when both options are specified (follow up to #6291, thanks to JukkaR for review)"

Note: See TracTickets for help on using tickets.