Skip to content

Commit

Permalink
Melange v3 long-lived branch (#6)
Browse files Browse the repository at this point in the history
* Melange v3 long-lived branch

* update

* chore: update nix flakes
  • Loading branch information
anmonteiro authored Feb 1, 2024
1 parent 9f29577 commit bd9532e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 67 deletions.
2 changes: 1 addition & 1 deletion examples/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
let mapJsonObjectString f decoder (encoder : int -> Js.Json.t) str =
let json = Json.parseOrRaise str in
Json.Decode.(dict decoder json)
|> Js.Dict.map (fun [@u] v -> f v)
|> Js.Dict.map ~f:(fun [@u] v -> f v)
|> Json.Encode.dict encoder |> Json.stringify

let sum = Array.fold_left ( + ) 0
Expand Down
61 changes: 21 additions & 40 deletions examples/encode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,46 @@

(* prints ["foo", "bar"] *)
let _ =
[| "foo"; "bar" |]
|> Json.Encode.stringArray
|> Json.stringify
|> Js.log
[| "foo"; "bar" |] |> Json.Encode.stringArray |> Json.stringify |> Js.log

(* prints ["foo", "bar"] *)
let _ =
[| "foo"; "bar" |]
|> Js.Array.map Json.Encode.string
|> Js.Array.map ~f:Json.Encode.string
|> Json.Encode.jsonArray
|> Json.stringify
|> Js.log

(* prints { x: 42, foo: 'bar' } *)
let _ =
Json.Encode.(
object_ [
("x", int 42);
("foo", string "bar")
]
|> Js.log
)
let _ = Json.Encode.(object_ [ ("x", int 42); ("foo", string "bar") ] |> Js.log)

(* Advanced example: encode a record *)
type line = {
start: point;
end_: point;
thickness: int option
}
and point = {
x: float;
y: float
}
type line = { start : point; end_ : point; thickness : int option }
and point = { x : float; y : float }

module Encode = struct
let point r =
let open! Json.Encode in (
object_ [
("x", float r.x);
("y", float r.y)
]
)
let open! Json.Encode in
object_ [ ("x", float r.x); ("y", float r.y) ]

let line r =
Json.Encode.(
object_ [
("start", point r.start);
("end", point r.end_);
("thickness", match r.thickness with Some x -> int x | None -> null)
]
)
object_
[
("start", point r.start);
("end", point r.end_);
("thickness", match r.thickness with Some x -> int x | None -> null);
])
end

let data = {
start = { x = 1.1; y = -0.4 };
end_ = { x = 5.3; y = 3.8 };
thickness = Some 2
}
let data =
{
start = { x = 1.1; y = -0.4 };
end_ = { x = 5.3; y = 3.8 };
thickness = Some 2;
}

let _ =
data
|> Encode.line
|> Js.log
|> Js.log
38 changes: 19 additions & 19 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions src/Json_decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ let pair decodeA decodeB json =
decodeB (Array.unsafe_get source 1) )
with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin pair/tuple2")
else
let length = Js.String.make length in
raise
@@ DecodeError
{j|Expected array of length 2, got array of length $length|j}
(DecodeError
{j|Expected array of length 2, got array of length $length|j})
else raise @@ DecodeError ("Expected array, got " ^ _stringify json)

let tuple2 = pair
Expand All @@ -91,9 +92,10 @@ let tuple3 decodeA decodeB decodeC json =
decodeC (Array.unsafe_get source 2) )
with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin tuple3")
else
let length = Js.String.make length in
raise
@@ DecodeError
{j|Expected array of length 3, got array of length $length|j}
(DecodeError
{j|Expected array of length 3, got array of length $length|j})
else raise @@ DecodeError ("Expected array, got " ^ _stringify json)

let tuple4 decodeA decodeB decodeC decodeD json =
Expand All @@ -108,9 +110,10 @@ let tuple4 decodeA decodeB decodeC decodeD json =
decodeD (Array.unsafe_get source 3) )
with DecodeError msg -> raise @@ DecodeError (msg ^ "\n\tin tuple4")
else
let length = Js.String.make length in
raise
@@ DecodeError
{j|Expected array of length 4, got array of length $length|j}
(DecodeError
{j|Expected array of length 4, got array of length $length|j})
else raise @@ DecodeError ("Expected array, got " ^ _stringify json)

let dict decode json =
Expand Down Expand Up @@ -164,7 +167,7 @@ let oneOf decoders json =
match decoders with
| [] ->
let formattedErrors =
"\n- " ^ Js.Array.joinWith "\n- " (Array.of_list (List.rev errors))
"\n- " ^ Js.Array.join ~sep:"\n- " (Array.of_list (List.rev errors))
in
raise
@@ DecodeError
Expand Down

0 comments on commit bd9532e

Please sign in to comment.