I'm currently switching to the new HttpClient of Angular 4.3. One advantage is that I can specify a type info on the GET method and that the returned JSON is parsed into the given type, e.g.
this.http.get<Person> (url).subscribe(...)
But unfortunately, all dates in the JSON are parsed as numbers in the resulting object (probably because the Java Date objects are serialized as numbers in the backend).
With the old Http I used a reviver function when calling JSON.parse() like this:
this.http.get(url)
.map(response => JSON.parse(response.text(), this.reviver))
and in the reviver function I created date objects from the numbers:
reviver (key, value): any {
if (value !== null && (key === 'created' || key === 'modified'))
return new Date(value);
return value;
}
Is there a similar mechanism with the new HttpClient? Or what is the best practice to do conversion when the JSON is parsed?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…