Simple way to serialize objects to JSON in TypeScript

When we’re building an application we often need to serialize objects to JSON for storing them in a key value store (e.g. Redis) or publishing them on a queue.

While there are libraries available like class-transformer or io-ts, there’s an easier way …


This content originally appeared on DEV Community and was authored by Hans Ott

When we’re building an application we often need to serialize objects to JSON for storing them in a key value store (e.g. Redis) or publishing them on a queue.

While there are libraries available like class-transformer or io-ts, there's an easier way to serialize/unserialize objects to JSON.

class-transformer depends on the reflect-metadata package. Read this blogpost to know why you should be careful with depending on this package.

io-ts requires knowledge of some functional programming concepts. While I'm a big fan of functional programming, it's not for everyone. (The library is very well designed though)

In both cases we also need to depend on an extra package.

We want to keep our dependencies as small as possible.

What none of these packages offer is a way to deal with backwards compatibility (e.g. when you've added or renamed a property of an object).

I also want something simple.

Let's say we want to serialize a user with an email value object to JSON:

class Email {
  constructor(private readonly email: string) {
    if (!this.email.includes("@")) {
      throw new Error(`Not an email address: ${this.email}`);
    }
  }

  asString() {
    return this.email;
  }
}
class User {
  constructor(
    private readonly id: string,
    private readonly email: Email
  ) {
    if (!this.id) {
      throw new Error("User ID cannot be empty!");
    }
  }

  private toObject() {
    return {
      id: this.id,
      email: this.email.asString(),
    }
  }

  serialize() {
    return JSON.stringify(this.toObject());
  }

  static fromSerialized(serialized: string) {
    const user: ReturnType<User["toObject"]> = JSON.parse(serialized);

    return new User(
      user.id,
      new Email(user.email)
    );
  }
}
const user = new User("id", new Email("john.doe@acme.com"));
const json = user.serialize();
const json = await redis.get(key);
const user = User.fromSerialized(json);
  • We add a private toObject() method that returns a plain object for the User (since JSON is not aware of classes).
  • We add a serialize() method that returns the plain object as a JSON string.
  • We add a static unserialize(serialized: string) method to recreate the User instance. JSON.parse has any as return type, which results in no type checking or autocompletion. We can grab the return type of the toObject() method with ReturnType<User["toObject"]> to regain type checking/autocompletion.
  • Always guard against invalid state in your entities. This makes sure that a User always has an ID and a valid email address.
  • Look ma, no packages needed!

Let me know if this blogpost was useful! ?


This content originally appeared on DEV Community and was authored by Hans Ott


Print Share Comment Cite Upload Translate Updates
APA

Hans Ott | Sciencx (2021-06-24T14:21:09+00:00) Simple way to serialize objects to JSON in TypeScript. Retrieved from https://www.scien.cx/2021/06/24/simple-way-to-serialize-objects-to-json-in-typescript/

MLA
" » Simple way to serialize objects to JSON in TypeScript." Hans Ott | Sciencx - Thursday June 24, 2021, https://www.scien.cx/2021/06/24/simple-way-to-serialize-objects-to-json-in-typescript/
HARVARD
Hans Ott | Sciencx Thursday June 24, 2021 » Simple way to serialize objects to JSON in TypeScript., viewed ,<https://www.scien.cx/2021/06/24/simple-way-to-serialize-objects-to-json-in-typescript/>
VANCOUVER
Hans Ott | Sciencx - » Simple way to serialize objects to JSON in TypeScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/24/simple-way-to-serialize-objects-to-json-in-typescript/
CHICAGO
" » Simple way to serialize objects to JSON in TypeScript." Hans Ott | Sciencx - Accessed . https://www.scien.cx/2021/06/24/simple-way-to-serialize-objects-to-json-in-typescript/
IEEE
" » Simple way to serialize objects to JSON in TypeScript." Hans Ott | Sciencx [Online]. Available: https://www.scien.cx/2021/06/24/simple-way-to-serialize-objects-to-json-in-typescript/. [Accessed: ]
rf:citation
» Simple way to serialize objects to JSON in TypeScript | Hans Ott | Sciencx | https://www.scien.cx/2021/06/24/simple-way-to-serialize-objects-to-json-in-typescript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.