Commit | Line | Data |
---|---|---|
76073d29 LC |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org> | |
3 | ;;; | |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
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. | |
10 | ;;; | |
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. | |
15 | ;;; | |
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/>. | |
18 | ||
19 | (define-module (guix json) | |
20 | #:use-module (json) | |
21 | #:use-module (srfi srfi-9) | |
22 | #:export (define-json-mapping)) | |
23 | ||
24 | ;;; Commentary: | |
25 | ;;; | |
26 | ;;; Helpers to map JSON objects to SRFI-9 records. Taken from (guix swh). | |
27 | ;;; | |
28 | ;;; Code: | |
29 | ||
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) | |
36 | (json->scm input)) | |
37 | ((string? input) | |
38 | (json-string->scm input)) | |
39 | ((or (null? input) (pair? input)) | |
40 | 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)) | |
46 | ((_ table (field)) | |
47 | (assoc-ref table | |
48 | (symbol->string 'field)))))) | |
49 | (ctor (extract-field table spec) ...))))) | |
50 | ||
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." | |
55 | (begin | |
56 | (define-record-type rtd | |
57 | (ctor field ...) | |
58 | pred | |
59 | (field getter) ...) | |
60 | ||
61 | (define-json-reader json->record ctor | |
62 | (field spec ...) ...))) |