Re: [PATCH] ice-9: Add JSON module.
Matt Wettewrites: > On Sep 21, 2015, at 1:08 PM, Ludovic Courtès wrote: > > > David Thompson skribis: > > JSON is an commonly encountered format when writing web > applications, > much like XML, and I think it would be a good idea if the core > Guile > distribution had an SXML equivalent for JSON. This patch > introduces > such an interface in the (ice-9 json) module. > > > There’s also guile-json, bindings to a C library, but I think it’s > better to have a pure Scheme implementation, and to have it in > Guile > core. > > I wonder if we should introduce it in 2.0. What do people think? > > > I would be happy to have this in the core; I have used it. Agreed. > 1. There is a minor typo in the source: should be "denominator.” Indeed, good catch! > 2. The comments say integers are converted to exact and floating point > to inexact, but the code will convert 1.0 to exact. In Scheme terminology, 1.0 is an integer. > Would it work to use the parser from (language ecmascript parse), > possibly restricting it? Or do you think it’s more viable to have > a > separate parser because there are too many differences? > > > I vote for separate parser, the json.scm file is lightweight. > json.scm.go is < 17k. Agreed. The simplicity also allows confidence that it can safely handle potentially malicious JSON data. Mark
Re: [PATCH] ice-9: Add JSON module.
> On Sep 21, 2015, at 11:49 PM, Mark H Weaverwrote: > >> 2. The comments say integers are converted to exact and floating point >> to inexact, but the code will convert 1.0 to exact. > > In Scheme terminology, 1.0 is an integer. But in guile-2.0.11: scheme@(guile-user)> (exact? (string->number "1.0")) $1 = #f I also vote for using string->number: probably faster and more heavily tested. Matt
Re: [PATCH] ice-9: Add JSON module.
> On Sep 21, 2015, at 1:08 PM, Ludovic Courtèswrote: > > David Thompson skribis: > >> JSON is an commonly encountered format when writing web applications, >> much like XML, and I think it would be a good idea if the core Guile >> distribution had an SXML equivalent for JSON. This patch introduces >> such an interface in the (ice-9 json) module. > > There’s also guile-json, bindings to a C library, but I think it’s > better to have a pure Scheme implementation, and to have it in Guile > core. > > I wonder if we should introduce it in 2.0. What do people think? I would be happy to have this in the core; I have used it. A few comments: 1. There is a minor typo in the source: should be "denominator.” 2. The comments say integers are converted to exact and floating point to inexact, but the code will convert 1.0 to exact. > Would it work to use the parser from (language ecmascript parse), > possibly restricting it? Or do you think it’s more viable to have a > separate parser because there are too many differences? I vote for separate parser, the json.scm file is lightweight. json.scm.go is < 17k.
Re: [PATCH] ice-9: Add JSON module.
David Thompsonskribis: > JSON is an commonly encountered format when writing web applications, > much like XML, and I think it would be a good idea if the core Guile > distribution had an SXML equivalent for JSON. This patch introduces > such an interface in the (ice-9 json) module. There’s also guile-json, bindings to a C library, but I think it’s better to have a pure Scheme implementation, and to have it in Guile core. I wonder if we should introduce it in 2.0. What do people think? > +(define (json-error port) > + (throw 'json-error port)) This won’t print correctly, unless there’s an exception printer installed in boot-9.scm (see ‘getaddrinfo-error’ for instance.) Could you add one? Also, I think we need more details about the error: parse error, what kind, etc. Would it work to use the parser from (language ecmascript parse), possibly restricting it? Or do you think it’s more viable to have a separate parser because there are too many differences? Is there a standard test suite that we could test it against, somehow? Otherwise LGTM. Thanks, and sorry for the delay! Ludo’.
Re: [PATCH] ice-9: Add JSON module.
I tested this, including with the fixes specified in the email. (ice-9 json) seems to be working great... I'd love to see it merged!
Re: [PATCH] ice-9: Add JSON module.
Noticed a couple of small issues after I sent the initial patch that I've fixed in my local git branch: David Thompson da...@gnu.org writes: +(define-module (ice-9 json) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:export (read-json write-json)) No need to import SRFI-1. +(define (read-zeroes port) + Read a sequence of zeroes from PORT. + (let loop ((result '())) +(match (peek-char port) + ((? eof-object?) + result) + (#\0 + (read-char port) + (loop (cons 0 result))) + (else result Never used. Removed. -- David Thompson GPG Key: 0FF1D807
[PATCH] ice-9: Add JSON module.
Hello Guilers, JSON is an commonly encountered format when writing web applications, much like XML, and I think it would be a good idea if the core Guile distribution had an SXML equivalent for JSON. This patch introduces such an interface in the (ice-9 json) module. With (ice-9 json), this expression: (@ (name . Eva Luator) (age . 24) (schemer . #t) (hobbies hacking cycling surfing)) serializes to this JSON (except not pretty-printed): { name: Eva Luator, age: 24, schemer: true, hobbies: [ hacking, cycling, surfing ] } Thanks to Mark Weaver and Chris Webber for helping come to a consensus on a good syntax for JSON objects. From 2d4d8607aedaede98f413a84f135d8798d506233 Mon Sep 17 00:00:00 2001 From: David Thompson dthomps...@worcester.edu Date: Sat, 15 Aug 2015 14:09:23 -0400 Subject: [PATCH] ice-9: Add JSON module. * module/ice-9/json.scm: New file. * module/Makefile.am (ICE_9_SOURCES): Add it. * test-suite/tests/json.test: New file. * test-suite/Makefile.am (SCM_TESTS): Add it. * doc/ref/guile.texi (Guile Modules): Add JSON section. * doc/ref/json.texi: New file. * doc/ref/Makefile.am (guile_TEXINFOS): Add it. --- doc/ref/Makefile.am| 3 +- doc/ref/guile.texi | 2 + doc/ref/json.texi | 62 +++ module/Makefile.am | 3 +- module/ice-9/json.scm | 395 + test-suite/Makefile.am | 1 + test-suite/tests/json.test | 149 + 7 files changed, 613 insertions(+), 2 deletions(-) create mode 100644 doc/ref/json.texi create mode 100644 module/ice-9/json.scm create mode 100644 test-suite/tests/json.test diff --git a/doc/ref/Makefile.am b/doc/ref/Makefile.am index 31c26a7..5dfc019 100644 --- a/doc/ref/Makefile.am +++ b/doc/ref/Makefile.am @@ -95,7 +95,8 @@ guile_TEXINFOS = preface.texi \ goops.texi \ goops-tutorial.texi \ guile-invoke.texi \ - effective-version.texi + effective-version.texi \ + json.texi ETAGS_ARGS = $(info_TEXINFOS) $(guile_TEXINFOS) diff --git a/doc/ref/guile.texi b/doc/ref/guile.texi index db815eb..468d3a5 100644 --- a/doc/ref/guile.texi +++ b/doc/ref/guile.texi @@ -375,6 +375,7 @@ available through both Scheme and C interfaces. * Statprof::An easy-to-use statistical profiler. * SXML::Parsing, transforming, and serializing XML. * Texinfo Processing:: Munging documents written in Texinfo. +* JSON::Parsing and serializing JSON. @end menu @include slib.texi @@ -397,6 +398,7 @@ available through both Scheme and C interfaces. @include statprof.texi @include sxml.texi @include texinfo.texi +@include json.texi @include goops.texi diff --git a/doc/ref/json.texi b/doc/ref/json.texi new file mode 100644 index 000..43dba4d --- /dev/null +++ b/doc/ref/json.texi @@ -0,0 +1,62 @@ +@c -*-texinfo-*- +@c This is part of the GNU Guile Reference Manual. +@c Copyright (C) 2015 Free Software Foundation, Inc. +@c See the file guile.texi for copying conditions. +@c + +@node JSON +@section JSON + +@cindex json +@cindex (ice-9 json) + +The @code{(ice-9 json)} module provides procedures for parsing and +serializing JSON, the JavaScript Object Notation data interchange +format. For example, the JSON document: + +@example +@verbatim +{ + name: Eva Luator, + age: 24, + schemer: true, + hobbies: [ +hacking, +cycling, +surfing + ] +} +@end verbatim +@end example + +may be represented with the following s-expression: + +@example +@verbatim +(@ (name . Eva Luator) + (age . 24) + (schemer . #t) + (hobbies hacking cycling surfing)) +@end verbatim +@end example + +Strings, real numbers, @code{#t}, @code{#f}, @code{#nil}, lists, and +association lists may be serialized as JSON. Association lists +serialize to objects, and regular lists serialize to arrays. To +distinguish regular lists from association lists, the @code{@@} symbol +is used to ``tag'' the association list as a JSON object, as in the +above example. The keys of association lists may be either strings or +symbols. + +@deffn {Scheme Procedure} read-json port + +Parse JSON-encoded text from @var{port} and return its s-expression +representation. + +@end deffn + +@deffn {Scheme Procedure} write-json exp port + +Write the expression @var{exp} as JSON-encoded text to @var{port}. + +@end deffn diff --git a/module/Makefile.am b/module/Makefile.am index 7e96de7..6380953 100644 --- a/module/Makefile.am +++ b/module/Makefile.am @@ -256,7 +256,8 @@ ICE_9_SOURCES = \ ice-9/list.scm \ ice-9/serialize.scm \ ice-9/local-eval.scm \ - ice-9/unicode.scm + ice-9/unicode.scm \ + ice-9/json.scm srfi/srfi-64.go: srfi/srfi-64.scm srfi/srfi-64/testing.scm diff --git a/module/ice-9/json.scm b/module/ice-9/json.scm new file mode 100644 index 000..3850ee4 --- /dev/null +++ b/module/ice-9/json.scm @@ -0,0 +1,395