npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonreferenceresolver

v0.1.2

Published

A module to resolve circular dependencies for Newtonsoft JSON

Downloads

10

Readme

JsonReferenceResolver

This module was created to solve the problem of transfering and receiving JavaScript Objects from and to a Backend that uses Newtonsoft JSON.

This module also solves the problem with circular dependencies that often happen in a Newtonsoft JSON use-case.

Basically it iterates trough the Object and looks for objects with a id like value that can be set, if multiple objects are found with the same id all other objects are replaces by a objects that just contains a reference to the first identical found object.

This module has provides three different classes that each serve a different solution to a problem that can happen while working with Newtonsoft JSON.

These three classes are are Refifier, Resolver and IdResolver. These classes will be further explained below.

So if you would use new Refifier("$id", "$ref", 99999).refify(data) on this example object

{
    "$id": "1",
    "logicalObjectId": "0001",
    "name": "Order",
    "country": {
        "$id": "2",
        "logicalObjectId": "0002",
        "name": "USA"
    },
    "articles": [
        {
            "$id": "3",
            "logicalObjectId": "0003",
            "type": "PC",
            "manufacturer": {
                "$id": "4",
                "logicalObjectId": "0004",
                "name": "Lenovo",
                "country": {
        			"$id": "2",
                    "logicalObjectId": "0002",
        			"name": "USA"
    			}
            }
        }
    ] 
}

the result would looks like this

{
    "$id": "100000",
    "logicalObjectId": "0001",
    "name": "Order",
    "country": {
        "$id": "100001",
        "logicalObjectId": "0002",
        "name": "USA"
    },
    "articles": [
        {
            "$id": "100002",
            "logicalObjectId": "0003",
            "type": "PC",
            "manufacturer": {
                "$id": "100003",
                "logicalObjectId": "0004",
                "name": "Lenovo",
                "country": { "$ref": "100001" }
            }
        }
    ] 
}

Usage

Install the package via npm

$ npm install jsonreferenceresolver

Creating a new JsonRefResolver

The constructor of the JsonRefResolver sub packages all take different parameters.

takes in the id signature which will be used in the object as the key and the ref signature, that will be used to replace the duplicate objects.

import { Refifier, Resolver, IdResolver } from 'jsonreferenceresolver'

const refifier = new Refifier("$id", "$ref", 1000);
// takes in the `id` signature which will be used in the object as the key, the `ref` signature, that will be used to replace the duplicate objects and a seed which is the startvalue of the new `ids`.

const resolver = new Resolver("$id", "$ref");
// same as above, but you don't need a seed, because the id's wont change 

const idResolver = new IdResolver("$id", "$ref", "logicalObjectId", 2000);
// id signature, ref signature, the value that is definitly different in every object, seed

After refify or resolve are used the newIds count and the refList are not reset. If you want to reset the refList you can call refifyer/resolver.resetRefereceList() or you can make a new instance of those classes.

Building a refified object

import { Refifier } from 'jsonreferenceresolver'

const refifier = new Refifier("$id", "$ref", 1000);

const obj = {...};
const refifiedObj = refifier.refify(obj);

// if the data is a array use refifyList

const arr = [...];
const refifiedArr = refifier.refifyList(arr);

Resolving data with references

import { Resolver } from 'jsonreferenceresolver'

const resolver = new Resolver("$id", "$ref");

const obj = {...};
const refifiedObj = resolver.resolve(obj);

// if the data is a array use resolveJsonRefsList

const arr = [...];
const refifiedArr = resolver.resolveList(arr);

Resolving all Id's in a complex Object

import { IdResolver } from 'jsonreferenceresolver'

const resolver = new IdResolver("$id", "$ref", "logicalObjectId", 10000);

const obj = {...};
const refifiedObj = resolver.resolve(obj);

other functions

const refifier = new Refifier("$id", "$ref", 1000);
const resolver = new Resolver("$id", "$ref");


refifier/resolver.resetRefList() // clears just the refList manually, if the building should be done with continuous ids
refifier/resolver.getReferenceList()   // returns the refList as a Map() object or as an Array

Tests

In the test directory on GitHub

License

MIT