Hello,
I use your project as user configuration editor for a website.
I think jsoneditor must return false or an empty object when there is no (or invalid) data inside it.
I must use a try catch block like this inside my code, else the get() method crash entire javascript when user empty the editor.
let obj;
try {
obj = jsoneditor.get();
} catch(error) {
obj = {};
}
json = JSON.stringify(obj);
I think this security must be included inside your get method.
Thanks a lot for your work,
Pascal
Please read the docs about get and getText.
JSONEditor.get() : This method throws an exception when the editor does not contain valid JSON, (in code or text mode)... OK it's documented.
I think I do the simpler way because I need an "alway valid compacted string"? But there is a server side validation in all case...
I do overrides and diffs from and to a default standard json configuration file to minimize duplicated data and easier feature add to my robotics project.
With my current implementation the user return to the default (empty) json when there is invalid data!
Your exception is good because I must warn the user for invalid json and loss of data
A reset to default must be explicitly wanted with an empty json { }
Shouldn't you use getText instead if you need an always valid string?
Sorry for my english.
Yes I use getText() without any try catch. There is server side validation. I can reset to default conf with an empty { } jsoneditor.
I inject this inside DOM/form/hidden input from because my framework need to .serialize() the form and post with Ajax
Now I need a last upgrade : a reliable "onChange" event to remove my 1000ms polling/DOM update :
let json;
let oldJson = "";
function updateForm() {
json = jsoneditor.getText();
if(json != oldJson) {
$("#json").val(json);
oldJson = json;
}
}
updateForm();
setInterval(updateForm, 1000);
I read the doc... There is onChangeText event:)
:+1:
This editor is f-c-ing awesome thanks a lot. We can do any web user editable configurator with bulletproof json validity.
My complete code become :
<script>
<!--
$(function() {
let jsoneditor;
jsoneditor = new JSONEditor($("#jsoneditor")[0], {
mode: "tree",
modes: ["code", "form", "text", "tree", "view"],
enableSort: false,
enableTransform: false,
search: false,
navigationBar: false,
schema: JSON.parse('<?php echo getjson($schema); ?>'),
onChangeText: majForm
});
jsoneditor.set(overwriteKeys(JSON.parse('<?php echo getjson($minimal); ?>'),
JSON.parse('<?php echo $json; ?>'), true));
function majForm(json) {
$("#json").val(json);
}
$("#json").val(jsoneditor.getText());
});
//-->
</script>
I must look if I can replace jsoneditor.set() with a pre defined value, to remove the last $("#json").val(jsoneditor.getText()); programmatic update
I can do this but it's the same. I finished
<script>
<!--
$(function() {
let jsoneditor;
jsoneditor = new JSONEditor($("#jsoneditor")[0], {
mode: "tree",
modes: ["code", "form", "text", "tree", "view"],
enableSort: false,
enableTransform: false,
search: false,
navigationBar: false,
schema: JSON.parse('<?php echo getjson($schema); ?>'),
onChangeText: majForm
}, overwriteKeys(JSON.parse('<?php echo getjson($minimal); ?>'),
JSON.parse('<?php echo $json; ?>'), true));
function majForm(json) {
$("#json").val(json);
}
$("#json").val(jsoneditor.getText());
});
//-->
</script>
Glad to hear you like it :)
If you're talking about validation, you can even look into JSON schema.
I massively use of JSON schema since the beginning:)