All Projects → Canop → Json.prune

Canop / Json.prune

Licence: mit
A pruning version of JSON.stringify, allowing for example to stringify window or any big or recursive object

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Json.prune

Fblog
Small command-line JSON Log viewer
Stars: ✭ 137 (-3.52%)
Mutual labels:  json
Yii2 Json Api
Implementation of JSON API specification for the Yii framework
Stars: ✭ 139 (-2.11%)
Mutual labels:  json
Jsonc
JSON with comments for Go!
Stars: ✭ 142 (+0%)
Mutual labels:  json
Libvirt Hook Qemu
Libvirt hook for setting up iptables port-forwarding rules when using NAT-ed networking.
Stars: ✭ 137 (-3.52%)
Mutual labels:  json
Packages
📦 Package configurations - The #1 free and open source CDN built to make life easier for developers.
Stars: ✭ 139 (-2.11%)
Mutual labels:  json
Criterion
Microbenchmarking for Modern C++
Stars: ✭ 140 (-1.41%)
Mutual labels:  json
Rq
Record Query - A tool for doing record analysis and transformation
Stars: ✭ 1,808 (+1173.24%)
Mutual labels:  json
Hugejsonviewer
Viewer for JSON files that can be GBs large.
Stars: ✭ 142 (+0%)
Mutual labels:  json
Noproto
Flexible, Fast & Compact Serialization with RPC
Stars: ✭ 138 (-2.82%)
Mutual labels:  json
Dirty Json
A parser for invalid JSON
Stars: ✭ 141 (-0.7%)
Mutual labels:  json
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+1206.34%)
Mutual labels:  json
Cfgdiff
diff(1) all your configs
Stars: ✭ 138 (-2.82%)
Mutual labels:  json
Autocser
AutoCSer is a high-performance RPC framework. AutoCSer 是一个以高效率为目标向导的整体开发框架。主要包括 TCP 接口服务框架、TCP 函数服务框架、远程表达式链组件、前后端一体 WEB 视图框架、ORM 内存索引缓存框架、日志流内存数据库缓存组件、消息队列组件、二进制 / JSON / XML 数据序列化 等一系列无缝集成的高性能组件。
Stars: ✭ 140 (-1.41%)
Mutual labels:  json
Kafka Connect Mongodb
**Unofficial / Community** Kafka Connect MongoDB Sink Connector - Find the official MongoDB Kafka Connector here: https://www.mongodb.com/kafka-connector
Stars: ✭ 137 (-3.52%)
Mutual labels:  json
Bricks
A standard library for microservices.
Stars: ✭ 142 (+0%)
Mutual labels:  json
Dhooks
A simple python Discord webhook API wrapper
Stars: ✭ 136 (-4.23%)
Mutual labels:  json
Json Autotype
Automatic Haskell type inference from JSON input
Stars: ✭ 139 (-2.11%)
Mutual labels:  json
Funiture
慕课网课程推荐 Java并发编程与高并发解决方案:http://coding.imooc.com/class/195.html Java开发企业级权限管理系统:http://coding.imooc.com/class/149.html github: https://github.com/kanwangzjm/funiture, spring项目,权限管理、系统监控、定时任务动态调整、qps限制、sql监控(邮件)、验证码服务、短链接服务、动态配置等
Stars: ✭ 1,786 (+1157.75%)
Mutual labels:  json
Smoke
💨 Simple yet powerful file-based mock server with recording abilities
Stars: ✭ 142 (+0%)
Mutual labels:  json
Fig
A minimalist Go configuration library
Stars: ✭ 142 (+0%)
Mutual labels:  json

JSON.prune

Chat on Miaou Chat on Miaou

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);

Project/Test page

dystroy.org/JSON.prune

Include it in a page

<script src=http://dystroy.org/JSON.prune/JSON.prune.js></script>

Use it with CommonJS or in node

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 3 arguments:

  • the value to replace
  • the default replacement value
  • a boolean indicating whether the replacement is due to a cycle detection

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 obj = {a:3};
obj.self = obj;
var json = JSON.prune(obj);
console.log(json); // logs {"a":3,"self":"-pruned-"}
json = JSON.prune(obj, {prunedString: undefined });
console.log(json); // logs {"a":3}

Note: You get the same behavior with

json = JSON.prune(obj, {replacer: function(){}});

Example 3: Verbose Pruning

var options = {replacer:function(value, defaultValue, circular){
	if (circular) return '"-circular-"';
	if (value === undefined) return '"-undefined-"';
	if (Array.isArray(value)) return '"-array('+value.length+')-"';
	return defaultValue;
}};
var json = JSON.prune(obj, options);

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});

This produces

{"arr":[1,2,3,4,5,"-truncated-"]}

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].