Skip to content

Commit

Permalink
fix: json body prettify not working with comments (usebruno#1831)
Browse files Browse the repository at this point in the history
Added Microsoft's node-jsonc-parser library that is able to format json
with comments. Seems to handle bigints properly too.

It may be good to consider replacing existing `decomment` library with
`jsonc-parser` if its only use case is to decomment json.

Fixes usebruno#1830

Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
  • Loading branch information
2 people authored and Its-treason committed Apr 13, 2024
1 parent 854584b commit a097214
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/bruno-app/package.json
Expand Up @@ -42,6 +42,7 @@
"jsesc": "^3.0.2",
"jshint": "^2.13.6",
"json5": "^2.2.3",
"jsonc-parser": "^3.2.1",
"jsonlint": "^1.6.3",
"jsonpath-plus": "^7.2.0",
"know-your-http-well": "^0.5.0",
Expand Down
Expand Up @@ -8,6 +8,7 @@ import { humanizeRequestBodyMode } from 'utils/collections';
import StyledWrapper from './StyledWrapper';
import { updateRequestBody } from 'providers/ReduxStore/slices/collections/index';
import { toastError } from 'utils/common/error';
import { format, applyEdits } from 'jsonc-parser';
import { parse, stringify } from 'lossless-json';
import xmlFormat from 'xml-formatter';

Expand Down Expand Up @@ -39,8 +40,8 @@ const RequestBodyMode = ({ item, collection }) => {
const onPrettify = () => {
if (body?.json && bodyMode === 'json') {
try {
const bodyJson = parse(body.json);
const prettyBodyJson = stringify(bodyJson, null, 2);
const edits = format(body.json, undefined, { tabSize: 2, insertSpaces: true });
const prettyBodyJson = applyEdits(body.json, edits);
dispatch(
updateRequestBody({
content: prettyBodyJson,
Expand Down

0 comments on commit a097214

Please sign in to comment.