-
Notifications
You must be signed in to change notification settings - Fork 445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch JSON formatting from 'jsonc-parser' to 'js-beautify' #233
Conversation
Switched to 'js-beautify' from 'jsonc-parser'
package.json
Outdated
@@ -558,6 +558,7 @@ | |||
"highlight.js": "^9.6.0", | |||
"httpsnippet": "^1.16.5", | |||
"iconv-lite": "^0.4.15", | |||
"js-beautify": "^1.7.5", | |||
"jsonc-parser": "^1.0.3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this package in dependencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice patch, CIL 😃
Done! |
src/utils/responseFormatUtility.ts
Outdated
|
||
export class ResponseFormatUtility { | ||
public static formatBody(body: string, contentType: string, suppressValidation: boolean): string { | ||
if (contentType) { | ||
if (MimeUtility.isJSON(contentType)) { | ||
if (ResponseFormatUtility.IsJsonString(body)) { | ||
const edits = JSONFormat(body, undefined, { tabSize: 2, insertSpaces: true, eol: EOL }); | ||
body = applyEdits(body, edits); | ||
body = beautify(body, { indent_size: 4 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you increase the indent_size from 2 to 4 here?
The existing responseFormatUtility.ts makes use of 'jsonc-parser' to format incoming JSON. The speed doesn't vary much based on size of JSON, but it does vary depending on the number of "edits" that need to be made. When the current implementation tries to format JSON with many objects and sub objects, 30+ seconds can go by (and the waiting spinner stalls). 'pretty-data' uses JSON.stringify(JSON.parse)) which is quick, but will break some invalid JSON where a key is listed twice. I have found that 'js-beautify' is quick and acceptable.
I used this link to test, it is both a large JSON payload and one that is slow to format using 'jsonc-parser' (I gave up waiting), returned in seconds with 'js-beautify': http://www.vizgr.org/historical-events/search.php?format=json&begin_date=-3000000&end_date=20151231&lang=en