prettyjson
Version:
Package for formatting JSON data in a coloured YAML-style, perfect for CLI output
366 lines (306 loc) • 14.9 kB
HTML
<html>
<head>
<title>prettyjson - Node.js package for formatting JSON data in a coloured YAML-style, perfect for CLI output</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" media="all" href="docco.css" />
<link rel="stylesheet" media="all" href="stylesheets/syntax.css" />
<link rel="stylesheet" media="all" href="stylesheets/main.css" />
<script type="text/javascript" src="javascripts/highlight.pack.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-6212472-4']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<a class="forkme" href="http://github.com/rafeca/prettyjson">
<img src="https://a248.e.akamai.net/assets.github.com/img/e6bef7a091f5f3138b8cd40bc3e114258dd68ddf/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub">
</a>
<div id="container">
<h1>prettyjson <a href="http://travis-ci.org/rafeca/prettyjson"><img src="https://secure.travis-ci.org/rafeca/prettyjson.png" alt="Build Status"></a> <a href="http://badge.fury.io/js/prettyjson"><img src="https://badge.fury.io/js/prettyjson.png" alt="NPM version"></a></h1>
<p>Package for formatting JSON data in a coloured YAML-style, perfect for CLI output.
</p>
<h2>How to install</h2>
<p>Just install it via NPM:
</p>
<pre><code class="lang-bash">$ npm install prettyjson</code></pre>
<h2>Using it (from the CLI)</h2>
<p>This package installs a command line interface to render JSON data in a more convenient way. You can use the CLI
in three different ways:
</p>
<p><strong>Decode a JSON file:</strong> If you want to see the contents of a JSON file, just pass it as the first argument to the CLI:
</p>
<pre><code class="lang-bash">$ prettyjson package.json</code></pre>
<p><img src="http://rafeca.com/prettyjson/images/example3.png" alt="Example 1">
</p>
<p><strong>Decode the stdin:</strong> You can also pipe the result of a command (for example an HTTP request) to the CLI to see
the JSON result in a clearer way:
</p>
<pre><code class="lang-bash">$ curl https://api.github.com/users/rafeca | prettyjson</code></pre>
<p><img src="http://rafeca.com/prettyjson/images/example4.png" alt="Example 2">
</p>
<p><strong>Decode random strings:</strong> if you call the CLI with no arguments, you'll get a prompt where you can past JSON strings
and they'll be automatically displayed in a clearer way:
</p>
<p><img src="http://rafeca.com/prettyjson/images/example5.png" alt="Example 3">
</p>
<p>If you install the package globally (with <code>npm install -g prettyjson</code>), the CLI will be installed automatically in your PATH
thanks to npm.
</p>
<h3>Customizing colors via command line</h3>
<p>Now it's possible to customize the colors of the output via environment variables, thanks to @bahamas10:
</p>
<pre><code class="lang-bash">$ PRETTYJSON_KEYS=red PRETTYJSON_DASH=blue PRETTYJSON_STRING=yellow prettyjson package.json</code></pre>
<p>The available options are <code>PRETTYJSON_KEYS</code>, <code>PRETTYJSON_DASH</code>, <code>PRETTYJSON_STRING</code> and <code>PRETTYJSON_INDENT</code>.
</p>
<h2>Using it (from Node.js)</h2>
<p>It's pretty easy to use it... you just have to include it in your script and call the <code>render()</code> method:
</p>
<pre><code class="lang-javascript">var prettyjson = require('prettyjson');
var data = {
username: 'rafeca',
url: 'https://github.com/rafeca',
twitter_account: 'https://twitter.com/rafeca',
projects: ['prettyprint', 'connfu']
};
console.log(prettyjson.render(data));</code></pre>
<p>And will output:
</p>
<p><img src="http://rafeca.com/prettyjson/images/example1.png" alt="Example 4">
</p>
<p>You can also configure the colors of the hash keys and array dashes
(using <a href="https://github.com/Marak/colors.js">colors.js</a> colors syntax):
</p>
<pre><code class="lang-javascript">var prettyjson = require('prettyjson');
var data = {
username: 'rafeca',
url: 'https://github.com/rafeca',
twitter_account: 'https://twitter.com/rafeca',
projects: ['prettyprint', 'connfu']
};
console.log(prettyjson.render(data, {
keysColor: 'rainbow',
dashColor: 'magenta',
stringColor: 'white'
}));</code></pre>
<p>Will output something like:
</p>
<p><img src="http://rafeca.com/prettyjson/images/example2.png" alt="Example 5">
</p>
<h2>Annotated source</h2>
<p>You can check the <a href="http://rafeca.com/prettyjson/prettyjson.html">annotated source</a> for more information about how it works
</p>
<h2>Running Tests</h2>
<p>To run the test suite first invoke the following command within the repo, installing the development dependencies:
</p>
<pre><code class="lang-bash">$ npm install --dev</code></pre>
<p>then run the tests:
</p>
<pre><code class="lang-bash">$ npm test</code></pre>
<p>You can check the package's <a href="http://rafeca.com/prettyjson/coverage.html">test coverage</a> if you are one of those test paranoics
</p>
<h2>Change Log</h2>
<h3>0.9.0 — <em>October 4, 2013</em></h3>
<ul>
<li>Checks for hasOwnProperty in function render (by @dlight)</li>
</ul>
<h3>0.8.1 — <em>March 11, 2013</em></h3>
<ul>
<li>Add compatibility for Node.js 0.10.0</li>
<li>Update dependencies</li>
</ul>
<h3>0.8.0 — <em>February 23, 2013</em></h3>
<ul>
<li>Fix: ENV vars were not being used correctly</li>
<li>Add an option to change the color of the strings</li>
<li>Minor codestyle improvements</li>
</ul>
<h3>0.7.1 — <em>October 29, 2012</em></h3>
<ul>
<li>Fix bug in 0.7.0 when input is an array</li>
</ul>
<h3>0.7.0 — <em>October 25, 2012</em></h3>
<ul>
<li>Allow having non-JSON characters at the beginning of the input string (ideal for curl -i)</li>
<li>Add a renderString() method to be used by the CLI</li>
<li>Change test reporter style to spec</li>
<li>Upgrade dependencies to the last versions</li>
</ul>
<h3>0.6.0 — <em>June 29, 2012</em></h3>
<ul>
<li>Update dependencies to support Node.js v0.8</li>
<li>Adding ability to use environmental variables</li>
</ul>
<h3>0.5.0 — <em>June 24, 2012</em></h3>
<ul>
<li>Updated dependencies, added support for Node.js up to 0.7.12</li>
<li>Minor improvements in README file</li>
<li>Add JSHint to the build process</li>
<li>Add jake task to execute tests automatically</li>
<li>Add test coverage info</li>
</ul>
<h3>0.4.0 — <em>February 24, 2012</em></h3>
<ul>
<li>Now prettyjson uses Mocha test framework instead of jasmine-node</li>
<li>Fixed leak in a "key" variable</li>
</ul>
<h3>0.3.1 — <em>February 15, 2012</em></h3>
<ul>
<li>Modified website design</li>
<li>Modify webpage link in package.json</li>
</ul>
<h3>0.3.0 — <em>January 24, 2012</em></h3>
<ul>
<li>Added CLI interface</li>
<li>Now prettyjson package requires Nodejs 0.6.x</li>
</ul>
<h3>0.2.1 — <em>January 23, 2012</em></h3>
<ul>
<li>Fix: Bug when the JSON has attributes with booleans, integers or null values</li>
</ul>
<h3>0.2.0 — <em>January 22, 2012</em></h3>
<ul>
<li>Now using node-releasetools for the release process</li>
<li>Disabled Node.js 0.6 from Travis CI temporally</li>
<li>Minor copy in Readme.md</li>
</ul>
<h3>0.1.4 — <em>December 1, 2011</em></h3>
<ul>
<li>Added contributors to the Readme file</li>
</ul>
<h3>0.1.3 — <em>November 17, 2011</em></h3>
<ul>
<li>Fixed the GitHub publishing of tags in the jake task</li>
<li>Updated package.json to make it compatible with Node.js 0.6.x</li>
<li>Updated travis YAML file to use the new Node.js support on Travis</li>
</ul>
<h3>0.1.2 — <em>November 14, 2011</em></h3>
<ul>
<li>Updated Jakefile with tasks to automate publishing new versions</li>
</ul>
<h3>0.1.1 — <em>October 11, 2011</em></h3>
<ul>
<li>Added changelog jake task to add changelog automatically</li>
<li>The library version is retrieved from <code>package.json</code> file</li>
</ul>
<h3>0.1.0 — <em>October 10, 2011</em></h3>
<ul>
<li>Initial release</li>
</ul>
<h2>Contributors</h2>
<ul>
<li>Rafael de Oleza <a href="mailto:rafeca@gmail.com">rafeca@gmail.com</a></li>
<li>Dave Eddy <a href="mailto:dave@daveeddy.com">dave@daveeddy.com</a></li>
<li>Elias Amaral <a href="mailto:tolkiendili@gmail.com">tolkiendili@gmail.com</a></li>
<li>jiyinyiyong <a href="mailto:jiyinyiyong@gmail.com">jiyinyiyong@gmail.com</a></li>
</ul>
<h2>How to contribute</h2>
<p>Good pull requests - patches, improvements, new features - are a fantastic
help.
</p>
<p>If you've spotted any small, obvious errors and want to help out by patching it,
that will be much appreciated.
</p>
<p>If your contribution involves a significant amount of work or substantial
changes to any part of the project, please open a "contribution enquiry" issue
first to check that the work is wanted or matches the goals of the project.
</p>
<p>All pull requests should remain focused in scope and avoid containing unrelated
commits.
</p>
<p>Please follow this process; it's the best way to get your work included in the
project:
</p>
<ol>
<li><p><a href="http://help.github.com/fork-a-repo/">Fork</a> the project.</p>
</li>
<li><p>Clone your fork (<code>git clone
git@github.com:<your-username>/<repo-name>.git</code>).</p>
</li>
<li><p>Add an <code>upstream</code> remote (<code>git remote add upstream
git://github.com/<upsteam-owner>/<repo-name>.git</code>).</p>
</li>
<li><p>Get the latest changes from upstream (e.g. <code>git pull upstream
<dev-branch></code>).</p>
</li>
<li><p>Create a new topic branch to contain your feature, change, or fix (<code>git
checkout -b <topic-branch-name></code>).</p>
</li>
<li><p>Create the needed tests to ensure that your contribution is not broken in the future.
If you are creating a small fix or patch to an existing feature, just a simple test
will do, if it is a brand new feature, make sure to create a new test suite.</p>
</li>
<li><p>Make sure that your changes adhere to the current coding conventions used
throughout the project - indentation, accurate comments, etc.</p>
</li>
<li><p>Commit your changes in logical chunks; use git's <a href="https://help.github.com/articles/interactive-rebase">interactive
rebase</a> feature to tidy
up your commits before making them public. Please adhere to these <a href="http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html">git commit
message guidelines</a>
or your pull request is unlikely be merged into the main project.</p>
</li>
<li><p>Locally merge (or rebase) the upstream branch into your topic branch.</p>
</li>
<li><p>Push your topic branch up to your fork (<code>git push origin
<topic-branch-name></code>).</p>
</li>
<li><p><a href="http://help.github.com/send-pull-requests/">Open a Pull Request</a> with a
clear title and description.</p>
</li>
</ol>
<p>If you have any other questions about contributing, please feel free to contact
me.</p>
<h2>License</h2>
<p>(The MIT License)
</p>
<p>Copyright (c) 2011 Rafael de Oleza <a href="mailto:rafeca@gmail.com">rafeca@gmail.com</a>
</p>
<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
</p>
<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
</p>
<p>THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</p>
</div>
<div id="footer">
<a href="http://rafeca.com"><span>Rafael de Oleza — rafeca</span></a>
<a class="fadedlink" href="https://github.com/rafeca"><img src="images/github-logo.png" alt="@github"></a>
<a class="fadedlink" href="https://twitter.com/rafeca"><img src="images/twitter-logo.png" alt="@twitter"></a>
<a class="fadedlink" href="http://linkedin.com/in/rafeca"><img src="images/linkedin-logo.png" alt="@linkedin"></a>
<a class="fadedlink" href="mailto:rafeca@gmail.com"><img src="images/gmail-logo.png" alt="@gmail"></a>
</div>
<script type="text/javascript">
var codes = document.getElementsByTagName('code');
for (var i=0;i<codes.length;i++) {
if (codes[i].className.indexOf('lang-') === 0) {
codes[i].className = codes[i].className.slice('lang-'.length);
}
switch(codes[i].className) {
case 'js':
codes[i].className = 'javascript';
break;
}
}
hljs.initHighlightingOnLoad();
</script>
</body>
</html>