Using revive()

This section introduces you into model revival with a simple example.

Synopsis

import { revive } from 'revivejs'

const model = revive(json, schema, options)

Description

The revive function is used to revive models from a json string.

The arguments of the function are the following:

Argument

Type

Description

json

string

Json to deserialize

schema

RevivalSchema

A description of how to deserialize the data.

This can be either a detailed schema or an object constructor (class or function). See Complex Schemas for more details.

options

RevivalOptions

Describes how to treat the input such as missing or unknown fields.

See Options for more details.

Revival can be done in two ways

  1. By specifying a class or a function as the schema

  2. By specifying a complex schema mapping out fields along with other embedded schemas.

Simple Model Revival

The simplest way to revive an object is by specifying its constructor function or class.

import { revive } from 'revivejs'

class Person {
    name = ''
    public sayHi() {
        console.log(`Hi, I'm ${this.name}.`)
    }
}

const data = '{"name": "John Smith"}'
revive(data, Person).sayHi() // Hi, I'm John Smith.

You can use both classes and good old fashioned javascript functions.

Last updated

Was this helpful?