jsoneditor/docs/api.md

291 lines
7.7 KiB
Markdown
Raw Normal View History

2013-05-04 17:54:03 +08:00
# API Reference
## JSONEditor
### Constructor
2015-12-15 02:58:17 +08:00
#### `JSONEditor(container [, options [, json]])`
2013-05-04 17:54:03 +08:00
Constructs a new JSONEditor.
*Parameters:*
2016-01-12 20:16:13 +08:00
- `{Element} container`
2016-01-12 19:40:23 +08:00
2014-07-28 14:45:31 +08:00
An HTML DIV element. The JSONEditor will be created inside this container element.
2016-01-12 19:40:23 +08:00
- `{Object} options`
Optional object with options. The available options are described under
[Configuration options](#configuration-options).
- `{JSON} json`
2016-01-12 20:16:13 +08:00
2013-05-04 17:54:03 +08:00
Initial JSON data to be loaded into the JSONEditor. Alternatively, the method `JSONEditor.set(json)` can be used to load JSON data into the editor.
*Returns:*
2016-01-12 19:40:23 +08:00
- `{JSONEditor} editor`
2016-01-12 20:16:13 +08:00
2013-05-04 17:54:03 +08:00
New instance of a JSONEditor.
2016-01-12 19:40:23 +08:00
### Configuration options
- `{Object} ace`
Provide a custom version of the [Ace editor](http://ace.c9.io/) and use this instead of the version that comes embedded with JSONEditor. Only applicable when `mode` is `code`.
2016-01-12 20:16:13 +08:00
- `{Object} ajv`
2016-01-12 19:40:23 +08:00
Provide a custom instance of [ajv](https://github.com/epoberezkin/ajv), the
library used for JSON schema validation. Example:
```js
var options = {
ajv: Ajv({ allErrors: true, verbose: true })
2016-01-12 19:40:23 +08:00
}
```
- `{function} onChange`
Set a callback function triggered when the contents of the JSONEditor change. Called without parameters. Will only be triggered on changes made by the user, not in case of programmatic changes via the functions `set` or `setText`.
- `{function} onEditable`
Set a callback function to determine whether individual nodes are editable or read-only. Only applicable when option `mode` is `tree`. The callback is invoked as `editable(node)`, where `node` is an object `{field: string, value: string, path: string[]}`. The function must either return a boolean value to set both the nodes field and value editable or read-only, or return an object `{field: boolean, value: boolean}`.
- `{function} onError`
Set a callback function triggered when an error occurs. Invoked with the error as first argument. The callback is only invoked
for errors triggered by a users action, like switching from code mode to tree mode or clicking the Format button whilst the editor doesn't contain valid JSON.
- `{function} onModeChange(newMode, oldMode)`
Set a callback function triggered right after the mode is changed by the user. Only applicable when
the mode can be changed by the user (i.e. when option `modes` is set).
- `{boolean} escapeUnicode`
If true, unicode characters are escaped and displayed as their hexadecimal code (like `\u260E`) instead of of the character itself (like `☎`). False by default.
- `{boolean} history`
Enables history, adds a button Undo and Redo to the menu of the JSONEditor. True by default. Only applicable when `mode` is 'tree' or 'form'.
- `{String} mode`
Set the editor mode. Available values: 'tree' (default), 'view', 'form', 'code', 'text'. In 'view' mode, the data and datastructure is read-only. In 'form' mode, only the value can be changed, the datastructure is read-only. Mode 'code' requires the Ace editor to be loaded on the page. Mode 'text' shows the data as plain text.
- `{String[]} modes`
Create a box in the editor menu where the user can switch between the specified modes. Available values: see option `mode`.
- `{String} name`
Initial field name for the root node, is undefined by default. Can also be set using `JSONEditor.setName(name)`. Only applicable when `mode` is 'tree', 'view', or 'form'.
- `{Object} schema`
Validate the JSON object against a JSON schema. A JSON schema describes the
structure that a JSON object must have, like required properties or the type
that a value must have.
2016-01-12 19:40:23 +08:00
See [http://json-schema.org/](http://json-schema.org/) for more information.
- `{boolean} search`
Enables a search box in the upper right corner of the JSONEditor. True by default. Only applicable when `mode` is 'tree', 'view', or 'form'.
- `{Number} indentation`
Number of indentation spaces. 2 by default. Only applicable when `mode` is 'code' or 'text'.
- `{String} theme`
Set the Ace editor theme, uses included 'ace/theme/jsoneditor' by default. Please note that only the default theme is included with jsoneditor, so if you specify another one you need to make sure it is loaded.
2013-05-04 17:54:03 +08:00
### Methods
#### `JSONEditor.collapseAll()`
Collapse all fields. Only applicable for mode 'tree', 'view', and 'form'.
#### `JSONEditor.destroy()`
Destroy the editor. Clean up DOM, event listeners, and web workers.
2013-05-04 17:54:03 +08:00
#### `JSONEditor.expandAll()`
Expand all fields. Only applicable for mode 'tree', 'view', and 'form'.
#### `JSONEditor.focus()`
Set focus to the JSONEditor.
2013-05-04 17:54:03 +08:00
#### `JSONEditor.set(json)`
Set JSON data.
*Parameters:*
- `{JSON} json`
2016-01-12 20:16:13 +08:00
2013-05-04 17:54:03 +08:00
JSON data to be displayed in the JSONEditor.
2013-05-04 18:02:20 +08:00
#### `JSONEditor.setMode(mode)`
Switch mode. Mode `code` requires the [Ace editor](http://ace.ajax.org/).
*Parameters:*
2016-01-12 20:16:13 +08:00
- `{String} mode`
2015-12-15 02:58:17 +08:00
Available values: `tree`, `view`, `form`, `code`, `text`.
2013-05-04 18:02:20 +08:00
2013-05-04 17:54:03 +08:00
#### `JSONEditor.setName(name)`
Set a field name for the root node.
*Parameters:*
2016-01-12 20:16:13 +08:00
- `{String | undefined} name`
2013-05-04 17:54:03 +08:00
Field name of the root node. If undefined, the current name will be removed.
2016-01-12 20:16:13 +08:00
#### `JSONEditor.setSchema(schema)`
Set a JSON schema for validation of the JSON object. See also option `schema`.
See [http://json-schema.org/](http://json-schema.org/) for more information on the JSON schema definition.
*Parameters:*
- `{Object} schema`
A JSON schema.
2013-05-04 17:54:03 +08:00
#### `JSONEditor.setText(jsonString)`
Set text data in the editor.
2013-05-04 17:54:03 +08:00
2016-01-12 20:16:13 +08:00
This method throws an exception when the provided jsonString does not contain
2015-12-15 02:58:17 +08:00
valid JSON and the editor is in mode `tree`, `view`, or `form`.
2013-05-04 17:54:03 +08:00
*Parameters:*
2014-07-28 14:45:31 +08:00
2016-01-12 20:16:13 +08:00
- `{String} jsonString`
Contents of the editor as string.
2013-05-04 17:54:03 +08:00
#### `JSONEditor.get()`
2015-12-15 02:58:17 +08:00
Get JSON data.
This method throws an exception when the editor does not contain valid JSON,
which can be the case when the editor is in mode `code` or `text`.
2013-05-04 17:54:03 +08:00
*Returns:*
2014-07-28 14:45:31 +08:00
2016-01-12 20:16:13 +08:00
- `{JSON} json`
2014-07-28 14:45:31 +08:00
JSON data from the JSONEditor.
2013-05-04 17:54:03 +08:00
#### `JSONEditor.getMode()`
Retrieve the current mode of the editor.
*Returns:*
2016-01-12 20:16:13 +08:00
- `{String} mode`
Current mode of the editor for example `tree` or `code`.
2013-05-04 17:54:03 +08:00
#### `JSONEditor.getName()`
Retrieve the current field name of the root node.
*Returns:*
2016-01-12 20:16:13 +08:00
- `{String | undefined} name`
2013-05-04 17:54:03 +08:00
Current field name of the root node, or undefined if not set.
#### `JSONEditor.getText()`
Get JSON data as string.
*Returns:*
2014-07-28 14:45:31 +08:00
2016-01-12 20:16:13 +08:00
- `{String} jsonString`
Contents of the editor as string. When the editor is in code `text` or `code`,
the returned text is returned as-is. For the other modes, the returned text
is a compacted string. In order to get the JSON formatted with a certain
number of spaces, use `JSON.stringify(JSONEditor.get(), null, 2)`.
2013-05-04 17:54:03 +08:00
### Examples
A tree editor:
```js
var options = {
"mode": "tree",
"search": true
};
2014-05-30 16:33:11 +08:00
var editor = new JSONEditor(container, options);
2013-05-04 17:54:03 +08:00
var json = {
"Array": [1, 2, 3],
"Boolean": true,
"Null": null,
"Number": 123,
"Object": {"a": "b", "c": "d"},
"String": "Hello World"
};
editor.set(json);
editor.expandAll();
var json = editor.get(json);
```
A text editor:
```js
var options = {
"mode": "text",
"indentation": 2
};
2014-05-30 16:33:11 +08:00
var editor = new JSONEditor(container, options);
2013-05-04 17:54:03 +08:00
var json = {
"Array": [1, 2, 3],
"Boolean": true,
"Null": null,
"Number": 123,
"Object": {"a": "b", "c": "d"},
"String": "Hello World"
};
editor.set(json);
2015-11-02 15:53:13 +08:00
var json = editor.get();
2013-05-04 17:54:03 +08:00
```
## JSON parsing and stringification
2014-07-28 14:45:31 +08:00
In general to parse or stringify JSON data, the browsers built in JSON parser can be used. To create a formatted string from a JSON object, use:
2013-05-04 17:54:03 +08:00
```js
var formattedString = JSON.stringify(json, null, 2);
2013-05-04 17:54:03 +08:00
```
to create a compacted string from a JSON object, use:
```js
var compactString = JSON.stringify(json);
2013-05-04 17:54:03 +08:00
```
To parse a String to a JSON object, use:
```js
var json = JSON.parse(string);
```