Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1516 from snyk/fix/dont-create-json-if-you-dont-n…
…eed-it Don't create JSON if we don't need it
- Loading branch information
Showing
6 changed files
with
1,126 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const debug = require('debug')('snyk-json'); | ||
|
||
/** | ||
* Attempt to json-stringify an object which is potentially very large and might exceed the string limit. | ||
* If it does exceed the string limit, try again without pretty-print to hopefully come out below the string limit. | ||
* @param obj the object from which you want to get a JSON string | ||
*/ | ||
export function jsonStringifyLargeObject(obj: any): string { | ||
let res = ''; | ||
try { | ||
// first try pretty-print | ||
res = JSON.stringify(obj, null, 2); | ||
return res; | ||
} catch (err) { | ||
// if that doesn't work, try non-pretty print | ||
debug('JSON.stringify failed - trying again without pretty print', err); | ||
res = JSON.stringify(obj); | ||
return res; | ||
} | ||
} |
Oops, something went wrong.