json module whitespace fix

4 views
Skip to first unread message

Chris Cannam

unread,
Sep 15, 2014, 2:42:08 PM9/15/14
to yeti...@googlegroups.com

I've been using the yeti.experimental.json module a bit.

I know that when I asked about this once before, it was suggested that
this wasn't the best long-term solution for json parse/serialise, but it
does work, with a few eccentricities.

One of the eccentricities is that it doesn't cope with whitespace in
your json file at all. Here's a patch that fixes that for me:

jsonParse str is string -> json =
(invalid = failWith . ("Invalid JSON: " ^);
- var tokens = matchAll
('\s++|([{:,}\[\]]|"(?:[^\\"]|\\["\\/bfnrtu])*+"|' ^
-
'-?\d++(?:\.\d++)?(?:[eE][+-]?\d++)?|null|true|false)')
- (`at` 0) invalid str;
+ nontoken s = if strTrim s == '' then [] else invalid s; fi;
+ var tokens = concat
+ (matchAll ('([{:,}\[\]]|"(?:[^\\"]|\\["\\/bfnrtu])*+"|' ^
+
'-?\d++(?:\.\d++)?(?:[eE][+-]?\d++)?|null|true|false)')
+ (take 1) nontoken str);
fetch () =
case tokens of
s :: rest:
tokens := rest;
case s of
- '': fetch ();
'{':
m = [:];
empty? tokens or head tokens != '}' loop


Chris
Reply all
Reply to author
Forward
0 new messages