* guile-json: simple alist to json
@ 2016-02-18 22:21 Jan Nieuwenhuizen
2016-02-23 5:51 ` Aleix Conchillo Flaqué
0 siblings, 1 reply; 4+ messages in thread
From: Jan Nieuwenhuizen @ 2016-02-18 22:21 UTC (permalink / raw)
To: Aleix Conchillo Flaque; +Cc: guile-devel
[-- Attachment #1: Type: text/plain, Size: 236 bytes --]
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
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-builder-convert-symbols-to-string.patch --]
[-- Type: text/x-diff, Size: 1781 bytes --]
From 1faa02421076eeb7d680c6d189dab0cd8c272f3b Mon Sep 17 00:00:00 2001
From: Jan Nieuwenhuizen <janneke@gnu.org>
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 <aconchillo@gmail.com>
+;; Copyright (C) 2015 Jan Nieuwenhuizen <janneke@gnu.org>
;;
;; 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
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-builder-build-objects-from-alists.patch --]
[-- Type: text/x-diff, Size: 2338 bytes --]
From d1f33330840acf6f5421d6d04a0f8be61c2108d7 Mon Sep 17 00:00:00 2001
From: Jan Nieuwenhuizen <janneke@gnu.org>
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 <aconchillo@gmail.com>
-;; Copyright (C) 2015 Jan Nieuwenhuizen <janneke@gnu.org>
+;; Copyright (C) 2015,2016 Jan Nieuwenhuizen <janneke@gnu.org>
;;
;; 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
[-- Attachment #4: Type: text/plain, Size: 154 bytes --]
--
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | Avatar® http://AvatarAcademy.nl
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: guile-json: simple alist to json
2016-02-18 22:21 guile-json: simple alist to json Jan Nieuwenhuizen
@ 2016-02-23 5:51 ` Aleix Conchillo Flaqué
2016-02-23 20:09 ` Jan Nieuwenhuizen
0 siblings, 1 reply; 4+ messages in thread
From: Aleix Conchillo Flaqué @ 2016-02-23 5:51 UTC (permalink / raw)
To: Jan Nieuwenhuizen; +Cc: guile-devel
[-- Attachment #1: Type: text/plain, Size: 1339 bytes --]
On Thu, Feb 18, 2016 at 2:21 PM, Jan Nieuwenhuizen <janneke@gnu.org> wrote:
> 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}"
>
>
Hi Jan,
thanks for the patch! I have tried with guile 2.0.11 and it's giving me th
e error at the end. I had zero time to fix it, but I believe it's
complaining because of this:
scheme@(guile-user)> (symbol 'a)
ERROR: In procedure string:
ERROR: In procedure string: Wrong type (expecting character): a
"a" is already a symbol.
Best,
Aleix
scheme@(guile-user)> (use-modules (json))
scheme@(guile-user)> (scm->json-string '((a . 1) (b . 2)))
ERROR: In procedure string:
ERROR: In procedure string: Wrong type (expecting character): a
Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> ,bt
4 (call-with-output-string #<procedure 1975db0 at
json/builder.scm:196:3 (p)>)
In json/builder.scm:
173:4 3 (json-build ((a . 1) (b . 2)) #<output: string 195eb60> #f #f
0)
118:34 2 (json-alist? ((a . 1) (b . 2)))
In ice-9/boot-9.scm:
1423:18 1 (symbol a)
In unknown file:
0 (string a)
[-- Attachment #2: Type: text/html, Size: 2987 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: guile-json: simple alist to json
2016-02-23 5:51 ` Aleix Conchillo Flaqué
@ 2016-02-23 20:09 ` Jan Nieuwenhuizen
2016-02-24 6:13 ` Aleix Conchillo Flaqué
0 siblings, 1 reply; 4+ messages in thread
From: Jan Nieuwenhuizen @ 2016-02-23 20:09 UTC (permalink / raw)
To: Aleix Conchillo Flaqué; +Cc: guile-devel
[-- Attachment #1: Type: text/plain, Size: 622 bytes --]
Aleix Conchillo Flaqué writes:
Hi Aleix,
> thanks for the patch! I have tried with guile 2.0.11 and it's giving me th
> e error at the end. I had zero time to fix it, but I believe it's complaining because of this:
>
> scheme@(guile-user)> (symbol 'a)
> ERROR: In procedure string:
> ERROR: In procedure string: Wrong type (expecting character): a
>
> "a" is already a symbol.
Ouch. Yes, I get that too...
I have been running this for quite some time but aparrently I prepared
patches and sent untested, "cleaned-up" code. Sorry.
Please find a new patch set attached.
Greetings,
Jan
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-builder-convert-symbols-to-string.patch --]
[-- Type: text/x-diff, Size: 1891 bytes --]
From 518feb262370855550a3af61eafa650f3452ac24 Mon Sep 17 00:00:00 2001
From: Jan Nieuwenhuizen <janneke@gnu.org>
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 | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/json/builder.scm b/json/builder.scm
index c725b19..f12dee8 100644
--- a/json/builder.scm
+++ b/json/builder.scm
@@ -1,6 +1,7 @@
;;; (json builder) --- Guile JSON implementation.
;; Copyright (C) 2013 Aleix Conchillo Flaque <aconchillo@gmail.com>
+;; Copyright (C) 2015 Jan Nieuwenhuizen <janneke@gnu.org>
;;
;; This file is part of guile-json.
;;
@@ -105,6 +106,12 @@
(display (number->string (exact->inexact scm)) port)
(display (number->string scm) port)))
+(define (->string x)
+ (cond ((char? x) (make-string 1 x))
+ ((number? x) (number->string x))
+ ((symbol? x) (symbol->string x))
+ (else x)))
+
(define (json-build-string scm port escape)
(display "\"" port)
(display
@@ -121,7 +128,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 +162,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
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-builder-build-objects-from-alists.patch --]
[-- Type: text/x-diff, Size: 2342 bytes --]
From d45b1be0805bdabef65d9f8edad122ee5e9f07e0 Mon Sep 17 00:00:00 2001
From: Jan Nieuwenhuizen <janneke@gnu.org>
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 | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/json/builder.scm b/json/builder.scm
index f12dee8..e1339b2 100644
--- a/json/builder.scm
+++ b/json/builder.scm
@@ -1,7 +1,7 @@
;;; (json builder) --- Guile JSON implementation.
;; Copyright (C) 2013 Aleix Conchillo Flaque <aconchillo@gmail.com>
-;; Copyright (C) 2015 Jan Nieuwenhuizen <janneke@gnu.org>
+;; Copyright (C) 2015,2016 Jan Nieuwenhuizen <janneke@gnu.org>
;;
;; This file is part of guile-json.
;;
@@ -112,6 +112,17 @@
((symbol? x) (symbol->string x))
(else 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
@@ -146,7 +157,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)
@@ -164,8 +175,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
[-- Attachment #4: Type: text/plain, Size: 156 bytes --]
--
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | Avatar® http://AvatarAcademy.nl
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: guile-json: simple alist to json
2016-02-23 20:09 ` Jan Nieuwenhuizen
@ 2016-02-24 6:13 ` Aleix Conchillo Flaqué
0 siblings, 0 replies; 4+ messages in thread
From: Aleix Conchillo Flaqué @ 2016-02-24 6:13 UTC (permalink / raw)
To: Jan Nieuwenhuizen; +Cc: guile-devel
On Tue, Feb 23, 2016 at 12:09 PM, Jan Nieuwenhuizen <janneke@gnu.org> wrote:
>
> Ouch. Yes, I get that too...
>
> I have been running this for quite some time but aparrently I prepared
> patches and sent untested, "cleaned-up" code. Sorry.
>
> Please find a new patch set attached.
>
Applied patches and released 0.5.0.
Thanks!
Aleix
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-02-24 6:13 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-18 22:21 guile-json: simple alist to json Jan Nieuwenhuizen
2016-02-23 5:51 ` Aleix Conchillo Flaqué
2016-02-23 20:09 ` Jan Nieuwenhuizen
2016-02-24 6:13 ` Aleix Conchillo Flaqué
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).