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

Handling json with long numbers #740

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"baseui": "^14.0.0",
"copy-to-clipboard": "^3.3.3",
"lodash": "^4.17.21",
"lossless-json": "^4.0.2",
"next": "14.2.16",
"next-logger": "^5.0.0",
"pino": "^9.3.2",
Expand Down
11 changes: 6 additions & 5 deletions src/components/pretty-json/pretty-json.types.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export type Props = {
json: JsonValue;
json: PrettyJsonValue;
};

export type JsonValue =
export type PrettyJsonValue =
| string
| number
| boolean
| null
| JsonObject
| JsonArray;
| JsonArray
| bigint;

type JsonObject = { [key: string]: JsonValue };
type JsonArray = JsonValue[];
type JsonObject = { [key: string]: PrettyJsonValue };
type JsonArray = PrettyJsonValue[];
2 changes: 2 additions & 0 deletions src/route-handlers/query-workflow/query-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export async function queryWorkflow(
},
});

// TODO @assem.hafez: we may loss numeric percision here as we are parsing a result from other languages that may include long numbers
// one options is not to parse the result and return it as string and handle the parsing on the client side
return NextResponse.json({
result: res.queryResult
? JSON.parse(
Expand Down
52 changes: 52 additions & 0 deletions src/utils/__tests__/lossless-json-parse.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import losslessJsonParse from '../lossless-json-parse';

describe('losslessJsonParse', () => {
it('should parse large numbers as BigInt', () => {
const json = '{"bigNumber": 90071992547409923213}';
const result = losslessJsonParse(json);
expect(result).toEqual({ bigNumber: BigInt('90071992547409923213') });
});

it('should parse strings of large numbers as string', () => {
const json = '{"bigNumber": "90071992547409923213"}';
const result = losslessJsonParse(json);
expect(result).toEqual({ bigNumber: '90071992547409923213' });
});

it('should parse a JSON string with integers correctly', () => {
const json = '{"integer": 42}';
const result = losslessJsonParse(json);
expect(result).toEqual({ integer: 42 });
});

it('should parse floating point numbers correctly', () => {
const json = '{"float": 3.14}';
const result = losslessJsonParse(json);
expect(result).toEqual({ float: 3.14 });
});

it('should use the provided reviver function', () => {
const json = '{"key": "value"}';
const reviver = (key: string, value: any) => {
if (key === 'key') {
return 'newValue';
}
return value;
};
const result = losslessJsonParse(json, reviver);
expect(result).toEqual({ key: 'newValue' });
});

it('should handle null and undefined reviver correctly', () => {
const json = '{"key": "value"}';
const resultWithNullReviver = losslessJsonParse(json, null);
const resultWithUndefinedReviver = losslessJsonParse(json, undefined);
expect(resultWithNullReviver).toEqual({ key: 'value' });
expect(resultWithUndefinedReviver).toEqual({ key: 'value' });
});

it('should throw an error if the JSON is invalid', () => {
const json = '{"key": "value"';
expect(() => losslessJsonParse(json)).toThrow();
});
});
65 changes: 65 additions & 0 deletions src/utils/__tests__/lossless-json-stringify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import losslessJsonStringify from '../lossless-json-stringify';

describe('losslessJsonStringify', () => {
it('should stringify a JSON object with safe numbers correctly', () => {
const json = { number: 123 };
const result = losslessJsonStringify(json);
expect(result).toBe('{"number":123}');
});

it('should stringify BigInts as numbers', () => {
const json = { bigNumber: BigInt('900719925474099223423423') };
const result = losslessJsonStringify(json);
expect(result).toBe('{"bigNumber":900719925474099223423423}');
});

it('should stringify integers correctly', () => {
const json = { integer: 42 };
const result = losslessJsonStringify(json);
expect(result).toBe('{"integer":42}');
});

it('should stringify floating point numbers correctly', () => {
const json = { float: 3.14 };
const result = losslessJsonStringify(json);
expect(result).toBe('{"float":3.14}');
});

it('should use the provided reviver function', () => {
const json = { key: 'value' };
const reviver = (key: string, value: any) => {
if (key === 'key') {
return 'newValue';
}
return value;
};
const result = losslessJsonStringify(json, reviver);
expect(result).toBe('{"key":"newValue"}');
});

it('should handle null and undefined reviver correctly', () => {
const json = { key: 'value' };
const resultWithNullReviver = losslessJsonStringify(json, null);
const resultWithUndefinedReviver = losslessJsonStringify(json, undefined);
expect(resultWithNullReviver).toBe('{"key":"value"}');
expect(resultWithUndefinedReviver).toBe('{"key":"value"}');
});

it('should format JSON with spaces correctly', () => {
const json = { key: 'value' };
const result = losslessJsonStringify(json, null, 2);
expect(result).toBe('{\n "key": "value"\n}');
});

it('should return an empty string for invalid JSON', () => {
const json = () => {};
const result = losslessJsonStringify(json);
expect(result).toBe('');
});

it('should remove invalid JSON keys', () => {
const json = { key: 'value', fn: () => {} };
const result = losslessJsonStringify(json);
expect(result).toBe('{"key":"value"}');
});
});
7 changes: 5 additions & 2 deletions src/utils/data-formatters/__tests__/format-payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ describe('formatPayload', () => {
});

it('should parse JSON data correctly', () => {
const payload = { data: btoa(JSON.stringify({ key: 'value' })) };
expect(formatPayload(payload)).toEqual({ key: 'value' });
const payload = { data: btoa('{"key":"value","long":284789263475236586}') };
expect(formatPayload(payload)).toEqual({
key: 'value',
long: BigInt('284789263475236586'),
});
});

it('should remove double quotes from the string if JSON parsing fails', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/data-formatters/format-input-payload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logger from '@/utils/logger';

import losslessJsonParse from '../lossless-json-parse';
const formatInputPayload = (
payload: { data?: string | null } | null | undefined
) => {
Expand All @@ -25,7 +26,7 @@ function parseJsonLines(input: string) {

try {
// Try to parse the current JSON string
const jsonObject = JSON.parse(currentJson);
const jsonObject = losslessJsonParse(currentJson);
// If successful, add the object to the array
jsonArray.push(jsonObject);
// Reset currentJson for the next JSON object
Expand All @@ -38,7 +39,7 @@ function parseJsonLines(input: string) {
// Handle case where the last JSON object might be malformed
if (currentJson.trim() !== '') {
try {
const jsonObject = JSON.parse(currentJson);
const jsonObject = losslessJsonParse(currentJson);
jsonArray.push(jsonObject);
} catch {
logger.error(
Expand Down
4 changes: 3 additions & 1 deletion src/utils/data-formatters/format-payload.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import losslessJsonParse from '../lossless-json-parse';

const formatPayload = (
payload: { data?: string | null } | null | undefined
) => {
Expand All @@ -11,7 +13,7 @@ const formatPayload = (

// try parsing as JSON
try {
return JSON.parse(parsedData);
return losslessJsonParse(parsedData);
} catch {
// remove double quotes from the string
const formattedString = parsedData.replace(/"/g, '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ exports[`formatWorkflowHistoryEvent should format workflow activityTaskScheduled
},
"heartbeatTimeoutSeconds": 0,
"input": [
1725747370575410000,
1725747370575409843n,
"gadence-canary-xdc",
"workflow.sanity",
],
Expand Down Expand Up @@ -523,7 +523,7 @@ exports[`formatWorkflowHistoryEvent should format workflow startChildWorkflowExe
"fields": {},
},
"input": [
1726492751798812400,
1726492751798812308n,
30000000000,
],
"jitterStart": null,
Expand Down
16 changes: 16 additions & 0 deletions src/utils/lossless-json-parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { parse, isSafeNumber, isInteger, type Reviver } from 'lossless-json';

export default function losslessJsonParse(
json: string,
reviver?: Reviver | null | undefined
) {
return parse(json, reviver, (value) => {
if (!isSafeNumber(value)) {
return BigInt(value);
}
if (isInteger(value)) {
return parseInt(value);
}
return parseFloat(value);
});
}
16 changes: 16 additions & 0 deletions src/utils/lossless-json-stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { stringify, type Reviver } from 'lossless-json';

export default function losslessJsonStringify(
json: unknown,
reviver?: Reviver | null | undefined,
space?: string | number | undefined
) {
const stringified = stringify(json, reviver, space, [
{
test: (value) => typeof value === 'bigint',
stringify: (value) => (value || '').toString(),
},
]);

return stringified || '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from 'react';

import { render } from '@/test-utils/rtl';

import losslessJsonStringify from '@/utils/lossless-json-stringify';

import WorkflowSummaryTabJsonView from '../workflow-history-event-details-json';

jest.mock('@/components/copy-text-button/copy-text-button', () =>
Expand All @@ -13,23 +15,28 @@ jest.mock('@/components/pretty-json/pretty-json', () =>
);

describe('WorkflowSummaryTabJsonView Component', () => {
const inputJson = { input: 'inputJson' };
const losslessInputJson = {
input: 'inputJson',
long: BigInt('9007199254740991345435'),
};

it('renders correctly with initial props', () => {
const { getByText } = render(
<WorkflowSummaryTabJsonView entryValue={inputJson} />
<WorkflowSummaryTabJsonView entryValue={losslessInputJson} />
);

expect(getByText('PrettyJson Mock')).toBeInTheDocument();
});

it('renders copy text button and pass the correct text', () => {
const { getByText } = render(
<WorkflowSummaryTabJsonView entryValue={inputJson} />
<WorkflowSummaryTabJsonView entryValue={losslessInputJson} />
);

const copyButton = getByText(/Copy Button/);
expect(copyButton).toBeInTheDocument();
expect(copyButton.innerHTML).toMatch(JSON.stringify(inputJson, null, '\t'));
expect(copyButton.innerHTML).toMatch(
losslessJsonStringify(losslessInputJson, null, '\t')
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useMemo } from 'react';
import CopyTextButton from '@/components/copy-text-button/copy-text-button';
import PrettyJson from '@/components/pretty-json/pretty-json';
import useStyletronClasses from '@/hooks/use-styletron-classes';
import losslessJsonStringify from '@/utils/lossless-json-stringify';

import { cssStyles } from './workflow-history-event-details-json.styles';
import type { Props } from './workflow-history-event-details-json.types';
Expand All @@ -12,7 +13,7 @@ export default function WorkflowHistoryEventDetailsJson({ entryValue }: Props) {
const { cls } = useStyletronClasses(cssStyles);

const textToCopy = useMemo(() => {
return JSON.stringify(entryValue, null, '\t');
return losslessJsonStringify(entryValue, null, '\t');
}, [entryValue]);
return (
<div className={cls.jsonViewWrapper}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { type GetWorkflowHistoryResponse } from '@/route-handlers/get-workflow-h
import formatWorkflowHistoryEvent from '@/utils/data-formatters/format-workflow-history-event';
import { type FormattedHistoryEvent } from '@/utils/data-formatters/schema/format-history-event-schema';
import logger from '@/utils/logger';
import losslessJsonStringify from '@/utils/lossless-json-stringify';
import request from '@/utils/request';
import { RequestError } from '@/utils/request/request-error';

Expand All @@ -23,7 +24,7 @@ export default function WorkflowHistoryExportJsonButton(props: Props) {
>('idle');

const downloadJSON = (jsonData: any) => {
const blob = new Blob([JSON.stringify(jsonData, null, '\t')], {
const blob = new Blob([losslessJsonStringify(jsonData, null, '\t')], {
type: 'application/json',
});
const url = window.URL.createObjectURL(blob);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useMemo } from 'react';

import CopyTextButton from '@/components/copy-text-button/copy-text-button';
import PrettyJson from '@/components/pretty-json/pretty-json';
import losslessJsonStringify from '@/utils/lossless-json-stringify';

import getQueryJsonContent from './helpers/get-query-json-content';
import { styled } from './workflow-queries-result-json.styles';
Expand All @@ -15,7 +16,7 @@ export default function WorkflowQueriesResultJson(props: Props) {
);

const textToCopy = useMemo(() => {
return JSON.stringify(content, null, '\t');
return losslessJsonStringify(content, null, '\t');
}, [content]);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type JsonValue } from '@/components/pretty-json/pretty-json.types';
import { type PrettyJsonValue } from '@/components/pretty-json/pretty-json.types';
import { type QueryWorkflowResponse } from '@/route-handlers/query-workflow/query-workflow.types';
import { type RequestError } from '@/utils/request/request-error';

Expand All @@ -9,6 +9,6 @@ export type Props = {
};

export type QueryJsonContent = {
content: JsonValue | undefined;
content: PrettyJsonValue | undefined;
isError: boolean;
};
Loading
Loading