Skip to content

Commit ab4e9eb

Browse files
authored
fix: use fast-json-format for prettifying json (#6026)
1 parent 24a36bc commit ab4e9eb

File tree

5 files changed

+7
-63
lines changed

5 files changed

+7
-63
lines changed

packages/bruno-app/src/components/RequestPane/RequestBody/RequestBodyMode/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { humanizeRequestBodyMode } from 'utils/collections';
88
import StyledWrapper from './StyledWrapper';
99
import { updateRequestBody } from 'providers/ReduxStore/slices/collections/index';
1010
import { toastError } from 'utils/common/error';
11-
import { prettifyJSON } from 'utils/common';
11+
import fastJsonFormat from 'fast-json-format';
1212
import xmlFormat from 'xml-formatter';
1313

1414
const RequestBodyMode = ({ item, collection }) => {
@@ -39,7 +39,7 @@ const RequestBodyMode = ({ item, collection }) => {
3939
const onPrettify = () => {
4040
if (body?.json && bodyMode === 'json') {
4141
try {
42-
const prettyBodyJson = prettifyJSON(body.json);
42+
const prettyBodyJson = fastJsonFormat(body.json);
4343
dispatch(
4444
updateRequestBody({
4545
content: prettyBodyJson,

packages/bruno-app/src/components/RequestPane/WsBody/SingleWSMessage/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import React, { useState } from 'react';
1010
import { useDispatch, useSelector } from 'react-redux';
1111
import { autoDetectLang } from 'utils/codemirror/lang-detect';
1212
import { toastError } from 'utils/common/error';
13-
import { prettifyJSON } from 'utils/common/index';
13+
import fastJsonFormat from 'fast-json-format';
1414
import xmlFormat from 'xml-formatter';
1515
import WSRequestBodyMode from '../BodyMode/index';
1616

@@ -105,7 +105,7 @@ export const SingleWSMessage = ({
105105
const onPrettify = () => {
106106
if (codeType === 'json') {
107107
try {
108-
const prettyBodyJson = prettifyJSON(content);
108+
const prettyBodyJson = fastJsonFormat(content);
109109
const currentMessages = [...(body.ws || [])];
110110
currentMessages[index] = {
111111
...currentMessages[index],

packages/bruno-app/src/utils/common/index.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { customAlphabet } from 'nanoid';
22
import xmlFormat from 'xml-formatter';
3-
import { format, applyEdits } from 'jsonc-parser';
43
import { JSONPath } from 'jsonpath-plus';
54
import fastJsonFormat from 'fast-json-format';
65

@@ -54,34 +53,6 @@ export const safeStringifyJSON = (obj, indent = false) => {
5453
}
5554
};
5655

57-
export const prettifyJSON = (obj, spaces = 2) => {
58-
try {
59-
const text = obj.replace(/\\"/g, '"').replace(/\\'/g, '\'');
60-
61-
const placeholders = [];
62-
const modifiedJson = text.replace(/"[^"]*?"|{{[^{}]+}}/g, (match) => {
63-
if (match.startsWith('{{')) {
64-
const placeholder = `__BRUNO_VAR_PLACEHOLDER_${placeholders.length}__`;
65-
placeholders.push(match);
66-
return `"${placeholder}"`; // Wrap bare variable in quotes to make it a valid JSON string
67-
}
68-
return match;
69-
});
70-
71-
const edits = format(modifiedJson, undefined, { tabSize: spaces, insertSpaces: true });
72-
let result = applyEdits(modifiedJson, edits);
73-
74-
for (let i = 0; i < placeholders.length; i++) {
75-
const placeholder = `__BRUNO_VAR_PLACEHOLDER_${i}__`;
76-
result = result.replace(`"${placeholder}"`, placeholders[i]);
77-
}
78-
79-
return result;
80-
} catch (e) {
81-
return obj;
82-
}
83-
};
84-
8556
export const safeParseXML = (str, options) => {
8657
if (!str || !str.length || typeof str !== 'string') {
8758
return str;

packages/bruno-app/src/utils/common/index.spec.js

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {
66
humanizeDate,
77
relativeDate,
88
getContentType,
9-
formatSize,
10-
prettifyJSON
9+
formatSize
1110
} from './index';
1211

1312
describe('common utils', () => {
@@ -192,30 +191,4 @@ describe('common utils', () => {
192191
expect(formatSize(NaN)).toBe('0B');
193192
});
194193
});
195-
196-
describe('prettifyJSON', () => {
197-
it('should prettify a standard JSON string', () => {
198-
const input = '{"key":"value","number":123}';
199-
const expected = '{\n "key": "value",\n "number": 123\n}';
200-
expect(prettifyJSON(input)).toBe(expected);
201-
});
202-
203-
it('should handle JSON with a Bruno variable as a value', () => {
204-
const input = '{"id":{{request_id}}}';
205-
const expected = '{\n "id": {{request_id}}\n}';
206-
expect(prettifyJSON(input)).toBe(expected);
207-
});
208-
209-
it('should handle JSON with a Bruno variable inside a string value', () => {
210-
const input = '{"url":"https://example.com/{{path}}"}';
211-
const expected = '{\n "url": "https://example.com/{{path}}"\n}';
212-
expect(prettifyJSON(input)).toBe(expected);
213-
});
214-
215-
it('should return the original string for invalid JSON', () => {
216-
const input = '{"key":"value",';
217-
const expected = '{\n "key": "value",';
218-
expect(prettifyJSON(input)).toBe(expected);
219-
});
220-
});
221194
});

packages/bruno-app/src/utils/curl/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { forOwn } from 'lodash';
2-
import { prettifyJSON } from 'utils/common';
32
import curlToJson from './curl-to-json';
3+
import fastJsonFormat from 'fast-json-format';
44

55
export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-request') => {
66
const parseFormData = (parsedBody) => {
@@ -67,7 +67,7 @@ export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-reque
6767
body.file = parsedBody;
6868
}else if (contentType.includes('application/json')) {
6969
body.mode = 'json';
70-
body.json = prettifyJSON(parsedBody);
70+
body.json = fastJsonFormat(parsedBody);
7171
} else if (contentType.includes('xml')) {
7272
body.mode = 'xml';
7373
body.xml = parsedBody;

0 commit comments

Comments
 (0)