I am currently working on mapping a list of .Net objects to my Angular frontend using Web Api 2. Depending on the circumstances, these objects and their properties may be an Employment reference, Assignment reference, or an Assignment organization unit reference.
Here is an illustration of how the list of objects can appear, with one AssignmentHolder that could belong to any of those three classes and a list of DependentEntities that could also be from one of those three classes. https://i.sstatic.net/etjpb.png Below is how they are structured in my Angular application:
This is the object that contains them:
@JsonObject('AssignmentListItem')
export class AssignmentListItem {
@JsonProperty('AssignmentHolder')
AssignmentHolder: any = undefined;
@JsonProperty('DependentEntities')
DependentEntities: any = undefined;
@JsonProperty('AssignmentRoles', [AssignmentRole])
AssignmentRoles: AssignmentRole[] = undefined;
@JsonProperty('NumberOfDependentEntities', Number)
NumberOfDependentEntities: Number = undefined;
@JsonProperty('IsInherited', Boolean)
IsInherited: boolean = undefined;
}
These are the different classes:
@JsonObject('ReferenceBase')
export class ReferenceBase {
@JsonProperty('OrganizationRegistrationNumber', OrganizationRegistrationNumber)
OrganizationRegistrationNumber: OrganizationRegistrationNumber = undefined;
@JsonProperty('IsIncomplete', Boolean)
IsIncomplete: Boolean = undefined;
@JsonProperty('SortingName', String)
SortingName: string = undefined;
}
-------
@JsonObject('EmploymentReference')
export class EmploymentReference extends ReferenceBase {
@JsonProperty('NationalCivicRegistrationNumber', String)
NationalCivicRegistrationNumber: NationalCivicRegistrationNumber = undefined;
...
-----
@JsonObject('AssignmentReference')
export class AssignmentReference extends ReferenceBase {
...
------
@JsonObject('AssignmentOrganizationalUnitReference')
export class AssignmentOrganizationalUnitReference extends ReferenceBase {
...
These are the objects I aim to map depending on the content in the assignment list.
This is a custom DTO for utilizing a custom converter:
@JsonObject('AssignmentsDto')
export class AssignmentsDto {
...
And this is the JsonCustomConverter used:
@JsonConverter
export class ObjectConverter implements JsonCustomConvert<AssignmentListItem[]> {
...
Finally, here is the Assignment Api Service where the converter is applied:
// GET LIST OF ASSIGNMENTS
...
private convertData(res: Response) {
...
The error displayed in the console is:
A fatal error has occurred in JsonConvert. The JSON object failed to map to the JavaScript class "AssignmentsDto" due to a type error. Class property: AssignmentList Expected type: undefined JSON property: AssignmentList JSON type: [object, object, object, object, object]