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

(docs) Cookbooks "Serialise JSON Data using ppx_yojson_conv" and "Deserialise JSON Data using ppx_yojson_conv" #2415

Merged
merged 16 commits into from
Jun 24, 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
40 changes: 40 additions & 0 deletions data/cookbook/deserialise-from-json/00-ppx_yojson_conv.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
packages:
- name: ppx_yojson_conv
tested_version: "v0.17.0"
used_libraries:
- ppx_yojson_conv
- name: yojson
tested_version: 2.1.2
used_libraries:
- yojson
---
(* We open `Ppx_yojson_conv_lib.Yojson_conv.Primitives` to bring the functions that deserialise primitive types in scope. *)
open Ppx_yojson_conv_lib.Yojson_conv.Primitives

let json = {|
{
"name": "ocaml",
"url": "https://ocaml.org/"
}
|}

(* The annotation `[@@deriving of_yojson]` causes the PPX from `ppx_yojson_conv` to generate a function
`language_of_yojson` that converts values from type `Yojson.Safe.t` to `language`. *)
type language = {
name: string;
url: string
} [@@deriving of_yojson]

(* First we convert the JSON string to `Yojson.Safe.t` and then we use the generated function to create the record.
If parsing fails, an `Of_yojson_error` exception is thrown that we can handle. *)
try
let result =
json
|> Yojson.Safe.from_string
|> language_of_yojson
Some result
with Ppx_yojson_conv_lib.Yojson_conv.Of_yojson_error (exception, _) ->
let _ =
Printexc.to_string exception print_endline in
None
33 changes: 33 additions & 0 deletions data/cookbook/serialise-to-json/00-ppx_yojson_conv.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
packages:
- name: ppx_yojson_conv
tested_version: "v0.17.0"
used_libraries:
- ppx_yojson_conv
- name: yojson
tested_version: 2.1.2
used_libraries:
- yojson
---
(* `Ppx_yojson_conv_lib.Yojson_conv.Primitives` contains functions needed to serialise values of primitive types. *)
open Ppx_yojson_conv_lib.Yojson_conv.Primitives

(* The annotation `[@@deriving to_yojson]` causes the PPX from `ppx_yojson_conv` to generate a function
`yojson_of_language` that converts values from type `language` to `Yojson.Safe.t`. *)
type language = {
name: string;
url: string
} [@@deriving to_yojson]

let ocaml_language = {
name: "ocaml";
url: "https://ocaml.org/"
}

(* We convert the value `ocaml_language` to a `Yojson.Safe.t`, using the function `yojson_of_language` generated by the PPX,
and then convert that to a string. *)
let () =
ocaml_language
|> yojson_of_language
|> Yojson.Safe.to_string
|> print_endline
Loading