Function JsonProperty

  • The basic decorator for simple properties.

    It basically takes the incoming property value, applies eventual custom serialization or deserialization, and then sets the property value to the result.

    params can be:

    Usage examples:

    import { JsonClass, JsonMapper, JsonProperty } from '@mdcc/at-json';

    @JsonClass()
    class MyClass
    {
    @JsonProperty()
    basicProperty: string;

    @JsonProperty('extName')
    renamedProperty: number;

    @JsonProperty({
    name: 'custom',
    serialize: (mapper, n) => n.toString(),
    deserialize: (mapper, n) => parseInt(n, 10)
    })
    customProperty: number;
    }

    const mapper = new JsonMapper();
    const backendObject = { basicProperty: 'value', extName: 123, customProperty: '456' };
    const deserialized = mapper.deserialize<MyClass>(MyClass, backendObject);

    // basicProperty keeps the same name
    assert.equal(deserialized.basicProperty, 'value');
    // extName became renamedProperty
    assert.equal(deserialized.renamedProperty, 123);
    // customProperty became custom, and the string was converted to number
    assert.equal(deserialized.custom, 456);

    const backendObjectSerialized = mapper.serialize(deserialized);
    // reverse conversion was performed
    assert.deepEqual(backendObjectSerialized, backendObject);

    Parameters

    Returns PropertyDecorator

    the decorator for the property.

    Export

Generated using TypeDoc