4

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

M.R.
  • 31,425
  • 8
  • 90
  • 281
  • 4
    You should compare "RawJSON" as the export format instead. temp = RandomReal[1, {400, 20, 20}]; Developer`WriteRawJSONString[temp] === ExportString[temp, "RawJSON"] returns True. – Jason B. Jan 03 '17 at 21:38
  • Right, but what's the differences? – user5601 Jan 03 '17 at 22:12
  • 4
    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 – Jason B. Jan 03 '17 at 22:12

1 Answers1

4

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"].
Albert Retey
  • 23,585
  • 60
  • 104