-
Notifications
You must be signed in to change notification settings - Fork 5
/
39_json_stringify.js
96 lines (88 loc) · 3.58 KB
/
39_json_stringify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function jsonStringify() {
/**
* ========================================================
* Basic Syntax and Usage of JSON.stringify()
* ========================================================
* The JSON.stringify() method converts a JavaScript value (object, array, string,
* number, boolean, or null) to a JSON-formatted string.
*
* Syntax: JSON.stringify(value, replacer?, space?)
* - value: The JavaScript value to convert to a JSON string.
* - replacer: Either a function or an array used to filter or modify the results.
* - space: Specifies the indentation for readability.
*/
const person = {
name: "Ishtmeet",
age: 25,
};
const jsonString = JSON.stringify(person);
console.log(`Basic Usage: ${jsonString}`); // Output: '{"name":"Ishtmeet","age":25}'
/**
* ========================================================
* Using Replacer Function
* ========================================================
* The replacer function can be used to filter out values or to transform the values
* before they get stringified. The replacer function takes two arguments, the 'key' and 'value'.
*/
const replacerFunction = (key, value) => {
if (typeof value === "number") {
return value * 2;
}
return value;
};
console.log(`With Replacer Function: ${JSON.stringify(person, replacerFunction)}`); // Output: '{"name":"Ishtmeet","age":50}'
/**
* ========================================================
* Using Space for Indentation
* ========================================================
* The 'space' parameter specifies the number of spaces to use for indentation.
* This makes the output JSON string more readable.
*/
const prettyJsonString = JSON.stringify(person, null, 4);
console.log(`Pretty Printed JSON: \n${prettyJsonString}`);
/**
* ========================================================
* Nuances and Edge Cases
* ========================================================
*/
/**
* Handling Undefined and Functions
* --------------------------------
* JSON.stringify() will omit properties with undefined values, functions, or Symbol types.
*/
const objWithUndefined = { name: "Ishtmeet", greet: undefined, sayHi: function () {} };
console.log(`Omitting Undefined and Functions: ${JSON.stringify(objWithUndefined)}`); // Output: '{"name":"Ishtmeet"}'
/**
* Handling Circular References
* ----------------------------
* JSON.stringify() throws an error when there are circular references in the object.
*/
const circularObj = { name: "Ishtmeet" };
circularObj.self = circularObj;
// Uncomment the following line will result in an error
// console.log(JSON.stringify(circularObj));
/**
* toJSON Method
* ------------
* If an object has a toJSON method, JSON.stringify() calls it and stringifies
* the value returned by toJSON().
*/
const objWithToJSON = {
name: "Ishtmeet",
age: 25,
toJSON() {
return {
name: this.name,
};
},
};
console.log(`Using toJSON method: ${JSON.stringify(objWithToJSON)}`); // Output: '{"name":"Ishtmeet"}'
/**
* Handling Dates
* --------------
* JSON.stringify() will convert Date objects to their ISO string representation.
*/
const objWithDate = { name: "Ishtmeet", birthDate: new Date() };
console.log(`Handling Dates: ${JSON.stringify(objWithDate)}`);
}
jsonStringify();