Giter Site home page Giter Site logo

renderjson's Introduction

Renderjson

Render JSON into collapsible, themeable HTML. This library aims to be very simple with few options and no external dependencies. It's aimed at debugging but you can use it wherever it is useful.

The code renders the JSON lazily, only building the HTML when the user reveals the JSON by clicking the disclosure icons. This makes it extremely fast to do the initial render of huge JSON objects, since the only thing that renders initially is a single disclosure icon.

Live Example

A live example can be found here.

Example

<div id="test"></div>
<script type="text/javascript" src="renderjson.js"></script>
<script>
    document.getElementById("test").appendChild(
        renderjson({ hello: [1,2,3,4], there: { a:1, b:2, c:["hello", null] } })
    );
</script>

Usage

The module exports one entry point, the renderjson() function. It takes in the JSON you want to render as a single argument and returns an HTML element.

Options

There are a couple functions to call to customize the output:

renderjson.set_icons('+', '-');

Call set_icons() to set the disclosure icons to something other than "⊕" and "⊖".

renderjson.set_show_to_level(level);

Call set_show_to_level() to show different amounts of the JSON by default. The default is 0, and 1 is a popular choice. As a special case, if level is the string "all" then all the JSON will be shown by default. This, of course, removes the benefit of the lazy rendering, so it may be slow with large JSON objects.

renderjson.set_max_string_length(length);

Strings will be truncated and made expandable if they are longer than length. As a special case, if length is the string "none" then there will be no truncation. The default is "none".

renderjson.set_sort_objects(sort_bool);

Sort objects by key (default: false)

renderjson.set_replacer(replacer_function)
renderjson.set_property_list(property_list)

These are the equivalent of the JSON.stringify() replacer parameter. Mozilla's documentation has a good description of how this parameter works. See test.html for an example of what these can do.

These functions are chainable so you may do:

renderjson.set_icons('+', '-')
          .set_show_to_level(2)
        ({ hello: [1,2,3,4], there: { a:1, b:2, c:["hello", null] } })

Theming

The HTML output uses a number of classes so that you can theme it the way you'd like:

.disclosure    ("⊕", "⊖")
.syntax        (",", ":", "{", "}", "[", "]")
.string        (includes quotes)
.number
.boolean
.key           (object key)
.keyword       ("null", "undefined")
.object.syntax ("{", "}")
.array.syntax  ("[", "]")

Copyright and License

License: ISC

Copyright © 2013-2017 David Caldwell <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

renderjson's People

Contributors

caldwell avatar iamakulov avatar itslenny avatar mfa avatar mitar avatar sgrebnov avatar wulfsolter avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.