Skip to content
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

Merged
merged 5 commits into from
Aug 17, 2018

Conversation

ygraber
Copy link
Contributor

@ygraber ygraber commented Aug 15, 2018

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

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",
Copy link
Owner

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

Copy link
Owner

@Huachao Huachao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice patch, CIL 😃

@ygraber
Copy link
Contributor Author

ygraber commented Aug 16, 2018

Done!


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 });

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?

@Huachao Huachao merged commit 3ef028a into Huachao:master Aug 17, 2018
@Huachao
Copy link
Owner

Huachao commented Aug 17, 2018

@ygraber @deanchen Merged, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants