Skip to content

Commit

Permalink
feat: add parse/parseExn
Browse files Browse the repository at this point in the history
  • Loading branch information
glennsl committed Jul 17, 2022
1 parent 9ab6329 commit f18c70b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/DecodeExample.res
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ let data = `{
]
}`

let _ = data->Js.Json.parseExn->Json.decode(Decode.polyline)->Js.log
let _ = data->Json.parseExn->Json.decode(Decode.polyline)->Js.log
13 changes: 13 additions & 0 deletions src/Json.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
module Encode = Json_Encode
module Decode = Json_Decode

exception ParseError(string)

let decode = Decode.decode

let parse = str =>
try Ok(str->Js.Json.parseExn) catch {
| Js.Exn.Error(ex) => Error(ex->Js.Exn.message->Js.Option.getWithDefault("Unknown error", _))
}

let parseExn = str =>
try str->Js.Json.parseExn catch {
| Js.Exn.Error(ex) =>
raise(ParseError(ex->Js.Exn.message->Js.Option.getWithDefault("Unknown error", _)))
}
9 changes: 9 additions & 0 deletions src/Json.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Encode = Json_Encode
module Decode = Json_Decode

exception ParseError(string)

let decode: (Js.Json.t, Decode.t<'a>) => result<'a, string>

let parse: string => result<Js.Json.t, string>
let parseExn: string => Js.Json.t

0 comments on commit f18c70b

Please sign in to comment.