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

toJSON -- encode Macaulay2 things as JSON data

Synopsis

Description

This method returns a string containing JSON data corresponding to the given Macaulay2 thing. If the Indent option is null (the default), then there are no newlines or indentation.

i1 : x = hashTable {"foo" => {1, 2, {pi, true, false, nil}}}

o1 = HashTable{"foo" => {1, 2, {pi, true, false, nil}}}

o1 : HashTable
i2 : toJSON x

o2 = {"foo": [1, 2, [3.14159265358979, true, false, null]]}

If the Indent option is an integer, then newlines are added between values of arrays and members of lists and the given integer determines the number of spaces to indent for each level of indentation.

i3 : toJSON(x, Indent => 2)

o3 = {
       "foo": [
         1,
         2,
         [
           3.14159265358979,
           true,
           false,
           null
         ]
       ]
     }

Alternatively, the Indent option can be a string corresponding to the indentation used for each level.

i4 : toJSON(x, Indent => "\t")

o4 = {
             "foo": [
                     1,
                     2,
                     [
                             3.14159265358979,
                             true,
                             false,
                             null
                     ]
             ]
     }

The ValueSeparator option determines the string to use between values in an array and members in an object. If it is null (the default), then the string will either be ", " or ",", depending on whether Indent is null or not, respectively. Otherwise, the given string is used.

i5 : toJSON(x, ValueSeparator => " , ")

o5 = {"foo": [1 , 2 , [3.14159265358979 , true , false , null]]}

The NameSeparator option determines the string to use after the name of an object member. By default, it is ": ".

i6 : toJSON(x, NameSeparator => " : ")

o6 = {"foo" : [1, 2, [3.14159265358979, true, false, null]]}

By default, the members of objects are given in arbitrary order. To sort them by name in lexicographic order as strings, use the Sort option.

i7 : toJSON(hashTable{"foo" => 1, "bar" => 2, "baz" => 3}, Sort => true)

o7 = {"bar": 2, "baz": 3, "foo": 1}

Ways to use toJSON :

For the programmer

The object toJSON is a method function with options.