0
This question is possibly duplicated, but I seek a different approach. The situation is as follows: I made a small program to create a JSON file, creating names and assigning values. The JSON destination when read will be an object, so the names become property identifiers. It would be interesting to filter the field names more rigorously, following the language criteria.
Edited: Bruno Vianna warns of the fact that JSON is different from literal objects, even though the structure is the same. Doing a JSON correctly does not solve the problem since it has different rules (see the example).
What I got from information is instructions on how to create a name that does not give problems when developing the code. There is a page on MDN and also a similar issue from here with an exhaustive answer. For the beginning of the identifier is easy: a letter, _ or $. The rest I did not find a character set or regular expression to search for problems, and there are still the names reserved. All this has to be listed for a checker. My alternative so far is to follow the Unix filename pattern, take out the spaces, etc. which is rather conservative.
Has anyone made a checker? Know any?
EDITED: Example: this JSON was validated by Jsonlint
var str = '{"prop+1": "valor1","%prop2": "valor2"}';
var obj = JSON.parse(str);
var res = '<h1>Teste</h1>';
res += '<li>' + obj["prop+1"] + '</li>';
res += '<li>' + obj["%prop2"] + '</li>';
res += '<li>' + obj.prop+1 + '</li>';
res += '<li>' + obj.%prop2 + '</li>';
document.getElementById("result").innerHTML = res;
The code didn’t work on jsfiddle - I thought the properties would be accessible in quotes, but no. Anyway, they are problematic identifiers that pass at first (creation of JSON) but can give problem later.
"Problem" is something very vague. Just remember, when rephrasing your question, that a literal object is not the same as a JSON object, although its structure is equal.
– Brunno Vianna
I really did not understand your doubt... You can put an example?
– KhaosDoctor
Brunno: the problem is that JSON is not literal object, or it would be easy - only detect the error when creating JSON. So I need to filter the identifiers thinking about the rules of a literal object - it was worth the tip! @Khaosdoctor: I really missed an example, I put it there.
– Gustavo