ArkScript
A small, fast, functional and scripting language for video games
JSON module

json:open

Open a file and read its content as JSON, then return it.

Parameters

  • filename: path to JSON file

Return value UserType: json value

Author

Example

(let json (json:open "test.json"))

json:fromString

Takes a String as an argument, representing a JSON object, and return it parsed as a jsonObject.

Parameters

  • jsonObject: JSON stored in a string

Return value UserType: json value

Author

Example

(let json (json:fromString "{\"hello\": \"world\", \"key\": 12}"))

json:get

Retrieve a value from a jsonObject by its key.

Parameters

  • jsonObject: JSON object
  • key: a string if the object is a dictionnary, a number if it's an array

Return value the stored value

Author

Example

(let json (json:fromString "{\"hello\": \"world\", \"key\": 12}"))
(print (json:get json "hello")) # world

json:toString

Takes a jsonObject and transforms it into a String.

Parameters

  • jsonObject: JSON object

Return value String: the string representation of the object

Author

Example

(let json (json:fromString "{\"hello\": \"world\", \"key\": 12}"))
(print (json:toString json)) # {"hello": "world", "key": 12}

json:set

Modify a value in a jsonObject, given a key (String) and a new value.

Parameters

  • jsonObject: JSON object
  • key: a string
  • value: any type

Return value nil

Author

Example

(let json (json:fromString "{\"hello\": \"world\", \"key\": 12}"))
(json:set json "hello" 14)
(print json) # {"hello": 14, "key": 12}

json:fromList

Take a list of an even number of values, the even ones are the keys (String) and the odd ones the values, and build a jsonObject from it.

Parameters

  • jsonAsList: JSON stored in an ArkScript list

Return value UserType: json value

Author

Example

(let json (json:fromList [
"key" "value"
"keybis" 12
"this_is_an_array" [1 2 3]
"bool" true
"subobject" (json:fromList [
"a" 1
"b" 2
])
]))

json:write

Take a jsonObject and a filename (String), and write the jsonObject to the file. The file will be created if it doesn't exist, otherwise all the previous content will be wiped before writting to it.

Parameters

  • jsonObject: JSON object
  • filename: a string

Return value nil

Author

Example

(let json (json:fromList [
"key" "value"
"keybis" 12
"this_is_an_array" [1 2 3]
"bool" true
"subobject" (json:fromList [
"a" 1
"b" 2
])
]))
(json:write json "filename.json")

json:len

Parameters

  • jsonObject: JSON object

Return value Number: size of the given JSON object

Author

Example

(let json (json:fromList [
"key" "value"
"keybis" 12
"this_is_an_array" [1 2 3]
"bool" true
"subobject" (json:fromList [
"a" 1
"b" 2
])
]))
(json:len json) # 5