feat(JSON tools): handle big float

Fix #254
This commit is contained in:
ShareVB committed 2026-01-01 16:52:04 +01:00
1 parent 93b688a1b9
commit 0c334e9560
20 files changed
+31 -27

No files matched your search

+1 -1
View File
@@ -29,7 +29,7 @@ function deserializeValue(value: string, tryParseValues: boolean): unknown {
return valueAsString;
}
try {
return JSON.parse(valueAsString);
return JSON.parseBigNum(valueAsString);
}
catch (_) {
return valueAsString;
@@ -9,10 +9,10 @@ const { t } = useI18n();
function parseJSON(value: string) {
try {
return JSON.parseBigInt(value);
return JSON.parseBigNum(value);
}
catch {
return JSON.parseBigInt(value.replace(/`((?:[^`]|\\`)*)`/g, (_m, s) => `"${s.replace(/"/g, '\\""').replace(/\r/g, '\\r').replace(/\n/g, '\\n')}"`));
return JSON.parseBigNum(value.replace(/`((?:[^`]|\\`)*)`/g, (_m, s) => `"${s.replace(/"/g, '\\""').replace(/\r/g, '\\r').replace(/\n/g, '\\n')}"`));
}
}
+1 -1
View File
@@ -26,7 +26,7 @@ const result = computedAsync(async () => {
const jqtypeValue = jqtype.value;
try {
const obj = JSON.parseBigInt(jsonString);
const obj = JSON.parseBigNum(jsonString);
if (jqtypeValue === 'jq') {
return JSON.stringify(await jq.json(obj, jqOrJsonPathString), null, indent);
}
+4
View File
@@ -1,4 +1,5 @@
import _ from 'lodash';
import Decimal from 'decimal.js';
import type { Difference, DifferenceStatus } from './json-diff.types';
export { diff };
@@ -109,6 +110,9 @@ function getType(value: unknown): 'object' | 'array' | 'value' {
if (Array.isArray(value)) {
return 'array';
}
if (Decimal.isDecimal(value)) {
return 'value';
}
if (typeof value === 'object') {
return 'object';
}
+2 -2
View File
@@ -11,8 +11,8 @@ const { t } = useI18n();
const rawLeftJson = ref('');
const rawRightJson = ref('');
const leftJson = computed(() => withDefaultOnError(() => JSON.parseBigInt(rawLeftJson.value), undefined));
const rightJson = computed(() => withDefaultOnError(() => JSON.parseBigInt(rawRightJson.value), undefined));
const leftJson = computed(() => withDefaultOnError(() => JSON.parseBigNum(rawLeftJson.value), undefined));
const rightJson = computed(() => withDefaultOnError(() => JSON.parseBigNum(rawRightJson.value), undefined));
const jsonValidationRules = [
{
+1 -1
View File
@@ -54,7 +54,7 @@ const formattedJson = computed(() => {
const jsonTextValue = jsonText.value;
try {
return JSON.stringify(
JSON.parseBigInt(jsonTextValue),
JSON.parseBigNum(jsonTextValue),
null, indentSize.value);
}
catch {
@@ -13,7 +13,7 @@ const defaultValue = '{a: {b: 2}}';
function transformer(value: string) {
return withDefaultOnError(
() => {
let o = JSON.parseBigInt(value);
let o = JSON.parseBigNum(value);
if (mode.value === 'flatten') {
o = flatten(o);
}
+2 -2
View File
@@ -45,7 +45,7 @@ const cleanJson = computed(() => {
jsonContentValue = jsonrepair(jsonContentValue);
}
return JSON.stringify(
JSON.parseBigInt(jsonContentValue),
JSON.parseBigNum(jsonContentValue),
null, indentSize.value);
}
catch (e: any) {
@@ -125,7 +125,7 @@ const cleanJson = computed(() => {
<c-card v-if="!conversionError || autoRepair" :title="autoRepair ? t('tools.json-linter.texts.title-repaired-version') : t('tools.json-linter.texts.title-formatted-version')" mt-5>
<n-form-item :label="t('tools.json-linter.texts.label-indent-size-0-compact')" label-placement="left">
<n-input-number-i18n v-model:value="indentSize" min="0" max="10" style="width: 100px" />
<n-input-number v-model:value="indentSize" min="0" max="10" style="width: 100px" />
</n-form-item>
<textarea-copyable :value="cleanJson" language="json" />
</c-card>
+2 -2
View File
@@ -12,8 +12,8 @@ const merged = computed(() => {
try {
return JSON.stringify(
merge(
JSON.parseBigInt(json1.value),
JSON.parseBigInt(json2.value),
JSON.parseBigNum(json1.value),
JSON.parseBigNum(json2.value),
),
null, 2,
);
+1 -1
View File
@@ -7,7 +7,7 @@ import { withDefaultOnError } from '@/utils/defaults';
const { t } = useI18n();
const defaultValue = '{\n\t"hello": [\n\t\t"world"\n\t]\n}';
const transformer = (value: string) => withDefaultOnError(() => JSON.stringify(JSON.parseBigInt(value), null, 0), '');
const transformer = (value: string) => withDefaultOnError(() => JSON.stringify(JSON.parseBigNum(value), null, 0), '');
const rules: UseValidationRule<string>[] = [
{
+1 -1
View File
@@ -32,7 +32,7 @@ const json = ref(`{
const result = computed(() => {
try {
const obj = JSON.parseBigInt(json.value);
const obj = JSON.parseBigNum(json.value);
return JSON.stringify(jsonquery(obj, jsonQuery.value), null, indent);
}
catch (e: any) {
+3 -3
View File
@@ -16,7 +16,7 @@ function sortObjectKeys<T>(obj: T, sortMethod: string): T {
.reduce((sortedObj, key) => {
sortedObj[key] = sortObjectKeys((obj as Record<string, unknown>)[key], sortMethod);
return sortedObj;
}, {} as Record<string, unknown>) as T;
}, Object.create(obj, {}) as Record<string, unknown>) as T;
}
function sortObjectValues<T>(obj: T, sortMethod: string, keyName: string): T {
@@ -46,7 +46,7 @@ function sortObjectValues<T>(obj: T, sortMethod: string, keyName: string): T {
return sortedKeys.reduce((sortedObj, key) => {
sortedObj[key] = sortObjectValues((obj as Record<string, unknown>)[key], sortMethod, keyName);
return sortedObj;
}, {} as Record<string, unknown>) as T;
}, Object.create(obj, {}) as Record<string, unknown>) as T;
}
return obj;
@@ -63,7 +63,7 @@ function formatJson({
keyName: MaybeRef<string>
indentSize?: MaybeRef<number>
}) {
const parsedObject = JSON.parseBigInt(get(rawJson));
const parsedObject = JSON.parseBigNum(get(rawJson));
if (['key_name', 'key_name_desc'].includes(get(sortMethod))) {
return JSON.stringify(sortObjectKeys(parsedObject, get(sortMethod)), null, get(indentSize));
+1 -1
View File
@@ -14,7 +14,7 @@ function transformer(value: string) {
if (value === '') {
return '';
}
return convertArrayToCsv({ arrayOrObject: JSON.parseBigInt(value) });
return convertArrayToCsv({ arrayOrObject: JSON.parseBigNum(value) });
}, '');
}
+1 -1
View File
@@ -9,7 +9,7 @@ import { withDefaultOnError } from '@/utils/defaults';
const { t } = useI18n();
function transformer(value: string) {
return withDefaultOnError(() => stringifyObject(JSON.parseBigInt(value), {
return withDefaultOnError(() => stringifyObject(JSON.parseBigNum(value), {
indent: ' ',
singleQuotes: false,
}), '');
@@ -22,7 +22,7 @@ const tableName = useQueryParamOrStorage({ name: 'table', storageName: 'json-to-
const sqlOutput = computed(() => {
try {
let arr = JSON.parseBigInt(jsonInput.value);
let arr = JSON.parseBigNum(jsonInput.value);
if (!Array.isArray(arr)) {
arr = [arr];
}
+1 -1
View File
@@ -19,7 +19,7 @@ function transformer(value: string) {
if (value === '') {
return '';
}
return encode(JSON.parseBigInt(value));
return encode(JSON.parseBigNum(value));
}, '');
}
+1 -1
View File
@@ -8,7 +8,7 @@ const { t } = useI18n();
const defaultValue = '{"a":{"_attributes":{"x":"1.234","y":"It\'s"}}}';
function transformer(value: string) {
return withDefaultOnError(() => {
return convert.js2xml(JSON.parseBigInt(value), { compact: true });
return convert.js2xml(JSON.parseBigNum(value), { compact: true });
}, '');
}
@@ -8,7 +8,7 @@ import { withDefaultOnError } from '@/utils/defaults';
const { t } = useI18n();
const transformer = (value: string) => withDefaultOnError(() => stringify(JSON.parseBigInt(value)), '');
const transformer = (value: string) => withDefaultOnError(() => stringify(JSON.parseBigNum(value)), '');
const rules: UseValidationRule<string>[] = [
{
+1 -1
View File
@@ -28,7 +28,7 @@ const rawJsonValidation = useValidation({
source: rawJson,
rules: [
{
validator: v => v === '' || (get(repairJson) ? jsonrepair(v) : JSON.parseBigInt(v)),
validator: v => v === '' || (get(repairJson) ? jsonrepair(v) : JSON.parseBigNum(v)),
get message() {
return t('tools.json-viewer.text.provided-json-is-not-valid') + (!get(repairJson) ? t('tools.json-viewer.text.try-again-with-repairjsonlabel', [repairJsonLabel]) : '');
},
+3 -3
View File
@@ -1,6 +1,6 @@
import { type MaybeRef, get } from '@vueuse/core';
import { jsonrepair } from 'jsonrepair';
import '@/utils/json5-bigint';
import '@/utils/json5-bignum';
export { sortObjectKeys, formatJson };
@@ -18,7 +18,7 @@ function sortObjectKeys<T>(obj: T): T {
.reduce((sortedObj, key) => {
sortedObj[key] = sortObjectKeys((obj as Record<string, unknown>)[key]);
return sortedObj;
}, {} as Record<string, unknown>) as T;
}, Object.create(obj, {}) as Record<string, unknown>) as T;
}
function unescapeUnicodeJSON(str: string) {
@@ -42,7 +42,7 @@ function formatJson({
}) {
const unwrappedJson = get(rawJson);
const jsonString = get(repairJson) ? jsonrepair(unwrappedJson) : unwrappedJson;
const parsedObject = JSON.parseBigInt(get(unescapeUnicode) ? unescapeUnicodeJSON(jsonString) : jsonString);
const parsedObject = JSON.parseBigNum(get(unescapeUnicode) ? unescapeUnicodeJSON(jsonString) : jsonString);
return JSON.stringify(get(sortKeys) ? sortObjectKeys(parsedObject) : parsedObject, null, get(indentSize));
}