Last active
August 29, 2015 13:57
-
-
Save m1el/9471332 to your computer and use it in GitHub Desktop.
Wolfram Mathematica JSON form - convert expression to JSON representation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:: {Root[3 + #1^4 &, 2], "asd"} // JSONForm
:: ["List", ["Root", ["Function", ["Plus", 3, ["Power", ["Slot", 1], 4]]], 2, 0], ["String", "asd"]]