Member-only story

Common JSON patterns in Haskell, Rust, and TypeScript

7 min readApr 5, 2020

--

Not a Medium member? Read the post here for free.

A lot of web development is transforming JSON one way or another. In TypeScript/JavaScript, this is straightforward, since JSON is built into the language. But can we also achieve good ergonomics in Haskell and Rust?

Dear reader, I am glad you asked! 🙌

The comparisons we will see are not meant to show if one approach is better than another. Instead, it is intended to be a reference to become familiar with common patterns across multiple languages. Throughout this post, we will utilize several tools and libraries.

The core of working with JSON in Haskell and Rust is covered by:

  • Aeson: a Haskell JSON serialization/deserialization library¹.
  • Serde: a Rust JSON serialization/deserialization library.

The ergonomics is then improved in Haskell by grabbing one of the following options²:

We’ll go through typical use-cases seen in TypeScript/JavaScript codebases, and see how we can achieve the same in Haskell and Rust.

Table of Contents:

1. Preparation: Setting up our data
2. Comparison
- Get a field
- Get a nested field
- Get an optional field
- Set a field
- Set a nested field
- Set each item in a list
- Encode / Serialize
- Decode / Deserialize
3. Changelog

Preparation: Setting up our data

First, we will set up our data structures and a few examples, which we will use throughout this post. Haskell and Rust require a bit more ceremony because we will use packages/crates. For TypeScript we use ts-node to run TypeScript in a REPL.

--

--

Christian Kjær
Christian Kjær

Written by Christian Kjær

Founder of codetalk.io and blogging on codethoughts.io 👀 •• Rust/Serverless ❤️ Previously: Director of Eng at Famly 🌱 Built IoT platform at Factbird ☁️

No responses yet

Write a response