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.
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.
Last updated
Was this helpful?