Code Monkey home page Code Monkey logo

hulk's Introduction

Hulk

Hulk is an in-browser JSON editor. View an example

Usage

$.hulk('#selector', {foo: "bar"}, function(data) {
    console.log("Here's the updated data: " + data);
});

Will insert the JSON editor into the div with id "selector". Generally ID's are preferred.

The third argument is a callback that will execute when the user presses "Save". Alternatively, you can retrieve the state of the world at any point by calling:

$.hulkSmash('#selector');

There are two CSS files, hulk.css and hulk-colors.css, which make the layout much more readable. Of course, you are happy to style the inputs however you desire.

Argument documentation

The $.hulk function takes the following arguments in this order:

  • selector (string) - Any valid jQuery selector. The input nodes will be inserted into the DOM based on the selector you give. This argument is required.

  • data (object) - Any valid JSON object. This will be serialized and splayed into the DOM. This argument is required.

  • callback (function) - When the user presses "Save" hulk will re-serialize the data and pass it as an argument to the function you provide. This argument is optional.

  • options (object) - A dictionary of optional settings for hulk. The possible options are outlined below. This argument is optional.

The $.hulkSmash function takes the following arguments in this order:

  • selector (string) - Any valid jQuery selector. The input nodes will be inserted into the DOM based on the selector you give. This argument is required.

  • options (object) - A dictionary of optional settings for hulk. The possible options are outlined below. This argument is optional.

Optional settings

These are options that you can pass to either $.hulk or $.hulkSmash.

  • separator (string) - Define a custom separator between keys and values. By default, the separator is "=>".

  • emptyString (string) - Serialize an empty text input field back into JSON using the empty string ("") instead of null. Defaults to false.

  • depth - Collapse all dictionaries and lists that are nested deeper than depth. depth=0 will collapse everything. depth=-1 will expand everything. The default is to show all JSON.

  • smartParsing - Try to parse text inputs into matching data types, eg turn the string "5.53" into the number 5.53 when serializing into JSON, and turn "true" into true. If set to false, all objects will be serialized as strings. The default is true.

The following are not yet implemented, but will be soon:

  • permissions (string) - Define how customizable and editable the custom JSON dictionary is. For example, you may only want to update existing values, not add new objects. This takes a few different values.

    • "all" - allow the user to edit everything. This is the default.
    • "values-only" - only allow the user to edit the leaf nodes. Keys cannot be added or modified.
    • "no-append" - User can edit keys and values, but can't add or delete nodes from the object.

Why?

Editing raw JSON is error-prone and not friendly to non-technical people. This makes it easy to edit and serialize JSON. This also makes it easy for a programmer and a non-technical user to collaborate.

We also use this tool internally at Twilio to edit configuration files.

Notes

  • Javascript objects do not maintain a sorted order. This means, when serializing an object from HTML to JSON, the keys will not be sorted. To maintain some semblance of order, keys are presented on the page in alphabetical order when serializing from JSON to HTML (this is something we can control). Consider sorting keys once they are received on the server or whichever sane language is dealing with your new JSON object.

  • There is inherent uncertainty in using text inputs for data entry. Does the entry "5.5" represent the number 5.5 or the string "5.5"? In this case we do some basic parsing.

    • if the value looks like a number, it's converted to a number
    • if the value looks like a boolean ("true" or "false"), it's converted to a boolean
    • if the value is empty, or the word "null", it's converted to the null value.

    That said, if you set the smartParsing option to false, all inputs will be serialized and returned as strings.

Installation

  1. Include the jQuery source on your page

  2. Include the hulk plugin.

  3. Call away

hulk's People

Watchers

teamdstn avatar James Cloos 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.