5
$\begingroup$

Update: This problem was fixed in Mathematica 11.3.


It seems that in version 10.0.2 and 10.1.0, floating point numbers are truncated to only 6 digits of precision when exporting to JSON.

Example:

ExportString[RandomReal[1, 5], "JSON"]
(* "[
    0.832049,
    0.355306,
    0.615313,
    0.332704,
    0.186309
]" *)

What is a quick workaround? I would expect about $MachinePrecision digits to be preserved.

Note: I have already reported this problem and support confirmed that it's a known issue. I'm posting here to ask for temporary workarounds or existing alternative JSON exporters.


Update:

After some spelunking, it seems that the functions doing the actual formatting are these:

Mathematica graphics

All the functions starting with c are implemented in C through LibraryLink, so I have no access to them. I can override cEncodeReal and cEncodeRealList, but that only fixes single reals or lists of all reals. All other expressions, including things such as {"a" -> {1.2, 2.3}} are fully handled by the opaque cEncodeExpr.

Reimplementing this function would mean pretty much reimplementing a complete JSON exporter in which case one might as well start from scratch and not use Export...

$\endgroup$
  • $\begingroup$ What if you export the InputForm[]? $\endgroup$ – J. M. is away Jun 12 '15 at 9:45
  • $\begingroup$ @Guesswhoitis. It fails, unfortunately. All formatting seems to be done in C. $\endgroup$ – Szabolcs Jun 12 '15 at 10:02
  • $\begingroup$ @Szabolcs, have you filed a ticket w/ support? $\endgroup$ – alancalvitti Apr 26 '18 at 17:05
  • $\begingroup$ @alancalvitti Yes, in June 2015, i.e. the same time when posting this question. At that time it was already a known issue, according to support. $\endgroup$ – Szabolcs Apr 26 '18 at 17:11
  • $\begingroup$ @Szabolcs, is it resolved as of 11.3? $\endgroup$ – alancalvitti Apr 26 '18 at 17:26
2
$\begingroup$

At least in version 11.0.1.0 it is possible to just set SetSystemOptions["MachineRealPrintPrecision"->Round[$MachinePrecision]] without modifying any files. Note that only integers seem to be allowed.

$\endgroup$
1
$\begingroup$

Here's a workaround until this gets fixed.

WARNING: Modifying installation files can break things. Do this at your own risk, and keep the modification in mind, in case you experience problems with JSON export later. Do this only if you are affected by the problem.

Open the file

SystemFiles`Links`JSONTools`JSONTools.m

Skip to line 233, where the number of digits to be printed can be set on:

optionsList[[PRINTPRECISION]] = ...

Set it to 16 to preserve approximately $MachinePrecision digits.

(With a bit more work, this could conceivably be done without modifying installation files, through the cSetOptions function defined in the same file. But I'm lazy and wanted a permanent solution anyway.)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.