diff json.ur @ 1:4d103b4450ee

Converted a record to JSON
author Adam Chlipala <adam@chlipala.net>
date Thu, 02 Dec 2010 11:35:01 -0500
parents 63697ef80a2c
children 478524b9d23a
line wrap: on
line diff
--- a/json.ur	Thu Dec 02 11:10:57 2010 -0500
+++ b/json.ur	Thu Dec 02 11:35:01 2010 -0500
@@ -190,3 +190,12 @@
         {ToJson = toJ,
          FromJson = fromJ}
     end
+
+fun json_record [ts ::: {Type}] (fl : folder ts) (jss : $(map json ts)) (names : $(map (fn _ => string) ts)) : json $ts =
+    {ToJson = fn r => "{" ^ @foldR3 [json] [fn _ => string] [id] [fn _ => string]
+                             (fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] (j : json t) name v acc =>
+                                 escape name ^ ":" ^ j.ToJson v ^ (case acc of
+                                                                       "" => ""
+                                                                     | _ => "," ^ acc))
+                             "" fl jss names r ^ "}",
+     FromJson = fn _ => error <xml>Uhoh!</xml>}