Interceptor for dereferencing json $ref
and $id
tags. Useful for serilizing and deserializing circular references. The interceptor links all the objects together again in the proper order. Doing this it's possible to have the original circular references in json that you might have in .NET. This is especially usefull for projects that use Entity Framework. Due to automatically fix-up navigation properties, there can easily be many circular references.
In the Startup.cs
file of your .NET project (web API), add the PreserveReferencesHandling
option to Json Newtonsoft. Don't forget to do the same for SignalR if you use it.
services.AddControllers().AddNewtonsoftJson(o =>
{
o.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
});
Or when using System.Text.Json
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
Has to be set seperatly when using SingalR
services.AddSignalR().AddJsonProtocol(options =>
{
options.PayloadSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
Install the package
npm install angular-json-refs-interceptor
In app.module.ts
, use the interceptor.
import { RefsInterceptor } from 'angular-json-refs-interceptor';
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: RefsInterceptor, multi: true },
],
- Thanks to Gaurav for his post
- Thanks to Alexander Vasilyev for his post