Fix: Prettify JSON for Res-preview without parsing to avoid JS specific roundings

This commit is contained in:
ramki-bruno
2025-02-27 12:01:58 +05:30
parent 4406cc573a
commit 56581b3641
2 changed files with 14 additions and 3 deletions

View File

@@ -9,7 +9,7 @@ import QueryResultPreview from './QueryResultPreview';
import StyledWrapper from './StyledWrapper';
import { useState, useMemo, useEffect } from 'react';
import { useTheme } from 'providers/Theme/index';
import { uuid } from 'utils/common/index';
import { prettifyJson, uuid } from 'utils/common/index';
const formatResponse = (data, dataBuffer, mode, filter) => {
if (data === undefined || !dataBuffer) {
@@ -33,12 +33,15 @@ const formatResponse = (data, dataBuffer, mode, filter) => {
if (filter) {
try {
data = JSONPath({ path: filter, json: data });
return prettifyJson(JSON.stringify(data));
} catch (e) {
console.warn('Could not apply JSONPath filter:', e.message);
}
}
return safeStringifyJSON(data, true);
// Prettify the JSON string directly instead of parse->stringify to avoid
// issues like rounding numbers bigger than Number.MAX_SAFE_INTEGER etc.
return prettifyJson(rawData);
}
if (mode.includes('xml')) {
@@ -53,7 +56,7 @@ const formatResponse = (data, dataBuffer, mode, filter) => {
return data;
}
return safeStringifyJSON(data, true);
return prettifyJson(rawData);
};
const formatErrorMessage = (error) => {

View File

@@ -1,5 +1,6 @@
import { customAlphabet } from 'nanoid';
import xmlFormat from 'xml-formatter';
import { format as jsoncFormat, applyEdits as jsoncApplyEdits } from 'jsonc-parser';
// a customized version of nanoid without using _ and -
export const uuid = () => {
@@ -26,6 +27,13 @@ export const waitForNextTick = () => {
});
};
export const prettifyJson = (doc) => {
return jsoncApplyEdits(
doc,
jsoncFormat(doc, null, {insertSpaces: true, tabSize: 2})
);
}
export const safeParseJSON = (str) => {
if (!str || !str.length || typeof str !== 'string') {
return str;