0.1.0-0 • Published 1 year ago

json-serialize-refs v0.1.0-0

Weekly downloads
1
License
MIT
Repository
-
Last release
1 year ago

json-serialize-refs

NPM JavaScript Style Guide

serialize and deserialize preserving refs ( resolving circular references automatically ) compatible with Newtonsoft.Json for NET

install

yarn add json-serialize-refs

usage

prototypes:

/** convert given obj to json resolving references as specified by preserveType ( NewtonJson NET compatible ) */
function stringifyRefs(obj: any, replacer: any = null, space: any = null, preserveType: PreserveType = PreserveType.All);

/** convert back from json to object reconnecting references if any ( Newtonsoft JSON compatible ) */
function parseRefs(text: string, reviver?: (this: any, key: string, value: any) => any);

/** helper for fetch json text and parse */
function parseRefsResponse<T = any>(jsonPromise: Promise<string>): Promise<T>;

serialize/deserialize example:

import { stringifyRefs, parseRefs, PreserveType } from "json-serialize-refs";

interface test1_aobj_type {
    strVal: string;
}

interface test1_type {
    dt: Date;    
    boolTrue: boolean;
    boolFalse: boolean;
    nr: number;
    aobj: test1_aobj_type;
    selftest: test1_type;
    value: string;
    arrdata: (test1_type | string)[];
    selfarray: (test1_type | string)[];
    arrdata2: ((test1_type | string)[] | string)[];
}

const obj = {} as test1_type;
obj.dt = new Date("2019-12-31T23:59:59Z");
obj.boolTrue = true;
obj.boolFalse = false;
obj.nr = 3.14159;
obj.aobj = { strVal: "some" };
obj.selftest = obj;
obj.value = "sample string";
obj.arrdata = [obj, "other"];
obj.selfarray = obj.arrdata;
obj.arrdata2 = [obj.arrdata, "another"];

const json = stringifyRefs(obj, null, 1, PreserveType.All);

console.log("ORIG STRINGIFIED:");
console.log("-----------------------");
console.log(json);

const obj2 = parseRefs(json);
const json2 = stringifyRefs(obj2, null, 1, PreserveType.All);

console.log("OBJ2 STRINGIFIED:");
console.log("-----------------------");
console.log(json2);

console.log("VAL TEST RESULT:");
console.log("-----------------------");
console.log("dt eq: " + String(obj.dt.toString() === obj2.dt.toString()));
console.log("boolTrue eq: " + String(obj.boolTrue === obj2.boolTrue));
console.log("boolFalse eq: " + String(obj.boolFalse === obj2.boolFalse));
console.log("nr eq: " + String(obj.nr === obj2.nr));

console.log("SELF TEST RESULT:");
console.log("selftest eq: " + String(obj2.selftest === obj2));
console.log("arrdata[0] eq: " + String(obj2.arrdata[0] === obj2));
console.log("selfarray eq: " + String(obj2.selfarray === obj2.arrdata));
console.log("arrdata2[0] eq: " + String(obj2.arrdata2[0] === obj2.arrdata));

output:

ORIG STRINGIFIED:
-----------------------
{
 "$id": "1",
 "dt": "2019-12-31T23:59:59.000Z",
 "boolTrue": true,
 "boolFalse": false,
 "nr": 3.14159,
 "aobj": {
  "$id": "2",
  "strVal": "some"
 },
 "selftest": {
  "$ref": "1"
 },
 "value": "sample string",
 "arrdata": {
  "$id": "3",
  "$values": [
   {
    "$ref": "1"
   },
   "other"
  ]
 },
 "selfarray": {
  "$ref": "3"
 },
 "arrdata2": {
  "$id": "4",
  "$values": [
   {
    "$ref": "3"
   },
   "another"
  ]
 }
}
OBJ2 STRINGIFIED:
-----------------------
{
 "$id": "1",
 "dt": "2019-12-31T23:59:59.000Z",
 "boolTrue": true,
 "boolFalse": false,
 "nr": 3.14159,
 "aobj": {
  "$id": "2",
  "strVal": "some"
 },
 "selftest": {
  "$ref": "1"
 },
 "value": "sample string",
 "arrdata": {
  "$id": "3",
  "$values": [
   {
    "$ref": "1"
   },
   "other"
  ]
 },
 "selfarray": {
  "$ref": "3"
 },
 "arrdata2": {
  "$id": "4",
  "$values": [
   {
    "$ref": "3"
   },
   "another"
  ]
 }
}
VAL TEST RESULT:
-----------------------
dt eq: true
boolTrue eq: true
boolFalse eq: true
nr eq: true
SELF TEST RESULT:
selftest eq: true
arrdata[0] eq: true
selfarray eq: true
arrdata2[0] eq: true

parse webapi data:

fetch('sys/doSomething',
    {
      method: 'post',
      headers: { 'Content-Type': 'application/json' },
      body: stringifyRefs({ token, nfos })
    })
    .then(response => parseRefsResponse<IData>(response.text()))
    .then(someData => {

with c# net core webapi service configured this way:

public void ConfigureServices(IServiceCollection services)
{
    //...

    services.AddMvc().AddNewtonsoftJson((o) =>
    {
        o.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.All;
    });

    //...
}

tip: use Reinforced.Typings tool to convert NET types to Typescript ( example )

how to contribute ( quickstart )

git clone https://github.com/devel0/json-serialize-refs.git
cd json-serialize-refs
yarn install
code .
  • hit F5 to start example

unit tests

launchers

from vscode CTRL+SHIFT+D follows launchers available

  • "Launch Program" : start example program
  • "Debug Jest Tests" : run unit test with ability to breakpoint on them
  • "netcore ( preserve object )" and "netcore ( preserve all )" generate newtonsoft json format into netcore folder

troubleshoots

from deserialization of All preserve type Newtonsoft generate follow error "Cannot preserve reference to array or readonly list, or list created from a non-default constructor: System.Int32[]" ( see answer ) ; a workaround is to use Objects (already the default) preserve mode.