1.1.1 • Published 9 months ago

angular-json-refs-interceptor v1.1.1

Weekly downloads
5
License
ISC
Repository
github
Last release
9 months ago

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