From 5c95ae60327ece0c3036c5eea0a4f2cd450c5cc5 Mon Sep 17 00:00:00 2001 From: Robert Soeldner Date: Tue, 15 Oct 2024 16:38:50 +0200 Subject: [PATCH] add getKeys to json lib --- examples/json/Json.sig | 3 +++ examples/json/Json.sml | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/examples/json/Json.sig b/examples/json/Json.sig index 0d0bf5920d..5bb6bec4d4 100644 --- a/examples/json/Json.sig +++ b/examples/json/Json.sig @@ -26,6 +26,9 @@ sig val getObject : json -> string -> json option val getObject' : json -> string -> json + val getKeys : json -> (string list) option + val getKeys' : json -> string list + val getBool : json -> bool option val getBool' : json -> bool diff --git a/examples/json/Json.sml b/examples/json/Json.sml index edd37ebc84..9eee8dd02e 100644 --- a/examples/json/Json.sml +++ b/examples/json/Json.sml @@ -290,6 +290,17 @@ fun getObject' json k = SOME v => v | NONE => raise ERR "getObject'" "expects object." + +fun getKeys json = + case json of + AList l => SOME (map #1 l) + | _ => NONE + +fun getKeys' json = + case json of + AList l => map #1 l + | _ => raise ERR "getKeys'" "expects object." + fun getBool json = case json of Boolean b => SOME b @@ -349,6 +360,8 @@ fun foldArray' json (f: json -> 'a) = | NONE => raise ERR "foldArray'" "expects value." (* val json = hd (#1 (parse ([],Substring.full "{\"foo\" : [1, 23, 4], \"bar\" : 2}"))) *) +(* val keys = getKeys json *) +(* val keys' = getKeys' json *) (* val foo = getObject json "foo" *) (* val foo' = getObject' json "foo" *) (* val foo_ints = foldlArray foo' getInt *)