# HG changeset patch # User Chris Cannam # Date 1619075512 -3600 # Thu Apr 22 08:11:52 2021 +0100 # Node ID 7d75cf1278cf3dce73d74406033e84db20e1de30 # Parent ae04bfe3da886b0678223c488b162fe1dc9652dd Write integers as integers diff --git a/json.sml b/json.sml --- a/json.sml +++ b/json.sml @@ -366,7 +366,16 @@ in implode (escape' [] (explode s)) end - + + fun serialiseNumber n = + implode (map (fn #"~" => #"-" | c => c) + (explode + (if Real.isFinite n andalso + Real.== (n, Real.realRound n) andalso + Real.<= (Real.abs n, 1e6) + then Int.toString (Real.round n) + else Real.toString n))) + fun serialise json = case json of OBJECT pp => "{" ^ String.concatWith @@ -375,8 +384,7 @@ serialise value) pp) ^ "}" | ARRAY arr => "[" ^ String.concatWith "," (map serialise arr) ^ "]" - | NUMBER n => implode (map (fn #"~" => #"-" | c => c) - (explode (Real.toString n))) + | NUMBER n => serialiseNumber n | STRING s => "\"" ^ stringEscape s ^ "\"" | BOOL b => Bool.toString b | NULL => "null"