EN
What is a JSON-safe object?
1 answers
5 points
Reading Kyle Simpson's book You Don't Know JS: this & Object Prototypes, I have found some term: JSON-safe object.
What it is?
1 answer
1 points
It is object that can be converted to JSON without loosing any information.
That object shouldn't contain properties types that are not allowed in JSON notaiton.
Allowed are: POJO objects ({}
), arrays, strings, numbers and booleans.
e.g.
xxxxxxxxxx
1
var userObject1 = {name: 'John', age: 23};
2
var userObject2 = JSON.parse(JSON.stringify(userObject1));
3
4
// deep equals operation for userObject1 and userObject2 should return true
5
6
console.log(JSON.stringify(userObject1));
7
console.log(JSON.stringify(userObject2));
Not JSON-safe object example:
xxxxxxxxxx
1
var userObject1 = {name: 'John', age: 23, expression: /^\d^/gi};
2
var userObject2 = JSON.parse(JSON.stringify(userObject1));
3
4
// in this case deep equals operation for userObject1 and userObject2 should return false
5
6
function printObject(object) {
7
var text = '';
8
for (var key in object) {
9
if (text) {
10
text += ', ';
11
}
12
text += key + '=' + object[key];
13
}
14
console.log('{ ' + text + ' }');
15
}
16
17
printObject(userObject1);
18
printObject(userObject2);
0 commentsShow commentsAdd comment