I usually have trouble serializing/deserializing
json when I first learn a staticly typed language. I had the same experience with C# and in Haskell. Luckily, there’s a lot of resources and it’s easy to understand. This time I’m learning Ocaml/Bucklescript and I’m encoutering the same thing. Good thing bucklescript’s standard library provides Js.Json.
Creating a simple
json from a record. First, create the record type.
Then create a record value.
Now, a function that will serialize the record.
To see the output in the console.
I’m gonna do the other way around in this next section. Deserializing
json! When I get a
json object I will transform it into
Let’s say this is the
json object I get.
First, I need to define a record since there’s no such thing as anonymous records in Ocaml.
[@@bs.deriving abstract] decoractor for maninpulating
I’ll create a utility function to help me extract json.
This will be my main decoding function
To simulate a
json object I’ll make
To view the output
I’ll encode a record using bs-json to turn it into a
json object. First, I’ll create the record type.
Then, the record itself.
Next, will be the function to create the
json object from the record
To view the output in the console.
I’m expecting the same
json from above.
Then, the function to transform the record to a
To see the output in the console