Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Melange v3 long-lived branch #6

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading