feat(JSON Prettifier): add json repair option

This commit is contained in:
ShareVB committed 2025-04-21 22:31:37 +02:00
commit 2519cc05d1
5 files changed
+45 -10

No files matched your search

+1
View File
@@ -174,6 +174,7 @@
"json-editor-vue": "^0.16.0",
"json-to-ts": "^1.7.0",
"json5": "^2.2.3",
"jsonrepair": "^3.12.0",
"jsonar-mod": "^1.9.0",
"jsonlint-mod": "^1.7.6",
"jsonpath": "^1.1.1",
+6 -3
View File
@@ -428,6 +428,9 @@ dependencies:
jsonpath:
specifier: ^1.1.1
version: 1.1.1
jsonrepair:
specifier: ^3.12.0
version: 3.12.0
jsstack.js:
specifier: ^2.0.0
version: 2.0.0
@@ -12906,8 +12909,8 @@ packages:
engines: {node: '>=0.10.0'}
dev: true
/jsonrepair@3.11.2:
resolution: {integrity: sha512-ejydGcTq0qKk1r0NUBwjtvswbPFhs19+QEfwSeGwB8KJZ59W7/AOFmQh04c68mkJ+2hGk+OkOmkr2bKG4tGlLQ==}
/jsonrepair@3.12.0:
resolution: {integrity: sha512-SWfjz8SuQ0wZjwsxtSJ3Zy8vvLg6aO/kxcp9TWNPGwJKgTZVfhNEQBMk/vPOpYCDFWRxD6QWuI6IHR1t615f0w==}
hasBin: true
dev: false
@@ -17668,7 +17671,7 @@ packages:
immutable-json-patch: 6.0.1
jmespath: 0.16.0
json-source-map: 0.6.1
jsonrepair: 3.11.2
jsonrepair: 3.12.0
lodash-es: 4.17.21
memoize-one: 6.0.0
natural-compare-lite: 1.4.0
+16 -4
View File
@@ -1,27 +1,36 @@
<script setup lang="ts">
import JSON5 from 'json5';
import { useStorage } from '@vueuse/core';
import { jsonrepair } from 'jsonrepair';
import {
get,
useStorage,
} from '@vueuse/core';
import { formatJson } from './json.models';
import { withDefaultOnError } from '@/utils/defaults';
import { useValidation } from '@/composable/validation';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
const inputElement = ref<HTMLElement>();
const repairJsonLabel = 'Repair JSON';
const rawJson = useStorage('json-prettify:raw-json', '{"hello": "world", "foo": "bar"}');
const indentSize = useStorage('json-prettify:indent-size', 3);
const sortKeys = useStorage('json-prettify:sort-keys', true);
const unescapeUnicode = useStorage('json-prettify:unescape-unicode', false);
const cleanJson = computed(() => withDefaultOnError(() => formatJson({ rawJson, indentSize, sortKeys, unescapeUnicode }), ''));
const repairJson = useStorage('json-prettify:repair-json', false);
const cleanJson = computed(() => withDefaultOnError(() => formatJson({ rawJson, indentSize, sortKeys, unescapeUnicode, repairJson }), ''));
const rawJsonValidation = useValidation({
source: rawJson,
rules: [
{
validator: v => v === '' || JSON5.parse(v),
message: 'Provided JSON is not valid.',
validator: v => v === '' || (get(repairJson) ? jsonrepair(v) : JSON5.parse(v)),
get message() {
return `Provided JSON is not valid.${!get(repairJson) ? ` Try again with "${repairJsonLabel}"` : ''}`;
},
},
],
watch: [repairJson],
});
</script>
@@ -37,6 +46,9 @@ const rawJsonValidation = useValidation({
<n-form-item label="Indent size :" label-placement="left" label-width="100" :show-feedback="false">
<n-input-number v-model:value="indentSize" min="0" max="10" style="width: 100px" />
</n-form-item>
<n-form-item :label="`${repairJsonLabel} :`" label-placement="left" label-width="110">
<n-switch v-model:value="repairJson" />
</n-form-item>
</div>
</div>
+16 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { sortObjectKeys } from './json.models';
import { formatJson, sortObjectKeys } from './json.models';
describe('json models', () => {
describe('sortObjectKeys', () => {
@@ -13,4 +13,19 @@ describe('json models', () => {
);
});
});
describe('formatJson', () => {
describe('repairJson', () => {
it('repairs and formats JSON when repairJson is true', () => {
const rawJson = '{"foo": "bar", "baz":}';
const result = formatJson({ rawJson, sortKeys: false, repairJson: true });
expect(result).to.equal(JSON.stringify({ foo: 'bar', baz: null }, null, 3));
});
it('throws an error for invalid JSON when repairJson is false', () => {
const rawJson = '{"foo": "bar", "baz":}';
expect(() => formatJson({ rawJson, sortKeys: false, repairJson: false })).to.throw();
});
});
});
});
+6 -2
View File
@@ -1,5 +1,6 @@
import { type MaybeRef, get } from '@vueuse/core';
import JSON5 from 'json5';
import { jsonrepair } from 'jsonrepair';
export { sortObjectKeys, formatJson };
@@ -31,14 +32,17 @@ function formatJson({
sortKeys = true,
indentSize = 3,
unescapeUnicode = false,
repairJson = false,
}: {
rawJson: MaybeRef<string>
sortKeys?: MaybeRef<boolean>
indentSize?: MaybeRef<number>
unescapeUnicode?: MaybeRef<boolean>
repairJson?: MaybeRef<boolean>
}) {
const raw = get(rawJson);
const parsedObject = JSON5.parse(get(unescapeUnicode) ? unescapeUnicodeJSON(raw) : raw);
const unwrappedJson = get(rawJson);
const jsonString = get(repairJson) ? jsonrepair(unwrappedJson) : unwrappedJson;
const parsedObject = JSON5.parse(get(unescapeUnicode) ? unescapeUnicodeJSON(jsonString) : jsonString);
return JSON.stringify(get(sortKeys) ? sortObjectKeys(parsedObject) : parsedObject, null, get(indentSize));
}