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

js-infinite-json

v0.0.1

Published

Circular JSON handler. Parse and stringify objects containing circular references

Downloads

5

Readme

InfiniteJSON

Circular JSON handler. Parse and stringify objects containing circular references

What is this for?

When using JSON.parse, have you ever gotten this before?

TypeError: Converting circular structure to JSON

Not friendly huh? This is caused because you have an object following the next circular pattern:

// Create an object
var obj = {
	a: 2
	, b: "Hello World"
}

// Make it circular
obj.c = obj;

If you use console.log to output the object, you will see that it is an infinite structure as you keep on drilling down into it.

This module is inteded to solve this issue by exposing to you a 'parse' and 'stringify' method, analogous to the methods from the native JSON object in modern browsers. In other words, you will be able to parse and rebuild circular objects.

How do I use it?

Normally you would do something similar to:

// To create a JSON
InfiniteJSON.stringify( obj );

// To read a JSON
InfiniteJSON.parse( json );

Is it better than JSON.parse and JSON.stringify?

No. Use native JSON object when possible (when you are not dealing with circular objects).

What implementation can I do with this?

Well, I'll let that up to you. A simple example could be.

Say you have a backend endpoint. The endpoint that deals with three resources/models like the following:

Category           : Just cause... the user wants categories for his sandwiches
	id
	user_id

Sandwich           : A type of sandwich the user likes and is part of a category
	id
	category_id
	name
	description

SandwichIngredient : An ingredient for a sandwich
	id
	sandwich_id
	name
	description

And say you have a Single Page Application, and you want to do all sorts of views on this data.

One way of handling such data provided by an endpoint, is to obtain the data in JSON, and assign it to three different values during javascript runtime. Now you have two options:

1: Match IDs! "Yay"... Now you can match IDs every time you want to perform some sort of relation creation, to perform a CRUD action on data, and use a library like lodash to ease finding based on ids.

-OR-

2: You could also based on the three values create a circular object, and then utilize the circular object to to access the data.

Say you went for option 2, because iterating on your data over and over is not a nice thing to do.

You can now decide to improve initial response time on you application by allowing the application to utilize the native Local Storage API, by just storing the circular JSON into an item. This way when loading occurs again, you may avoid the initial wait time to retreive the data from your endpoint, by just loading the structure from Local Storage for sandwich ingredients that are associated to sandwiches, which are associated to categories, and were previously stringified and stored in a nice little JSON array. You will have loaded a circular object that is now easy to manipulate, and from which it is easier to access data.

This will allow you to run your backen query to update the JavaScript object, without the user feeling any initial response dealy. Your user will probably be happier if he has a terrible weather condition, and he is on his mobile device trying to figure out which is his favorite sandwich so he may ask for it at the airport.

I guess that is an implementation of this module, I used it differently though. With a 3MB strucutre extensive structure, which should have probably been implemented differently, if I would have realized it on time.

Hope you find some use for this module :)

Can I provide some callback during the deserialization?

Yup. Do something like this:

// Parse the structure
var teachers = [];
var subjects = [];
InfiniteJSON.parse.findAndSolvePointers( someJSONString, null, function( key,value ) {

	// Is this a teacher?
	if( key === 'teacher' && typeof value === 'object'  )
	{ 	
		// Just cause its fun to overload the global scope
		teachers.push( value );
	}
	// Is this a subject?
	else if( typeof value === 'object' && value && value.hasOwnProperty('topics') && value.hasOwnProperty('teacher') )
	{
		subjects.push( value );
	}

	// Give the value back
	return value;

});