shakilsiraj / json-object-mapper

A TypeScript library to serialize and deserialize object graph from/to JSON in a fast and non-recursive way
MIT License
58 stars 18 forks source link

Serialize Array empty #34

Open miguelsilv opened 6 years ago

miguelsilv commented 6 years ago

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

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

returns =>

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

shakilsiraj commented 6 years ago

MyClass?

miguelsilv commented 6 years ago

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 ({})

shakilsiraj commented 6 years ago

Can you provide a complete test case please?

EhRom commented 5 years ago

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.

shakilsiraj commented 5 years ago

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?

quidproquo commented 5 years ago

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

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

to this:

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