guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

guile-json: simple alist to json


From: Jan Nieuwenhuizen
Subject: guile-json: simple alist to json
Date: Thu, 18 Feb 2016 23:21:37 +0100

Hi Aleix,

Attached are two patches to allow converting simple alists
to json, this removes the need for strings or hash-tables

    scheme@(json)> (scm->json-string '((a . 1) (b . 2)))
    $2 = "{\"a\" : 1,\"b\" : 2}"

Greetings, Jan

>From 1faa02421076eeb7d680c6d189dab0cd8c272f3b Mon Sep 17 00:00:00 2001
From: Jan Nieuwenhuizen <address@hidden>
Date: Wed, 5 Aug 2015 08:21:13 +0200
Subject: [PATCH 1/2] builder: convert symbols to string.

* json/builder.scm (->string): New function.
* json/builder.scm (json-build-string): Use it.
---
 json/builder.scm | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/json/builder.scm b/json/builder.scm
index c725b19..a4210bc 100644
--- a/json/builder.scm
+++ b/json/builder.scm
@@ -1,6 +1,7 @@
 ;;; (json builder) --- Guile JSON implementation.
 
 ;; Copyright (C) 2013 Aleix Conchillo Flaque <address@hidden>
+;; Copyright (C) 2015 Jan Nieuwenhuizen <address@hidden>
 ;;
 ;; This file is part of guile-json.
 ;;
@@ -105,6 +106,8 @@
       (display (number->string (exact->inexact scm)) port)
       (display (number->string scm) port)))
 
+(define (->string x) (if (symbol? x) (symbol->string x) x))
+
 (define (json-build-string scm port escape)
   (display "\"" port)
   (display
@@ -121,7 +124,7 @@
                      ((#\ht) '(#\\ #\t))
                      ((#\/) (if escape `(#\\ ,c) (list c)))
                      (else (string->list (build-char-string c)))))
-                 (string->list scm))))
+                 (string->list (->string scm)))))
    port)
   (display "\"" port))
 
@@ -155,6 +158,7 @@
    ((eq? scm #nil) (json-build-null port))
    ((boolean? scm) (json-build-boolean scm port))
    ((number? scm) (json-build-number scm port))
+   ((symbol? scm) (json-build-string (symbol->string scm) port escape))
    ((string? scm) (json-build-string scm port escape))
    ((list? scm) (json-build-array scm port escape pretty level))
    ((hash-table? scm) (json-build-object scm port escape pretty level))
-- 
2.6.3

>From d1f33330840acf6f5421d6d04a0f8be61c2108d7 Mon Sep 17 00:00:00 2001
From: Jan Nieuwenhuizen <address@hidden>
Date: Sat, 13 Feb 2016 21:16:46 +0100
Subject: [PATCH 2/2] builder: build objects from alists.

* json/builder.scm (atom?): New function.
* json/builder.scm (json-alist?): New function.
* json/builder.scm (json-build-object): SCM is an alist.
* json/builder.scm (json-build): Use it to support building objects from both
  hash-tables and alists.
---
 json/builder.scm | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/json/builder.scm b/json/builder.scm
index a4210bc..6b33008 100644
--- a/json/builder.scm
+++ b/json/builder.scm
@@ -1,7 +1,7 @@
 ;;; (json builder) --- Guile JSON implementation.
 
 ;; Copyright (C) 2013 Aleix Conchillo Flaque <address@hidden>
-;; Copyright (C) 2015 Jan Nieuwenhuizen <address@hidden>
+;; Copyright (C) 2015,2016 Jan Nieuwenhuizen <address@hidden>
 ;;
 ;; This file is part of guile-json.
 ;;
@@ -108,6 +108,16 @@
 
 (define (->string x) (if (symbol? x) (symbol->string x) x))
 
+(define (atom? x) (or (char? x) (number? x) (string? x) (symbol x)))
+
+(define (json-alist? x)
+  (and (pair? x)
+       (let loop ((x x))
+         (or (null? x)
+             (null? (car x))
+             (and (pair? (car x)) (atom? (caar x))
+                  (loop (cdr x)))))))
+
 (define (json-build-string scm port escape)
   (display "\"" port)
   (display
@@ -142,7 +152,7 @@
   (build-newline port pretty)
   (simple-format port "~A{" (indent-string pretty level))
   (build-newline port pretty)
-  (let ((pairs (hash-map->list cons scm)))
+  (let ((pairs scm))
     (unless (null? pairs)
       (build-object-pair (car pairs) port escape pretty (+ level 1))
       (for-each (lambda (p)
@@ -160,8 +170,10 @@
    ((number? scm) (json-build-number scm port))
    ((symbol? scm) (json-build-string (symbol->string scm) port escape))
    ((string? scm) (json-build-string scm port escape))
+   ((json-alist? scm) (json-build-object scm port escape pretty level))
    ((list? scm) (json-build-array scm port escape pretty level))
-   ((hash-table? scm) (json-build-object scm port escape pretty level))
+   ((hash-table? scm)
+    (json-build-object (hash-map->list cons scm) port escape pretty level))
    (else (throw 'json-invalid))))
 
 ;;
-- 
2.6.3

-- 
Jan Nieuwenhuizen <address@hidden> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | Avatar®  http://AvatarAcademy.nl  

reply via email to

[Prev in Thread] Current Thread [Next in Thread]