Function JsonArray

  • The basic decorator for array of simple properties.

    params has the same meaning that the one in JsonProperty.

    Usage examples:

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

    @JsonClass()
    class MyClass
    {
    @JsonArray()
    basicProperty: string[];

    @JsonArray('extName')
    renamedProperty: number[];

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

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

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

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

    const errorObject = { basicProperty: {} };
    const deserializedErrorObject = mapper.deserialize<MyClass>(MyClass, errorObject);
    // basicProperty is null
    assert.isNull(deserializedErrorObject.basicProperty);

    const errorObject2 = { basicProperty: [], renamedProperty: [], customProperty: {} };
    // this throws because customProperty was decorated with `@JsonArray(..., true)`
    const deserializedErrorObject2 = mapper.deserialize<MyClass>(MyClass, errorObject2);

    Parameters

    Returns PropertyDecorator

    the decorator for the property.

    Export

Generated using TypeDoc