json

JSON support for Koto.

from_string

|String| -> Value

Deserializes a string containing JSON data, returning a structured Koto value.

Example

data = r'
{
  "string": "O_o",
  "nested": {
    "number": -1.2
  },
  "entries": [
    {
      "foo": "bar"
    },
    {
      "foo": "baz"
    }
  ]
}'

result = json.from_string data
result.string
# -> O_o
result.nested.number
# -> -1.2
result.entries[0].foo
# -> bar
result.entries[1].foo
# -> baz

to_string

|Value| -> String

Returns a string containing the input value serialized as JSON data.

Example

data = 
  string: '>_>'
  nested:
    number: 99
  entries: (
    {foo: 'bar'},
    {foo: 'baz'},
  )

json.to_string data
# -> {
# ->   "string": ">_>",
# ->   "nested": {
# ->     "number": 99
# ->   },
# ->   "entries": [
# ->     {
# ->       "foo": "bar"
# ->     },
# ->     {
# ->       "foo": "baz"
# ->     }
# ->   ]
# -> }