1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix json)
21 #:use-module (srfi srfi-9)
22 #:export (define-json-mapping))
26 ;;; Helpers to map JSON objects to SRFI-9 records. Taken from (guix swh).
30 (define-syntax-rule (define-json-reader json->record ctor spec ...)
31 "Define JSON->RECORD as a procedure that converts a JSON representation,
32 read from a port, string, or hash table, into a record created by CTOR and
33 following SPEC, a series of field specifications."
34 (define (json->record input)
35 (let ((table (cond ((port? input)
38 (json-string->scm input))
39 ((or (null? input) (pair? input))
41 (let-syntax ((extract-field (syntax-rules ()
42 ((_ table (field key json->value))
43 (json->value (assoc-ref table key)))
44 ((_ table (field key))
45 (assoc-ref table key))
48 (symbol->string 'field))))))
49 (ctor (extract-field table spec) ...)))))
51 (define-syntax-rule (define-json-mapping rtd ctor pred json->record
52 (field getter spec ...) ...)
53 "Define RTD as a record type with the given FIELDs and GETTERs, à la SRFI-9,
54 and define JSON->RECORD as a conversion from JSON to a record of this type."
56 (define-record-type rtd
61 (define-json-reader json->record ctor
62 (field spec ...) ...)))