annotate json.ur @ 30:4e673b535434

Optimizing JSON generation
author Adam Chlipala <adam@chlipala.net>
date Thu, 04 Dec 2014 19:52:51 -0500
parents 7530b2b54353
children
rev   line source
adam@29 1 con json a = {ToJson : a -> string,
adam@29 2 FromJson : string -> a * string}
adam@0 3
adam@0 4 fun mkJson [a] (x : {ToJson : a -> string,
adam@0 5 FromJson : string -> a * string}) = x
adam@0 6
adam@0 7 fun skipSpaces s =
adam@0 8 let
adam@0 9 val len = String.length s
adam@0 10
adam@0 11 fun skip i =
adam@0 12 if i >= len then
adam@0 13 ""
adam@0 14 else
adam@0 15 let
adam@0 16 val ch = String.sub s i
adam@0 17 in
adam@0 18 if Char.isSpace ch then
adam@0 19 skip (i+1)
adam@0 20 else
adam@0 21 String.substring s {Start = i, Len = len-i}
adam@0 22 end
adam@0 23 in
adam@0 24 skip 0
adam@0 25 end
adam@0 26
adam@0 27 fun toJson [a] (j : json a) : a -> string = j.ToJson
ezyang@23 28 fun fromJson' [a] (j : json a) : string -> a * string = j.FromJson
adam@0 29
adam@0 30 fun fromJson [a] (j : json a) (s : string) : a =
adam@0 31 let
adam@0 32 val (v, s') = j.FromJson (skipSpaces s)
adam@0 33 in
adam@0 34 if String.all Char.isSpace s' then
adam@0 35 v
adam@0 36 else
adam@0 37 error <xml>Extra content at end of JSON record: {[s']}</xml>
adam@0 38 end
adam@0 39
adam@0 40 fun escape s =
adam@0 41 let
adam@30 42 fun esc s =
adam@30 43 case s of
adam@30 44 "" => "\""
adam@30 45 | _ =>
adam@0 46 let
adam@30 47 val ch = String.sub s 0
adam@0 48 in
adam@0 49 (if ch = #"\"" || ch = #"\\" then
adam@0 50 "\\" ^ String.str ch
adam@0 51 else
adam@30 52 String.str ch) ^ esc (String.suffix s 1)
adam@0 53 end
adam@0 54 in
adam@30 55 "\"" ^ esc s
adam@0 56 end
adam@0 57
adam@0 58 fun unescape s =
adam@0 59 let
adam@0 60 val len = String.length s
adam@0 61
adam@0 62 fun findEnd i =
adam@0 63 if i >= len then
adam@0 64 error <xml>JSON unescape: string ends before quote: {[s]}</xml>
adam@0 65 else
adam@0 66 let
adam@0 67 val ch = String.sub s i
adam@0 68 in
adam@0 69 case ch of
adam@0 70 #"\"" => i
adam@0 71 | #"\\" =>
adam@0 72 if i+1 >= len then
adam@0 73 error <xml>JSON unescape: Bad escape sequence: {[s]}</xml>
adam@0 74 else
adam@0 75 findEnd (i+2)
adam@0 76 | _ => findEnd (i+1)
adam@0 77 end
adam@0 78
adam@0 79 val last = findEnd 1
adam@0 80
adam@0 81 fun unesc i =
adam@0 82 if i >= last then
adam@0 83 ""
adam@0 84 else
adam@0 85 let
adam@0 86 val ch = String.sub s i
adam@0 87 in
adam@0 88 case ch of
adam@0 89 #"\\" =>
adam@0 90 if i+1 >= len then
adam@0 91 error <xml>JSON unescape: Bad escape sequence: {[s]}</xml>
adam@0 92 else
adam@0 93 String.str (String.sub s (i+1)) ^ unesc (i+2)
adam@0 94 | _ => String.str ch ^ unesc (i+1)
adam@0 95 end
adam@0 96 in
adam@0 97 if len = 0 || String.sub s 0 <> #"\"" then
adam@0 98 error <xml>JSON unescape: String doesn't start with double quote: {[s]}</xml>
adam@0 99 else
adam@0 100 (unesc 1, String.substring s {Start = last+1, Len = len-last-1})
adam@0 101 end
adam@0 102
adam@0 103 val json_string = {ToJson = escape,
adam@0 104 FromJson = unescape}
adam@0 105
adam@0 106 fun numIn [a] (_ : read a) s : a * string =
adam@0 107 let
adam@0 108 val len = String.length s
adam@0 109
adam@0 110 fun findEnd i =
adam@0 111 if i >= len then
adam@0 112 i
adam@0 113 else
adam@0 114 let
adam@0 115 val ch = String.sub s i
adam@0 116 in
adam@0 117 if Char.isDigit ch || ch = #"-" || ch = #"." || ch = #"E" || ch = #"e" then
adam@0 118 findEnd (i+1)
adam@0 119 else
adam@0 120 i
adam@0 121 end
adam@0 122
adam@0 123 val last = findEnd 0
adam@0 124 in
adam@0 125 (readError (String.substring s {Start = 0, Len = last}), String.substring s {Start = last, Len = len-last})
adam@0 126 end
adam@0 127
adam@0 128 fun json_num [a] (_ : show a) (_ : read a) : json a = {ToJson = show,
adam@0 129 FromJson = numIn}
adam@0 130
adam@0 131 val json_int = json_num
adam@0 132 val json_float = json_num
adam@0 133
adam@0 134 val json_bool = {ToJson = fn b => if b then "true" else "false",
adam@0 135 FromJson = fn s => if String.isPrefix {Full = s, Prefix = "true"} then
adam@0 136 (True, String.substring s {Start = 4, Len = String.length s - 4})
adam@0 137 else if String.isPrefix {Full = s, Prefix = "false"} then
adam@0 138 (False, String.substring s {Start = 5, Len = String.length s - 5})
adam@0 139 else
adam@0 140 error <xml>JSON: bad boolean string: {[s]}</xml>}
adam@0 141
adam@4 142 fun json_option [a] (j : json a) : json (option a) =
adam@4 143 {ToJson = fn v => case v of
adam@4 144 None => "null"
adam@4 145 | Some v => j.ToJson v,
adam@4 146 FromJson = fn s => if String.isPrefix {Full = s, Prefix = "null"} then
adam@4 147 (None, String.substring s {Start = 4, Len = String.length s - 4})
adam@4 148 else
adam@4 149 let
adam@4 150 val (v, s') = j.FromJson s
adam@4 151 in
adam@4 152 (Some v, s')
adam@4 153 end}
adam@4 154
adam@0 155 fun json_list [a] (j : json a) : json (list a) =
adam@0 156 let
adam@0 157 fun toJ' (ls : list a) : string =
adam@0 158 case ls of
adam@0 159 [] => ""
adam@0 160 | x :: ls => "," ^ toJson j x ^ toJ' ls
adam@0 161
adam@0 162 fun toJ (x : list a) : string =
adam@0 163 case x of
adam@0 164 [] => "[]"
adam@0 165 | x :: [] => "[" ^ toJson j x ^ "]"
adam@0 166 | x :: ls => "[" ^ toJson j x ^ toJ' ls ^ "]"
adam@0 167
adam@0 168 fun fromJ (s : string) : list a * string =
adam@0 169 let
adam@0 170 fun fromJ' (s : string) : list a * string =
adam@0 171 if String.length s = 0 then
adam@0 172 error <xml>JSON list doesn't end with ']'</xml>
adam@0 173 else
adam@0 174 let
adam@0 175 val ch = String.sub s 0
adam@0 176 in
adam@0 177 case ch of
adam@0 178 #"]" => ([], String.substring s {Start = 1, Len = String.length s - 1})
adam@0 179 | _ =>
adam@0 180 let
adam@0 181 val (x, s') = j.FromJson s
adam@0 182 val s' = skipSpaces s'
adam@0 183 val s' = if String.length s' = 0 then
adam@0 184 error <xml>JSON list doesn't end with ']'</xml>
adam@0 185 else if String.sub s' 0 = #"," then
adam@0 186 skipSpaces (String.substring s' {Start = 1, Len = String.length s' - 1})
adam@0 187 else
adam@0 188 s'
adam@0 189
adam@0 190 val (ls, s'') = fromJ' s'
adam@0 191 in
adam@0 192 (x :: ls, s'')
adam@0 193 end
adam@0 194 end
adam@0 195 in
adam@0 196 if String.length s = 0 || String.sub s 0 <> #"[" then
adam@0 197 error <xml>JSON list doesn't start with '[': {[s]}</xml>
adam@0 198 else
adam@0 199 fromJ' (skipSpaces (String.substring s {Start = 1, Len = String.length s - 1}))
adam@0 200 end
adam@0 201 in
adam@0 202 {ToJson = toJ,
adam@0 203 FromJson = fromJ}
adam@0 204 end
adam@1 205
adam@1 206 fun json_record [ts ::: {Type}] (fl : folder ts) (jss : $(map json ts)) (names : $(map (fn _ => string) ts)) : json $ts =
adam@18 207 {ToJson = fn r => "{" ^ @foldR3 [json] [fn _ => string] [ident] [fn _ => string]
adam@1 208 (fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] (j : json t) name v acc =>
adam@1 209 escape name ^ ":" ^ j.ToJson v ^ (case acc of
adam@1 210 "" => ""
adam@30 211 | acc => "," ^ acc))
adam@1 212 "" fl jss names r ^ "}",
adam@2 213 FromJson = fn s =>
adam@2 214 let
adam@2 215 fun fromJ s (r : $(map option ts)) : $(map option ts) * string =
adam@2 216 if String.length s = 0 then
adam@2 217 error <xml>JSON object doesn't end in brace</xml>
adam@2 218 else if String.sub s 0 = #"}" then
adam@2 219 (r, String.substring s {Start = 1, Len = String.length s - 1})
adam@2 220 else let
adam@2 221 val (name, s') = unescape s
adam@2 222 val s' = skipSpaces s'
adam@2 223 val s' = if String.length s' = 0 || String.sub s' 0 <> #":" then
ezyang@22 224 error <xml>No colon after JSON object field name</xml>
adam@2 225 else
adam@2 226 skipSpaces (String.substring s' {Start = 1, Len = String.length s' - 1})
adam@2 227
adam@2 228 val (r, s') = @foldR2 [json] [fn _ => string] [fn ts => $(map option ts) -> $(map option ts) * string]
adam@2 229 (fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] (j : json t) name' acc r =>
adam@2 230 if name = name' then
adam@2 231 let
adam@2 232 val (v, s') = j.FromJson s'
adam@2 233 in
adam@2 234 (r -- nm ++ {nm = Some v}, s')
adam@2 235 end
adam@2 236 else
adam@2 237 let
adam@2 238 val (r', s') = acc (r -- nm)
adam@2 239 in
adam@2 240 (r' ++ {nm = r.nm}, s')
adam@2 241 end)
adam@2 242 (fn _ => error <xml>Unknown JSON object field name {[name]}</xml>)
adam@2 243 fl jss names r
adam@2 244
adam@2 245 val s' = skipSpaces s'
adam@2 246 val s' = if String.length s' <> 0 && String.sub s' 0 = #"," then
adam@2 247 skipSpaces (String.substring s' {Start = 1, Len = String.length s' - 1})
adam@2 248 else
adam@2 249 s'
adam@2 250 in
adam@2 251 fromJ s' r
adam@2 252 end
adam@2 253 in
adam@2 254 if String.length s = 0 || String.sub s 0 <> #"{" then
adam@2 255 error <xml>JSON record doesn't begin with brace</xml>
adam@2 256 else
adam@2 257 let
adam@3 258 val (r, s') = fromJ (skipSpaces (String.substring s {Start = 1, Len = String.length s - 1}))
adam@2 259 (@map0 [option] (fn [t ::_] => None) fl)
adam@2 260 in
adam@18 261 (@map2 [option] [fn _ => string] [ident] (fn [t] (v : option t) name =>
adam@18 262 case v of
adam@18 263 None => error <xml>Missing JSON object field {[name]}</xml>
adam@18 264 | Some v => v) fl r names, s')
adam@2 265 end
adam@2 266 end}
ezyang@22 267
ezyang@22 268 fun json_variant [ts ::: {Type}] (fl : folder ts) (jss : $(map json ts)) (names : $(map (fn _ => string) ts)) : json (variant ts) =
ezyang@22 269 {ToJson = fn r => let val jnames = @map2 [json] [fn _ => string] [fn x => json x * string]
ezyang@22 270 (fn [t] (j : json t) (name : string) => (j, name)) fl jss names
ezyang@22 271 in @Variant.destrR [ident] [fn x => json x * string]
ezyang@22 272 (fn [p ::_] (v : p) (j : json p, name : string) =>
ezyang@22 273 "{" ^ escape name ^ ":" ^ j.ToJson v ^ "}") fl r jnames
ezyang@22 274 end,
ezyang@22 275 FromJson = fn s =>
ezyang@22 276 if String.length s = 0 || String.sub s 0 <> #"{" then
ezyang@22 277 error <xml>JSON variant doesn't begin with brace</xml>
ezyang@22 278 else
ezyang@22 279 let
ezyang@23 280 val (name, s') = unescape (skipSpaces (String.suffix s 1))
ezyang@22 281 val s' = skipSpaces s'
ezyang@22 282 val s' = if String.length s' = 0 || String.sub s' 0 <> #":" then
ezyang@22 283 error <xml>No colon after JSON object field name</xml>
ezyang@22 284 else
ezyang@22 285 skipSpaces (String.substring s' {Start = 1, Len = String.length s' - 1})
ezyang@22 286
ezyang@22 287 val (r, s') = (@foldR2 [json] [fn _ => string]
ezyang@22 288 [fn ts => ts' :: {Type} -> [ts ~ ts'] => variant (ts ++ ts') * string]
ezyang@22 289 (fn [nm ::_] [t ::_] [rest ::_] [[nm] ~ rest] (j : json t) name'
ezyang@22 290 (acc : ts' :: {Type} -> [rest ~ ts'] => variant (rest ++ ts') * string) [fwd ::_] [[nm = t] ++ rest ~ fwd] =>
ezyang@22 291 if name = name'
ezyang@22 292 then
ezyang@22 293 let val (v, s') = j.FromJson s'
ezyang@22 294 in (make [nm] v, s')
ezyang@22 295 end
ezyang@22 296 else acc [fwd ++ [nm = t]]
ezyang@22 297 )
ezyang@22 298 (fn [fwd ::_] [[] ~ fwd] => error <xml>Unknown JSON object variant name {[name]}</xml>)
ezyang@22 299 fl jss names) [[]] !
ezyang@22 300
ezyang@22 301 val s' = skipSpaces s'
ezyang@22 302 val s' = if String.length s' <> 0 && String.sub s' 0 = #"," then
ezyang@22 303 skipSpaces (String.substring s' {Start = 1, Len = String.length s' - 1})
ezyang@22 304 else
ezyang@22 305 s'
ezyang@22 306 in
ezyang@22 307 if String.length s' = 0 then
ezyang@22 308 error <xml>JSON object doesn't end in brace</xml>
ezyang@22 309 else if String.sub s' 0 = #"}" then
ezyang@22 310 (r, String.substring s' {Start = 1, Len = String.length s' - 1})
ezyang@22 311 else error <xml>Junk after JSON value in object</xml>
ezyang@22 312 end
ezyang@22 313 }
ezyang@22 314
ezyang@22 315 val json_unit : json unit = json_record {} {}
ezyang@23 316
ezyang@23 317 functor Recursive (M : sig
ezyang@23 318 con t :: Type -> Type
ezyang@23 319 val json_t : a ::: Type -> json a -> json (t a)
ezyang@23 320 end) = struct
ezyang@23 321 open M
ezyang@23 322
ezyang@23 323 datatype r = Rec of t r
ezyang@23 324
ezyang@23 325 fun rTo (Rec x) = (json_t {ToJson = rTo,
ezyang@23 326 FromJson = fn _ => error <xml>Tried to FromJson in ToJson!</xml>}).ToJson x
ezyang@23 327
ezyang@23 328 fun rFrom s =
ezyang@23 329 let
ezyang@23 330 val (x, s') = (json_t {ToJson = fn _ => error <xml>Tried to ToJson in FromJson!</xml>,
ezyang@23 331 FromJson = rFrom}).FromJson s
ezyang@23 332 in
ezyang@23 333 (Rec x, s')
ezyang@23 334 end
ezyang@23 335
ezyang@23 336 val json_r = {ToJson = rTo, FromJson = rFrom}
ezyang@23 337 end