A Complete Serialization Example

To demonstrate the practice of serializing and deserializing a domain object to and from JSON, let’s build a small example. Say that we want to persist a domain type Person that’s defined like this:

 module​ Domain = ​// our domain-driven types
 
 /// constrained to be not null and at most 50 chars
 type​ String50 = String50 ​of​ ​string
 
 /// constrained to be bigger than 1/1/1900 and less than today's date
 type​ Birthdate = Birthdate ​of​ DateTime
 
 /// Domain type
 type​ Person = {
  First: String50
  Last: String50
  Birthdate : Birthdate
  }

The String50 and Birthdate types cannot be serialized directly, so we first create a corresponding DTO type Dto.Person (a Person in a Dto module) ...

Get Domain Modeling Made Functional now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.