JSON.stringify extra parameters
First, create an object to be converted
const book = { "title": "lalalala", "authors": [ "lily", "lmy" ], "edition": 3, "year": 2011 };
Second parameter
Array time
const jsonText = JSON.stringify(book, ['title', 'year']); // Print out // {"title":"lalalala","year":2011}
When function
const jsonText = JSON.stringify(book, function(key ,value){ switch(key){ case "authors": return value.join(","); case "year": return 2000; default: return value; } }); // Print out // {"title":"lalalala","authors":"lily,lmy","edition":3,"year":2000}
So the second parameter also likes to be called the "filter" parameter. Note that in function filter, the default of switch must be written, otherwise, it will be converted to undefined
Third parameter
For figures
// Blank indent 4 characters, max. 10, if greater than 10, it will automatically change to 10 const jsonText = JSON.stringify(book, null, 4);
String
// String instead of blank indentation, of course, the maximum length of string is also 10, which will be intercepted const jsonText = JSON.stringify(book, null, '-lala-');
Filter mechanism of JSON.stringify (toJSON)
const book = { "title": "lalalala", "authors": [ "lily", "lmy" ], "edition": 3, "year": 2011, "toJSON"(){ return this.title; } }; const jsonText = JSON.stringify(book, ['edition', 'year']); // Print out // "lalalala"
So what's the priority of "filters" in toJSON?
- First, check whether there is a method of toJSON for the converted object. If there is, call the method. Otherwise, serialize in the default order
- If you pass in a second argument, use this function filter. The value passed in to the filter is the value returned in the first step.
- Serialize each value returned in step 2 accordingly
- If a third parameter is provided, perform the corresponding formatting.
What's the use of toJSON
const book = { "title": "lalalala", "date": "2011-09-11" "authors": [ "lily", "lmy" ], "edition": 3, "year": 2011, "toJSON"(){ return this.title; }, "toJSON"(){ return { title: this.title, year: this.year, time: new Date(this.date).getTime() } } }; const jsonText = JSON.stringify(book, ['time', 'year']); // Print out // {"time":1536624000000,"year":2011}
Of course, here is just a small example to provide an idea for business data transformation in the future
JSON.parse extra parameters
JSON.parse only accepts two parameters, and the second parameter is the filter parameter
const book = { "title": "lalalala", "date": "2018-09-11", "authors": [ "lily", "lmy" ], "edition": 3, "year": 2011, toJSON(){ return { title: this.title, year: this.year, time: new Date(this.date).getTime() }; } }; const jsonText = JSON.stringify(book, ['time', 'year', 'title']); const bookCopy = JSON.parse(jsonText, function(key, value) { if(key === 'title'){ return 'lalala'; } else { return value; } }); // Print out // {time: 1536624000000, year: 2011, title: "lalala"}
Note that the function in the "filter" parameter here also returns the default value, that is, if must have the end of else, and switch must have default, otherwise the final result is undefined. Of course, the combination of performance and code aesthetics, switch is recommended.
Thank you for reading~