Macaulay2 » Documentation
Packages » JSON :: fromJSON
next | previous | forward | backward | up | index | toc

fromJSON -- decode JSON data into Macaulay2 things

Synopsis

Description

The JSON data provided in the given string or file is parsed using the Parsing package with the context-free grammar specified by RFC 8259. The type of the return value will vary depending on the data.

Numbers will result in ZZ or RR objects, as appropriate.

i1 : fromJSON "2"

o1 = 2
i2 : fromJSON "2.71828"

o2 = 2.71828

o2 : RR (of precision 53)

Strings will result in strings.

i3 : fromJSON "\"Hello, world!\""

o3 = Hello, world!

JSON's true and false will result in the corresponding Macaulay2 booleans.

i4 : fromJSON "true"

o4 = true
i5 : fromJSON "false"

o5 = false

Due to the implementation of the Parsing package, null cannot be a return value, and so the symbol nil is returned when JSON's null is given.

i6 : fromJSON "null"

o6 = nil

o6 : Symbol

Objects will result in hash tables.

i7 : fromJSON "{\"foo\": 1, \"bar\": 2}"

o7 = HashTable{"bar" => 2}
               "foo" => 1

o7 : HashTable

Arrays will result in lists.

i8 : fromJSON "[1, 2, 3]"

o8 = {1, 2, 3}

o8 : List

The input may also be a file containing JSON data.

i9 : jsonFile = temporaryFileName() | ".json"

o9 = /tmp/M2-2518338-0/0.json
i10 : jsonFile << "[1, 2, 3]" << endl << close

o10 = /tmp/M2-2518338-0/0.json

o10 : File
i11 : fromJSON openIn jsonFile

o11 = {1, 2, 3}

o11 : List

Ways to use fromJSON :

For the programmer

The object fromJSON is a method function.