Reputation: 1402
I am trying to convert Typescript Contact class back to JSON which is possible according to the documentation for the 'json2typscript' package.
serializeObject returns an empty obj.
map(res => {
const jsonConvert: JsonConvert = new JsonConvert();
return jsonConvert.deserializeObject(res, Contact);
})
This does not work by returning {}:
try {
const obj = new JsonConvert().serializeObject(contact);
console.log(obj);
} catch (err) {
console.error(err);
}
This is the class I am trying to map against:
@JsonObject('Contact')
export class Contact {
@JsonProperty('id', String, true)
id: string = undefined;
@JsonProperty('created', String, true)
created: string = undefined;
@JsonProperty('updated', String, true)
updated: string = undefined;
@JsonProperty('first_name', String, true)
firstName: string = undefined;
@JsonProperty('last_name', String, true)
lastName: string = undefined;
@JsonProperty('role', String, true)
role: string = undefined;
@JsonProperty('phone', String, true)
phone: string = undefined;
@JsonProperty('email', String, true)
email: string = undefined;
@JsonProperty('notes', String, true)
notes: string = undefined;
@JsonProperty('companies', [Company])
companies: [] = undefined;
}
Any ideas on how to do this?
Upvotes: 1
Views: 584
Reputation: 15723
As this Pull Request
If you are using an object that is not a real instance of the mapped class you should pass a second argument to serializeObject
as a reference class:
jsonConvert.serializeObject(contact, Contact);
Works on v1.3.0+
Upvotes: 1