Map Schema
Reviving maps
To revive a map with string keys and any kind of data as values, the map schema can be used, to describe the type of the value expected in the map.
The values of the map have to be all of the same type i.e.
// bad
{
"foo": [ 1, 2, 3],
"bar": "John Smith",
"baz": {
"prop": "value"
}
}
// good
{
"john": {
"name": "John Smith",
"email": "john@yahoo.com"
},
"mary": {
"name": "Mary Poppins",
"email": "mary@gmail.com"
}
}
The map
schema is defined as
interface RevivalMapSchema<T> {
map: RevivalSchema<T>
}
Basically, we need to specify a map
property which holds the schema information for all map values.
Here's an example of how to use it
class Prop {
value = 0
getValue() { return this.value }
}
class Person {
name = ''
props: {[key: string]: Prop} = {}
getName() { return this.name }
getProps() { return this.props }
static getRevivalSchema(): RevivalSchema<Person> {
return {
type: Person,
properties: {
props: {
map: Prop
}
}
}
}
}
const data= '{"name": "John", "props": {"foo": {"value": 1}, "bar": {"value": 2}}}'
const p = revive(data, Person)
In the example above, props
is a map with values of type Prop
.
Last updated
Was this helpful?