Reputation: 171
I have an object that I send using HttpClient who may have some null properties. I wonder if there are any build-in solution(s) in Angular that ensure that null properties are not serialized.
Lets take this model for instance:
export class Dummy {
public constructor(
public readonly foo: string,
public readonly bar: string
) { }
}
And this service:
import { HttpClient } from '@angular/common/http';
export class DummyService {
public constructor(private readonly httpClient: HttpClient) { }
public dummyMethod(dummyObject: Dummy): Observable<any> {
return this.httpClient.Post('https://api.dummy/v1/dummies', dummyObject)
}
}
If dummyObject === new Dummy("FooValue", null)
then I expect this JSON to be serialized: { "foo": "FooValue" }
Upvotes: 1
Views: 4404
Reputation: 11000
In JSON, values must be one of the following data types:
a string a number an object (JSON object) an array a boolean null
So it can't ignore null
. But we can't see undefined
here, so you can use it instead of null
and serialised object will omit it.
Upvotes: 3