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

Handle arrays, functions, and custom string for replacement #4

Closed risq closed 8 years ago

risq commented 8 years ago

Hi, First, thank you for the lib, it has been very useful for some projects !

I had to edit it a little to make it fit to my needs, so I thought I could share it. Here are the changes :

Here is an example :

var obj = {
  key1: "111",
  key2: {
    arrayValue: [1, 2, 3],
    intValue: 1,
    objectValue: {
      key: "val",
    }
  },
  key3: function() {
    return 'ok';
  }
}

var pruned = JSON.parse(prune(obj, 2, 50));

Before:

pruned = {
  key1: "111",
  key2: {
    arrayValue: "-pruned-",
    intValue: 1,
    objectValue: "-pruned-"
  }
}

After:

pruned = {
  key1: "111",
  key2: {
    arrayValue: "Array (pruned)",
    intValue: 1,
    objectValue: "Object (pruned)"
  },
  key3: "Function (pruned)"
}

Using a custom string:

var pruned = JSON.parse(prune(obj, 2, 50, '...'));

pruned = {
  key1: "111",
  key2: {
    arrayValue: "...",
    intValue: 1,
    objectValue: "..."
  },
  key3: "..."
}
Canop commented 8 years ago

Hi, Having a custom string for pruned values seems useful (it might even be a well chosen single character, for a lighter JSON) but I don't understand what problems are solved by your two other changes. Note: it might be easier to discuss this in the Javascript room of Miaou: https://dystroy.org/miaou/8

Canop commented 8 years ago

Following our discussion on Miaou, I've made a new branch. See relevant documentation: https://github.com/Canop/JSON.prune/tree/custom-replacement#customization-replace-the--pruned--placeholder

Please review this branch and confirm it meets your needs.

risq commented 8 years ago

That's exactly what I wanted ! It fits well to my needs. Thanks again.

Canop commented 8 years ago

OK, I'll merge ASAP.