Key/value pairs of decoders for each object field.
How to display the name of the object being decoded in errors.
Optional
keyMap: DecoderObjectKeyMap<T>Optional map between json field names and user land field names. Useful when the client model does not match with what the server sends.
A decoder that validates and returns objects matching the specified structure
interface User {
firstName: string;
lastName: string;
age: number;
}
const userDecoder = JsonDecoder.object<User>(
{
firstName: JsonDecoder.string,
lastName: JsonDecoder.string,
age: JsonDecoder.number
},
'User',
{
firstName: 'first_name',
lastName: 'last_name'
}
);
const json = {
first_name: 'John',
last_name: 'Doe',
age: 30
};
userDecoder.decode(json); // Ok<User>({value: {firstName: 'John', lastName: 'Doe', age: 30}})
Decoder for objects with specified field decoders.