Open miguelsilv opened 6 years ago
MyClass?
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 ({})
Can you provide a complete test case please?
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.
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?
I solved this issue by defining the array property type from this:
...
@JsonProperty()
coordinates: any[] = undefined;
...
to this:
...
@JsonProperty()
coordinates: Array<any> = undefined;
...
api = { propy:"test", array:[ { atr:"hello" }, { atr:"hi" } ] }
deserialize = ObjectMapper.deserialize(MyClass, api) console.log(deserialize);
returns =>
{ propy:"test", array:[ {}, {} ] }