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

Serialize Array empty

Open miguelsilv opened this issue 7 years ago • 6 comments

api = { propy:"test", array:[ { atr:"hello" }, { atr:"hi" } ] }

deserialize = ObjectMapper.deserialize(MyClass, api) console.log(deserialize);

returns =>

{ propy:"test", array:[ {}, {} ] }

miguelsilv avatar Jul 25 '18 19:07 miguelsilv

MyClass?

shakilsiraj avatar Jul 26 '18 06:07 shakilsiraj

Every time the object has an array, it is not possible to deserialize. considering the class name of MyClass, where it has an array, with values coming from an api. When I deserialize it it becomes empty ({})

miguelsilv avatar Jul 26 '18 11:07 miguelsilv

Can you provide a complete test case please?

shakilsiraj avatar Jul 26 '18 12:07 shakilsiraj

I get a similar issue with array serialization. When I specify the JsonProperty attribute on an object array, I get an single object :

import { JsonProperty } from 'json-object-mapper';
import ServerHealthReport from '@/models/ServerHealthReport';

export default class PlatformHealthReport {
  public creationDate: Date;
  public endControlDate: Date;
  @JsonProperty({ type: ServerHealthReport, name: 'servers' })
  public servers: ServerHealthReport[];

  constructor() {
    this.creationDate = new Date();
    this.endControlDate = new Date();
    this.servers = new Array<ServerHealthReport>();
  }
}

When removing the attribute, it works fine, I get a regular array. It seems to be declared like in the example given on the main page of the github repo.

EhRom avatar Oct 17 '18 14:10 EhRom

Hi @EhRom,

Your code should be written as followed:


it("Defect 34", () => {
    class ServerHealthReport {
      @JsonProperty({ type: Date, serializer: DateSerializer })
      creationDate: Date = new Date(1541585888587);
      @JsonProperty({ type: Date, serializer: DateSerializer })
      endControlDate: Date = new Date(1541585888587);
    }

    class PlatformHealthReport {
      @JsonProperty({ type: Date, serializer: DateSerializer })
      public creationDate: Date = undefined;
      @JsonProperty({ type: Date, serializer: DateSerializer })
      public endControlDate: Date = undefined;
      @JsonProperty({ type: ServerHealthReport, name: "servers" })
      public servers: ServerHealthReport[] = undefined;

      constructor() {
        this.creationDate = new Date(1541585888587);
        this.endControlDate = new Date(1541585888587);
        this.servers = new Array<ServerHealthReport>();
      }
    }

    const instance = new PlatformHealthReport();
    expect(ObjectMapper.serialize(instance)).toBe(
      '{"creationDate":1541585888587,"endControlDate":1541585888587,"servers":[]}'
    );

    instance.servers.push(new ServerHealthReport());
    instance.servers.push(new ServerHealthReport());
    expect(ObjectMapper.serialize(instance)).toBe(
      '{"creationDate":1541585888587,"endControlDate":1541585888587,"servers":[{"creationDate":1541585888587,"endControlDate":1541585888587},{"creationDate":1541585888587,"endControlDate":1541585888587}]}'
    );
  });

Is it not the expected behaviour?

shakilsiraj avatar Nov 09 '18 08:11 shakilsiraj

I solved this issue by defining the array property type from this:

...
@JsonProperty()
coordinates: any[] = undefined;
...

to this:

...
@JsonProperty()
coordinates: Array<any> = undefined;
...

quidproquo avatar Mar 27 '19 01:03 quidproquo