json-typescript-mapper icon indicating copy to clipboard operation
json-typescript-mapper copied to clipboard

Deserialize json doesn' work for a class with parent constructor

Open pierre03 opened this issue 6 years ago • 0 comments

Hello, Here is my problem, i have a json object, which is like that:

{"commandId":"1","result":"ok","devices":[{"PluginId":"Device 1","DisplayName":"Device 1"},{"PluginId":"Device 2","DisplayName":"Device 2"}],"message":""}

The commandId and result is defined in GeneralResponse, for the other parameters, here is the class:

export class PluginParam {
  @JsonProperty('**PluginId**')
  public **pluginId**: string;
  @JsonProperty('**DisplayName**')
  public **displayName**: string;

  constructor() {
    this.PluginId = undefined;
    this.DisplayName = undefined;
  }
}
export class GetDevicesResponse extends GeneralResponse {
  @JsonProperty({clazz: PluginParam, name: 'devices'})
  public devices: PluginParam[];
  public message: string;
  constructor() {
    super();
    this.devices = void 0;
    this.message = void 0;
  }
}

When i get the jsonobject, i do this to use it:

processGetDevice(object: GetDevicesResponse): void {
  console.log('processGetDevice, devices', object.devices);
  console.log('processGetDevice', object);
  console.log('processGetDevice, devices 0 ', object.devices[0].PluginId);
  if (object.result === 'ok' || object.devices.length > 0) {
    this.selectedDevice = object.devices[0].PluginId;
      } else if (object.result === 'error') {
    this.toasterService.pop('error', 'processGetDevice', object.message);
  }
}

But i can not get the pluginId and the displayName, it seems that the JsonProperty doesn't work here. When i replace pluginId by PluginId and displayName by DisplayName, everything is ok. Do you have any idea, please? Is it related to the function super()? Thank you.

pierre03 avatar Apr 03 '18 10:04 pierre03