angulartypescriptangular-httpclientangular-json

Angular HttpClient SerializedName of fields in model


Is there any way to add some attribute/annotation to model class field to mark what is serialized name (question based on @SerializedName from Gson) of field in json response from server?

E.g. response from server contains fields like start_date or some_date and I would like to have fields in my model named startDate or acquisitionDate. In my Spring server fields in response model are annotated with @SerializedName("start_date").

I'm using Angular 10 and HttpClient like this:

httpClient.get<MyModel[]>(environment.apiUrl + '/getData')
            .pipe(map(models => models.map(m => Object.assign(new MyModel(), m)));

Solution

  • I found and used library https://github.com/typestack/class-transformer Also if anyone needs, there similiar libraries https://github.com/weichx/cerialize and https://github.com/kmathy/cerializr