fix: JSON stringify/parse BigInt

Fix #1615
This commit is contained in:
ShareVB committed 2025-07-20 21:01:01 +02:00
1 parent a8c6ebc81b
commit ad312d788f
16 files changed
+64 -19

No files matched your search

+6
View File
@@ -9,6 +9,7 @@ import { registerSW } from 'virtual:pwa-register';
import shadow from 'vue-shadow-dom';
import { hideSplashScreen } from 'vite-plugin-splash-screen/runtime';
import { plausible } from './plugins/plausible.plugin';
import '@/utils/json5-bigint';
import 'virtual:uno.css';
@@ -20,6 +21,11 @@ import { i18nPlugin } from './plugins/i18n.plugin';
import store from './tools/pomodoro-timer/app/store';
// eslint-disable-next-line no-extend-native
BigInt.prototype.toJSON = function () {
return JSON.rawJSON ? JSON.rawJSON(this.toString()) : this.toString();
};
library.add(fas);
registerSW();
+9
View File
@@ -43,3 +43,12 @@ declare module 'pdf-signature-reader' {
declare module 'vite-plugin-splash-screen/runtime' {
export function hideSplashScreen();
}
interface BigInt {
toJSON: () => string;
}
interface JSON {
parseBigInt: (jsonStr: string, options?: { minDigits?: number; fallbackToString?: boolean; }) => any;
rawJSON(value: string): any;
}
@@ -1,6 +1,5 @@
<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import JSON5 from 'json5';
import type { UseValidationRule } from '@/composable/validation';
import { isNotThrowing } from '@/utils/boolean';
import { withDefaultOnError } from '@/utils/defaults';
@@ -9,10 +8,10 @@ const { t } = useI18n();
function parseJSON(value: string) {
try {
return JSON5.parse(value);
return JSON.parseBigInt(value);
}
catch {
return JSON5.parse(value.replace(/`((?:[^`]|\\`)*)`/g, (_m, s) => `"${s.replace(/"/g, '\\""').replace(/\r/g, '\\r').replace(/\n/g, '\\n')}"`));
return JSON.parseBigInt(value.replace(/`((?:[^`]|\\`)*)`/g, (_m, s) => `"${s.replace(/"/g, '\\""').replace(/\r/g, '\\r').replace(/\n/g, '\\n')}"`));
}
}
+1 -1
View File
@@ -25,7 +25,7 @@ const result = computedAsync(async () => {
const jqtypeValue = jqtype.value;
try {
const obj = JSON5.parse(jsonString);
const obj = JSON.parseBigInt(jsonString);
if (jqtypeValue === 'jq') {
return JSON.stringify(await jq.json(obj, jqOrJsonPathString), null, indent);
}
+2 -2
View File
@@ -11,8 +11,8 @@ const { t } = useI18n();
const rawLeftJson = ref('');
const rawRightJson = ref('');
const leftJson = computed(() => withDefaultOnError(() => JSON5.parse(rawLeftJson.value), undefined));
const rightJson = computed(() => withDefaultOnError(() => JSON5.parse(rawRightJson.value), undefined));
const leftJson = computed(() => withDefaultOnError(() => JSON.parseBigInt(rawLeftJson.value), undefined));
const rightJson = computed(() => withDefaultOnError(() => JSON.parseBigInt(rawRightJson.value), undefined));
const jsonValidationRules = [
{
+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(JSON5.parse(value), null, 0), '');
const transformer = (value: string) => withDefaultOnError(() => JSON.stringify(JSON.parseBigInt(value), null, 0), '');
const rules: UseValidationRule<string>[] = [
{
+1 -1
View File
@@ -29,7 +29,7 @@ const json = ref(`{
const result = computed(() => {
try {
const obj = JSON5.parse(json.value);
const obj = JSON.parseBigInt(json.value);
return JSON.stringify(jsonquery(obj, jsonQuery.value), null, indent);
}
catch (e: any) {
+1 -2
View File
@@ -1,5 +1,4 @@
import { type MaybeRef, get } from '@vueuse/core';
import JSON5 from 'json5';
export { sortObjectKeys, sortObjectValues, formatJson };
@@ -64,7 +63,7 @@ function formatJson({
keyName: MaybeRef<string>
indentSize?: MaybeRef<number>
}) {
const parsedObject = JSON5.parse(get(rawJson));
const parsedObject = JSON.parseBigInt(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: JSON5.parse(value) });
return convertArrayToCsv({ arrayOrObject: JSON.parseBigInt(value) });
}, '');
}
+1 -1
View File
@@ -9,7 +9,7 @@ import { withDefaultOnError } from '@/utils/defaults';
const { t } = useI18n();
function transformer(value: string) {
return withDefaultOnError(() => stringifyObject(JSON5.parse(value), {
return withDefaultOnError(() => stringifyObject(JSON.parseBigInt(value), {
indent: ' ',
singleQuotes: false,
}), '');
@@ -21,7 +21,7 @@ const tableName = ref<string>('TableName');
const sqlOutput = computed(() => {
try {
let arr = JSON5.parse(jsonInput.value);
let arr = JSON.parseBigInt(jsonInput.value);
if (!Array.isArray(arr)) {
arr = [arr];
}
+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(JSON5.parse(value), { compact: true });
return convert.js2xml(JSON.parseBigInt(value), { compact: true });
}, '');
}
@@ -8,7 +8,7 @@ import { withDefaultOnError } from '@/utils/defaults';
const { t } = useI18n();
const transformer = (value: string) => withDefaultOnError(() => stringify(JSON5.parse(value)), '');
const transformer = (value: string) => withDefaultOnError(() => stringify(JSON.parseBigInt(value)), '');
const rules: UseValidationRule<string>[] = [
{
+1 -2
View File
@@ -1,6 +1,5 @@
<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import JSON5 from 'json5';
import { jsonrepair } from 'jsonrepair';
import {
get,
@@ -27,7 +26,7 @@ const rawJsonValidation = useValidation({
source: rawJson,
rules: [
{
validator: v => v === '' || (get(repairJson) ? jsonrepair(v) : JSON5.parse(v)),
validator: v => v === '' || (get(repairJson) ? jsonrepair(v) : JSON.parseBigInt(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]) : '');
},
+2 -2
View File
@@ -1,6 +1,6 @@
import { type MaybeRef, get } from '@vueuse/core';
import JSON5 from 'json5';
import { jsonrepair } from 'jsonrepair';
import '@/utils/json5-bigint';
export { sortObjectKeys, formatJson };
@@ -42,7 +42,7 @@ function formatJson({
}) {
const unwrappedJson = get(rawJson);
const jsonString = get(repairJson) ? jsonrepair(unwrappedJson) : unwrappedJson;
const parsedObject = JSON5.parse(get(unescapeUnicode) ? unescapeUnicodeJSON(jsonString) : jsonString);
const parsedObject = JSON.parseBigInt(get(unescapeUnicode) ? unescapeUnicodeJSON(jsonString) : jsonString);
return JSON.stringify(get(sortKeys) ? sortObjectKeys(parsedObject) : parsedObject, null, get(indentSize));
}
+33
View File
@@ -0,0 +1,33 @@
import JSON5 from 'json5';
function quoteLargeNumbers(jsonStr: string, minDigits: number): string {
const regex = new RegExp(`(-?\\d{${minDigits},})(?=\\s*[,}\\]])`, 'g');
return jsonStr.replace(regex, '"$1"');
}
JSON.parseBigInt = function (jsonStr: string, options?: { minDigits?: number; fallbackToString?: boolean }): unknown {
const minDigits = options?.minDigits ?? 15;
const fallbackToString = options?.fallbackToString ?? true;
const safeStr = quoteLargeNumbers(jsonStr, minDigits);
return JSON5.parse(safeStr, (_key, value) => {
if (typeof value === 'string') {
const bigintRegex = new RegExp(`^-?\\d{${minDigits},}$`);
if (bigintRegex.test(value)) {
try {
if (typeof BigInt !== 'undefined') {
return BigInt(value);
}
else if (fallbackToString) {
return value;
}
}
catch {
return value;
}
}
}
return value;
});
};