Browse Source

Importing improvements to Ur/Web Meta library

Adam Chlipala 10 years ago
parent
commit
04b812cc68
1 changed files with 8 additions and 10 deletions
  1. 8 10
      frameworks/Ur/urweb/meta/json.ur

+ 8 - 10
frameworks/Ur/urweb/meta/json.ur

@@ -39,22 +39,20 @@ fun fromJson [a] (j : json a) (s : string) : a =
 
 
 fun escape s =
 fun escape s =
     let
     let
-        val len = String.length s
-
-        fun esc i =
-            if i >= len then
-                "\""
-            else
+        fun esc s =
+            case s of
+                "" => "\""
+              | _ =>
                 let
                 let
-                    val ch = String.sub s i
+                    val ch = String.sub s 0
                 in
                 in
                     (if ch = #"\"" || ch = #"\\" then
                     (if ch = #"\"" || ch = #"\\" then
                          "\\" ^ String.str ch
                          "\\" ^ String.str ch
                      else
                      else
-                         String.str ch) ^ esc (i+1)
+                         String.str ch) ^ esc (String.suffix s 1)
                 end
                 end
     in
     in
-        "\"" ^ esc 0
+        "\"" ^ esc s
     end
     end
 
 
 fun unescape s =
 fun unescape s =
@@ -210,7 +208,7 @@ fun json_record [ts ::: {Type}] (fl : folder ts) (jss : $(map json ts)) (names :
                              (fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] (j : json t) name v acc =>
                              (fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] (j : json t) name v acc =>
                                  escape name ^ ":" ^ j.ToJson v ^ (case acc of
                                  escape name ^ ":" ^ j.ToJson v ^ (case acc of
                                                                        "" => ""
                                                                        "" => ""
-                                                                     | _ => "," ^ acc))
+                                                                     | acc => "," ^ acc))
                              "" fl jss names r ^ "}",
                              "" fl jss names r ^ "}",
      FromJson = fn s =>
      FromJson = fn s =>
                    let
                    let