json是一个 Igropyr 官方开发的JSON库。
install: raven install json
use: (import (json json))
string->json
(string->json string)
"{key1: value1, key2: value2, key3: value3 ...}" => ((key1 . value2)(key2 . value2)(key3 . value3) ...)
"[value1, value2, value3 ...]" => #(value1 value2 value3 ...)
json->string
(json->string json)
((key1 . value2)(key2 . value2)(key3 . value3) ...) => "{key1: value1, key2: value2, key3: value3 ...}"
#(value1 value2 value3 ...) => "[value1, value2, value3 ...]"
vector->array
(vector->array vector)
#(value1 value2 value3 ...) => ((0 . value1)(1 . value2)(2 . value3) ...)
array->vector
(vector->array list)
((0 . value1)(1 . value2)(2 . value3) ...) => #(value1 value2 value3 ...)
json-ref
(json-ref json key)
(json-ref json key1 key2 key3 ...)
用来返回任意层数json结构内某键的值。
when value is: json-ref return:
true => #t
false => #f
null => '()
当它接受多个参数时:
(json-ref json key1 key2 key3) =
(json-ref (json-ref (json-ref json key1) key2) key3)))
以下的过程内置高阶函数map:
- 它们能同时通过多层的过滤器对多分支进行操作。
- "Verify" 可以是一个键,布尔 #t 或一个过程.
- 当它是一个键时,表示当前层只通过适配键的分支。
- 当它是一个布尔#t时,表示适配当前层所有分支。
- 当它是一个过程时,这个过程作为当前层分支过滤器。
- 这个过程必须接受一个参数(key)并返回布尔值。
- 当这些过程接受多个参数时,一个参数表示一层的过滤器。
json-set
(json-set json verify value)
(json-set json verify1 verify2 verify3 ... value)
用于设置任意层数JSON结构中某一键的值.
"value" 可以是一个值或一个过程。
当它是一个值时,这个值被设置成对应键的值。
当它是一个过程时,这个过程接受对应键当前的值作为参数并返回设置的值。
不要将结构中的值设为#t,#f 和 '(),而应该用 'true, 'false 和 'null。
json-push
(json-push json key value)
(json-push json verify1 verify2 verify3 ... key value)
用于将键值对增加到任意层数的JSON结构中。
参数中比须含有至少一个键值对。
json-drop
(json-drop json verify)
(json-drop json verify1 verify2 verify3 ...)
用于在将任意层数的JSON结构中的键值对删除。
最后一个参数只接受一个键或一个过程。
json-reduce
(json-set json verify procedure)
(json-set json verify1 verify2 verify3 ... procedure)
类似于json-set,但最后一个参数只接受过程。
这个过程接受两个参数,到达此位置所历经的所有键的list:'(key1 key2 key3 ...) 和这个键的当前值。
这个过程必须返回一个值用来替代当前值。
需要注意的是,这里键的list不是过滤器的list。比如说,当使用#t来通过一层的所有分支时,这里返回的键是到达当前分支键值对通过这一层所使用的键,而不是当前层所有键的列表。