angular-json-refs-interceptor
v1.1.1
Published
Interceptor for Angular when using Json.NET's PreserveReferencesHandling
Downloads
175
Readme
angular-json-refs-interceptor
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.
Usage
In .NET
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;
});
In Angular
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
- Thanks to Gaurav for his post
- Thanks to Alexander Vasilyev for his post