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

fromJSON -- decode JSON data into Macaulay2 things

Description

The JSON data provided in the given string or file is parsed using the Jansson library. 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

JSON's null will result in Macaulay2's null.

i6 : fromJSON "null" === null

o6 = true

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-34717-0/0.json
i10 : jsonFile << "[1, 2, 3]" << endl << close

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

o10 : File
i11 : fromJSON openIn jsonFile

o11 = {1, 2, 3}

o11 : List

Ways to use fromJSON:

  • fromJSON(File)
  • fromJSON(String)

For the programmer

The object fromJSON is a method function.


The source of this document is in /build/reproducible-path/macaulay2-1.26.05+ds/M2/Macaulay2/packages/JSON.m2:385:0.