How can I perform json deserialization in F#?

Methodology

Just for the sake of going fully functional, you could use a monadic parser to perform the JSON deserialization .

serializer_workflow

An example

Let’s start from a simple example of json

{
    "items": [
        {
            "snippet": {
                "title": "Giraffe in VS 20017",
                "tags": [
                    "#hangoutsonair",
                    "Hangouts On Air",
                    "#hoa"
                ]
            }
        },
        {
            "snippet": {
                "title": "Yet another title",
                "tags": [
                    "#trythisnow",
                    "Try This Now",
                    "#ttn"
                ]
            }
        }
    ]
}

Target type

So assuming that you have defined the target type

type Snippet = { title: String; tags: String list }

JSON helpers

together with some helpers

let jparseObject (JSObject items) = items
let jparseObjName jname = jparseObject >> Map.find jname
let jparseArray mapper (JSArray items ) = items |> Seq.map mapper
let jparseArrName jname mapper = Map.find jname >> jparseArray mapper
let jparseString (JSString str) = str
let jparseStrName jname = Map.find jname >> jparseString

Solution

then you would end up doing

let json2snippets  = jparseObjName @"""items"""  >>
    jparseArray (jparseObjName @"""snippet""" >>
    jparseObject >> fun snippet ->
        {title =snippet |> jparseStrName @"""title""";
        tags=snippet |> jparseArrName  @"""tags""" jparseString |> Seq.toList })

where

(json2snippets parsed |> Seq.toList)

is a Snippet list.

2 thoughts on “How can I perform json deserialization in F#?

  1. Great comment!

    JS as a language still maintains many aspects of its origins, when it was specifically designed to run with rather sparse resources, like for instance, DTOs in JS are basically jagged arrays, or if you will, very simple objects based on a composition of arrays of arrays.

    Besides this, JS code should be designed in such a way that it can work even in browsers with few resources at hand. So, a process like serialization/deserialization to/from JSON presents itself a priori like an algorithmic challenge, and this is where some abstract concepts come to the rescue => functional programming.

    In what way does functional programming overlap with this (serialization/deserialization of JS objects to/from JSON)?

    The fact that is it possible to imagine (and design), solely based on functional programming (like in Haskell or F#), a parser capable of managing complex grammars that can describe the state of domain objects is how these two subjects meet and match.

    There are some tricks that help in this respect, that is, there are some basic patterns of object composition that can be used to abstract (simplify) this in a way that is at the same time very “object-oriented” and very “function-oriented”.

    All this can be applied in very much the same way at both the client side (in JS) and at the server side (in F#, o Haskell, or whatever you care to choose to develop server code)

    All of this is so clearly expressed in your post.

    Thanks a lot for the post.

    Kind regards, GEN

    Liked by 1 person

    • Many thanks to you for reading and commenting 👍 Your explanation – in particular in this case when you point out the similarities with JS and the importance of optimised usage of resources – is much appreciated, as usual!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s