Serializing and deserializing complex structures

In some cases, you might want to serialize or deserialize complex data structures. Most of the time, you will have a crate that does this for you (such as the chrono crate for dates and times). But in some cases this is not enough. Suppose you have a data structure that has a field that can take a value of either 1 or 2, and that each of them means something different. In Rust, you would use an enumeration for it, but we may not always have control of external APIs, for example.

Let's look at this structure:

{    "timestamp": "2018-01-16T15:43:04",    "type": 1,}

And let's say we have some code, almost ready to compile, which represents this structure:

#[derive(Debug)]enum DateType {    FirstType,

Get Rust High Performance 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.