Skip to content

Instantly share code, notes, and snippets.

@m1el
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m1el/9471332 to your computer and use it in GitHub Desktop.
Save m1el/9471332 to your computer and use it in GitHub Desktop.
Wolfram Mathematica JSON form - convert expression to JSON representation.
// Wolfram Mathematica JSON form - convert expression to JSON representation
SetAttributes[JSONForm, HoldAll];
SetAttributes[JSONForm`lispyI, SequenceHold];
SetAttributes[JSONForm`lispy, HoldAll];
JSONForm`lispyI[List, xs_] := Prepend[Map[JSONForm`lispy, xs], "List"];
JSONForm`lispyI[Symbol, xs_] := SymbolName[xs];
JSONForm`lispyI[String, xs_] := {"String", xs};
JSONForm`lispyI[Integer | Real, xs_] := xs;
JSONForm`lispyI[h_, t_] := Prepend[Map[JSONForm`lispy, Level[t, 1]], JSONForm`lispy[h]];
JSONForm`lispy[x_] := JSONForm`lispyI[Head[x], x];
JSONForm[x_] := ExportString[JSONForm`lispy[x], "JSON", Compact -> True];
@m1el
Copy link
Author

m1el commented Mar 10, 2014

:: {Root[3 + #1^4 &, 2], "asd"} // JSONForm
:: ["List", ["Root", ["Function", ["Plus", 3, ["Power", ["Slot", 1], 4]]], 2, 0], ["String", "asd"]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment