Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
JSON.prune is a pruning JSON.stringify for the very specific cases where you need to stringify big or recursive javascript objects and don't really need the result to be complete.
var json = JSON.stringify(window); // fails
var json = JSON.prune(window); // builds a JSON valid string from a pruned version of the
// recursive, deep, and not totally accessible window object
var prunedWindow = JSON.parse(JSON.prune(window)); // builds a lighter acyclic version of window
JSON.prune also lets you, in case of need, stringify inherited and/or non enumerable properties.
JSON.prune(window.location, {inheritedProperties:true}); // without inherited properties, FireFox and IE only show an empty object
JSON.prune.log is a proxy over console.log deep cloning the objects (using JSON.prune) before logging them, in order to avoid the delay problem encountered on non primitive objects logging.
You should not use it frequently, only when you really need to see the objects how they were at logging time.
// make sure someObject is logged as it was at logging time
JSON.prune.log(someObject);
var prune = require('json-prune');
var json = prune(obj);
Discussions
JavaScript room on Miaou
Customization: replace the "-pruned-" placeholder
Here's how are handled by default by JSON.prune the special values needing pruning:
Value
Default
undefined
Key and value are ommited (same as JSON.stringify)
function
Key and value are ommited (same as JSON.stringify)
already written or too deep object (cycle prevention)
The "-pruned-" string
array with too many elements
Truncation: JSON.prune applied to only the start of the array
By specifiying a replacer or a prunedString in JSON.prune options, you can customize those prunings.
The replacer function takes 4 arguments:
the value to replace
the default replacement value
a boolean indicating whether the replacement is due to a cycle detection (true for circular, false otherwise)
a string indicating the reason for pruning: 'circular' for circular references, 'depth' for max depth reached, or undefined for other cases (e.g. undefined values, functions, array truncation)
Returning undefined makes JSON.prune ommit the property (name and value).
The default value makes it easy to just specify the specific behavior you want instead of implementing the whole standard replacement.
Example 1: Use an object instead of a string as pruning mark
var json = JSON.prune(obj, {prunedString: '{}' });
Note: if you want a string to be inserted, don't forget the double quotes, as in '"-pruned-"'.
Example 2: Silent Pruning
If you want the pruned properties to just be ommited, pass undefined as prunedString:
var options = {replacer:function(value, defaultValue, circular, reason){
if (reason === 'circular') return '"-circular-"';
if (reason === 'depth') return '"-too-deep-"';
if (value === undefined) return '"-undefined-"';
if (Array.isArray(value)) return '"-array('+value.length+')-"';
return defaultValue;
}};
var json = JSON.prune(obj, options);
Note: the circular boolean (third argument) is kept for backward compatibility. The reason string (fourth argument) provides more granular information: 'circular' for circular references, 'depth' for max depth reached.
Example 4: Function "serialization"
var options = {replacer:function(value, defaultValue){
if (typeof value === "function") return JSON.stringify(value.toString());
return defaultValue;
}};
var json = JSON.prune(obj, options);
Example 5: Array truncation mark
The default behavior on big arrays is to silently write only the first elements. It's possible with a replacer to add a string as last element:
var obj = {arr: Array.apply(0,Array(100)).map(function(_,i){ return i+1 })}
function replacer(value, defaultValue){
if (Array.isArray(value)) return defaultValue.replace(/]$/, ',"-truncated-"]');
return defaultValue;
}
var json = (asPrunedJSON(obj, {arrayMaxLength:5, replacer});
TopGit chưa ghi nhận license cho Canop/JSON.prune. Phần lớn repo public trên GitHub là mã nguồn mở, nhưng điều khoản khác nhau từng repo — mở file LICENSE để xác nhận.
Canop/JSON.prune là gì?
Canop/JSON.prune (Canop/JSON.prune) là dự án đa ngôn ngữ TopGit theo dõi. Tính tới lần đồng bộ gần nhất, repo có 166 sao.
Đọc thêm về Canop/JSON.prune ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/Canop/JSON.prune là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
JSON.prune có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về JSON.prune.