Canop / JSON.prune

A pruning version of JSON.stringify, allowing for example to stringify window or any big or recursive object
MIT License
163 stars 27 forks source link

Bring Objects to highest level in output #9

Open bholbrook73 opened 6 years ago

bholbrook73 commented 6 years ago

A complex structure can show the highest level item as 'pruned' forcing one to sift through the json atring to find the one unpruned source. Example: https://jsonblob.com/bf90de6d-caf5-11e7-8bfc-27c198b81400

In the example, the root level "server" is pruned and the original is somewhere in the json with a key NOT labeled "server" making it VERY difficult to find. It would be much better if the root level server had the object.

Canop commented 6 years ago

Am I right in saying that what you want is having, for every set of positions an object is referred to, to keep the one which is at the lowest depth ?

If so this is an interesting request.

bholbrook73 commented 6 years ago

Yeah. If by lowest you mean lowest index.

Current _object: { server: { ... } }, server: "-pruned-"

Desired: _object: { server: "-pruned-" }, server: { ... }

Canop commented 6 years ago

The request is clear.

The problem is that to do this I'd need to handle the object to stringify in two times : a BFS traversal to know where to keep and where to prune, followed by the DFS traversal which is the natural order of JSON writing.

This is feasible but not totally trivial to minimize the impact on performance.

Canop commented 6 years ago

As the changes envisioned here would take some time to code and wouldn't be without impact on the library size and efficiency, I'd like to have more feedback from users before diving into it.

Please comment here or on Miaou.

ralt commented 6 years ago

Could this be opt-in?

Canop commented 6 years ago

For the performance, yes. But not reasonably for the library size.