4
$\begingroup$

What's the difference between Developer`WriteRawJSONString[] and ExportString[]? I see it used a lot but don't see a difference:

enter image description here

$\endgroup$
3
  • 4
    $\begingroup$ You should compare "RawJSON" as the export format instead. temp = RandomReal[1, {400, 20, 20}]; Developer`WriteRawJSONString[temp] === ExportString[temp, "RawJSON"] returns True. $\endgroup$
    – Jason B.
    Commented Jan 3, 2017 at 21:38
  • $\begingroup$ Right, but what's the differences? $\endgroup$
    – user5601
    Commented Jan 3, 2017 at 22:12
  • 4
    $\begingroup$ Also, compare the Trace output from the two: TracePrint[ Developer`WriteRawJSONString[{1, 2, 3}], TraceInternal -> True ] versus the ExportString version, and it looks like ExportString performs a bunch of validity checks, loads the format, then ends up using functions from the Developer` package, notably Developer`WriteRawJSONStream $\endgroup$
    – Jason B.
    Commented Jan 3, 2017 at 22:12

1 Answer 1

4
$\begingroup$

There are some differences, but as user9490 mentioned, at some level they both might eventually use the same internal functionality. Here are the differences I found:

  • ExportString[_,"JSON"] will export list of rules and Associations as JSON-objects while ExportString[_,"RawJSON"] and WriteRawJSONString can't handle list of rules but only work with Associations as input.
  • there is a difference in how they treat non-ascii characters. ExportString[_,"RawJSON"] will handle that as WriteRawJSONString does. I don't fully understand the details but think that what WriteRawJSONString and ExportString[_,"RawJSON"] do looks more consistent than what ExportString[_,"JSON"] does.
  • ExportString is documented functionality while Developer`WriteRawJSONString is not documented and thus is more likely to be changed or to go away.
  • ExportString presumably does more validity checks
  • According to simple tests WriteRawJSONString has better performance than both ExportString[_,"JSON"|"RawJSON"].
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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