diff --git a/src/JSONNode.js b/src/JSONNode.js index 5d5d92a..c16d83f 100644 --- a/src/JSONNode.js +++ b/src/JSONNode.js @@ -336,20 +336,20 @@ export default class JSONNode extends Component { } handleChangeValue (event) { - const value = JSONNode._getValueFromEvent(event) + const value = this._getValueFromEvent(event) this.props.events.onChangeValue(this.props.path, value) } handleClickValue (event) { if (event.ctrlKey && event.button === 0) { // Ctrl+Left click - JSONNode._openLinkIfUrl(event) + this._openLinkIfUrl(event) } } handleKeyDownValue (event) { if (event.ctrlKey && event.which === 13) { // Ctrl+Enter - JSONNode._openLinkIfUrl(event) + this._openLinkIfUrl(event) } } @@ -377,8 +377,8 @@ export default class JSONNode extends Component { * @param event * @private */ - static _openLinkIfUrl (event) { - const value = JSONNode._getValueFromEvent(event) + _openLinkIfUrl (event) { + const value = this._getValueFromEvent(event) if (isUrl(value)) { event.preventDefault() @@ -388,11 +388,19 @@ export default class JSONNode extends Component { } } - static _getValueFromEvent (event) { - return stringConvert(unescapeHTML(getInnerText(event.target))) + /** + * Get the value of the target of an event, and convert it to it's type + * @param event + * @return {string | number | boolean | null} + * @private + */ + _getValueFromEvent (event) { + const stringValue = unescapeHTML(getInnerText(event.target)) + return this.props.data.type === 'string' + ? stringValue + : stringConvert(stringValue) } - /** * Find the root DOM element of the JSONEditor * Search is done based on the CSS class 'jsoneditor' diff --git a/src/Main.js b/src/Main.js index 95019e3..38884d7 100644 --- a/src/Main.js +++ b/src/Main.js @@ -371,7 +371,7 @@ function jsonToData (path, json, expand) { } else { return { - type: 'json', + type: 'value', value: json } } @@ -383,21 +383,21 @@ function jsonToData (path, json, expand) { * @return {Object | Array | string | number | boolean | null} json */ function dataToJson (data) { - if (data.type === 'array') { - return data.items.map(dataToJson) - } - else if (data.type === 'object') { - const object = {} + switch (data.type) { + case 'array': + return data.items.map(dataToJson) - data.props.forEach(prop => { - object[prop.name] = dataToJson(prop.value) - }) + case 'object': + const object = {} - return object - } - else { - // type 'value' or 'string' - return data.value + data.props.forEach(prop => { + object[prop.name] = dataToJson(prop.value) + }) + + return object + + default: // type 'string' or 'value' + return data.value } }