Skip to content

Interceptor for Angular when using Json.NET's PreserveReferencesHandling

License

Notifications You must be signed in to change notification settings

JeremyMahieu/angular-json-refs-interceptor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

angular-json-refs-interceptor npm version

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

About

Interceptor for Angular when using Json.NET's PreserveReferencesHandling

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published