Skip to content

Commit

Permalink
Merge pull request #1322 from rsoeldner/json-accessors
Browse files Browse the repository at this point in the history
add basic json accessors
  • Loading branch information
konrad-slind authored Oct 14, 2024
2 parents 9ee2f86 + 49aeab4 commit a62035f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/json/Json.sig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ sig
val fromString : string -> json list * substring
val fromFile : string -> json list * substring

val foldArray : json -> (json -> 'a option) -> 'a list option
val foldArray' : json -> (json -> 'a ) -> 'a list

val getObject : json -> string -> json option
val getObject' : json -> string -> json

val getBool : json -> bool option
val getBool' : json -> bool

val getString : json -> string option
val getString' : json -> string

val getInt : json -> int option
val getInt' : json -> int

val getFloat : json -> real option
val getFloat' : json -> real

val pp_json : json PP.pprinter

end
79 changes: 79 additions & 0 deletions examples/json/Json.sml
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,83 @@ val pp_json =

val _ = PolyML.addPrettyPrinter (fn d => fn _ => fn json => pp_json json);


(* accessors *)

fun getObject json k =
case json of
AList al => Option.map #2 (List.find (fn (key, _) => key = k) al)
| _ => NONE

fun getObject' json k =
case getObject json k of
SOME v => v
| NONE => raise ERR "getObject'" "expects object."

fun getBool json =
case json of
Boolean b => SOME b
| _ => NONE

fun getBool' json =
case getBool json of
SOME v => v
| NONE => raise ERR "getBool'" "expects boolean."

fun getString json =
case json of
String str => SOME str
| _ => NONE

fun getString' json =
case getString json of
SOME v => v
| NONE => raise ERR "getString'" "expects string."

fun getInt json =
case json of
Number (Int i) => SOME i
| _ => NONE

fun getInt' json =
case getInt json of
SOME v => v
| NONE => raise ERR "getInt'" "expects int."

fun getFloat json =
case json of
Number (Float f) => SOME f
| _ => NONE

fun getFloat' json =
case getFloat json of
SOME v => v
| NONE => raise ERR "getFloat'" "expects float."

fun foldArray json (f: json -> 'a option) =
let
fun foldSeq acc [] = SOME (List.rev acc)
| foldSeq acc (x::xs) =
case f x of
NONE => NONE
| SOME v => foldSeq (v::acc) xs
in
case json of
List l => foldSeq [] l
| _ => NONE
end

fun foldArray' json (f: json -> 'a) =
case foldArray json (SOME o f) of
SOME v => v
| NONE => raise ERR "foldArray'" "expects value."

(* val json = hd (#1 (parse ([],Substring.full "{\"foo\" : [1, 23, 4], \"bar\" : 2}"))) *)
(* val foo = getObject json "foo" *)
(* val foo' = getObject' json "foo" *)
(* val foo_ints = foldlArray foo' getInt *)
(* val json_str = getString json *)
(* val bar = getObject json "bar" *)


end (* Json *)

0 comments on commit a62035f

Please sign in to comment.