以下のように使える
(import (rnrs) (text json object-builder))
;; JSON string
(define json-string "{
\"Image\": {
\"Width\": 800,
\"Height\": 600,
\"Title\": \"View from 15th Floor\",
\"Thumbnail\": {
\"Url\": \"http://www.example.com/image/481989943\",
\"Height\": 125,
\"Width\": 100
},
\"Animated\" : false,
\"IDs\": [116, 943, 234, 38793]
}
}")
;; records represent JSON object
(define-record-type image-holder
(fields image))
(define-record-type image
(fields width height title thumbnail animated ids))
(define-record-type thumbnail
(fields url height width))
;; JSON -> Scheme object definition
(define builder (json-object-builder
(make-image-holder
("Image"
(make-image
"Width"
"Height"
"Title"
("Thumbnail"
(make-thumbnail
"Url"
"Height"
"Width"))
"Animated"
("IDs" (@ list)))))))
;; Scheme object -> JSON definition
(define serializer (json-object-serializer
(("Image" image-holder-image
(("Width" image-width)
("Height" image-height)
("Title" image-title)
("Thumbnail" image-thumbnail
(("Url" thumbnail-url)
("Height" thumbnail-height)
("Width" thumbnail-width)))
("Animated" image-animated)
("IDs" image-ids (->)))))))
(let ((image (json-string->object json-string builder)))
;; do some useful thing with image
;; ok I want JSON string
(object->json-string image serializer))
;; Formatted for convenience
;; -> {
;; "Image": {
;; "Width": 800,
;; "Height": 600,
;; "Title": "View from 15th Floor",
;; "Thumbnail": {
;; "Url": "http://www.example.com/image/481989943",
;; "Height": 125, "Width": 100
;; },
;; "Animated": false,
;; "IDs": [116, 943, 234, 38793]
;; }
;; }
リストとかベクタとかも扱えたりするので、まぁ要りそうな機能はざっくりあるかなぁという感じ。CLOSを使ってJavaのアノテーションよろしくゴテゴテした感じにしてもよかったんだけど、最近そういう書き方を避けてるのと、既存のレコード等も再利用可能にするためにこんな感じのデザイン。SXMLに対してのオブジェクト構築と似た感じになってるのはそっちもそんな感じのデザインで作ってあるから。
使い勝手は(当面のJSONが必要な場面がこれなので)JWTを実装しつつ見ていくことになるが、そこまで大きな変更はないだろうなぁと思ったり。
No comments:
Post a Comment