mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-23 06:11:06 +03:00
feat(JSON Prettifier): add json repair option
This commit is contained in:
commit
2519cc05d1
5 files changed
+45
-10
No files matched your search
@@ -174,6 +174,7 @@
|
|||||||
"json-editor-vue": "^0.16.0",
|
"json-editor-vue": "^0.16.0",
|
||||||
"json-to-ts": "^1.7.0",
|
"json-to-ts": "^1.7.0",
|
||||||
"json5": "^2.2.3",
|
"json5": "^2.2.3",
|
||||||
|
"jsonrepair": "^3.12.0",
|
||||||
"jsonar-mod": "^1.9.0",
|
"jsonar-mod": "^1.9.0",
|
||||||
"jsonlint-mod": "^1.7.6",
|
"jsonlint-mod": "^1.7.6",
|
||||||
"jsonpath": "^1.1.1",
|
"jsonpath": "^1.1.1",
|
||||||
|
|||||||
Generated
+6
-3
@@ -428,6 +428,9 @@ dependencies:
|
|||||||
jsonpath:
|
jsonpath:
|
||||||
specifier: ^1.1.1
|
specifier: ^1.1.1
|
||||||
version: 1.1.1
|
version: 1.1.1
|
||||||
|
jsonrepair:
|
||||||
|
specifier: ^3.12.0
|
||||||
|
version: 3.12.0
|
||||||
jsstack.js:
|
jsstack.js:
|
||||||
specifier: ^2.0.0
|
specifier: ^2.0.0
|
||||||
version: 2.0.0
|
version: 2.0.0
|
||||||
@@ -12906,8 +12909,8 @@ packages:
|
|||||||
engines: {node: '>=0.10.0'}
|
engines: {node: '>=0.10.0'}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/jsonrepair@3.11.2:
|
/jsonrepair@3.12.0:
|
||||||
resolution: {integrity: sha512-ejydGcTq0qKk1r0NUBwjtvswbPFhs19+QEfwSeGwB8KJZ59W7/AOFmQh04c68mkJ+2hGk+OkOmkr2bKG4tGlLQ==}
|
resolution: {integrity: sha512-SWfjz8SuQ0wZjwsxtSJ3Zy8vvLg6aO/kxcp9TWNPGwJKgTZVfhNEQBMk/vPOpYCDFWRxD6QWuI6IHR1t615f0w==}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
@@ -17668,7 +17671,7 @@ packages:
|
|||||||
immutable-json-patch: 6.0.1
|
immutable-json-patch: 6.0.1
|
||||||
jmespath: 0.16.0
|
jmespath: 0.16.0
|
||||||
json-source-map: 0.6.1
|
json-source-map: 0.6.1
|
||||||
jsonrepair: 3.11.2
|
jsonrepair: 3.12.0
|
||||||
lodash-es: 4.17.21
|
lodash-es: 4.17.21
|
||||||
memoize-one: 6.0.0
|
memoize-one: 6.0.0
|
||||||
natural-compare-lite: 1.4.0
|
natural-compare-lite: 1.4.0
|
||||||
|
|||||||
@@ -1,27 +1,36 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import JSON5 from 'json5';
|
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 { formatJson } from './json.models';
|
||||||
import { withDefaultOnError } from '@/utils/defaults';
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
import { useValidation } from '@/composable/validation';
|
import { useValidation } from '@/composable/validation';
|
||||||
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
||||||
|
|
||||||
const inputElement = ref<HTMLElement>();
|
const inputElement = ref<HTMLElement>();
|
||||||
|
const repairJsonLabel = 'Repair JSON';
|
||||||
|
|
||||||
const rawJson = useStorage('json-prettify:raw-json', '{"hello": "world", "foo": "bar"}');
|
const rawJson = useStorage('json-prettify:raw-json', '{"hello": "world", "foo": "bar"}');
|
||||||
const indentSize = useStorage('json-prettify:indent-size', 3);
|
const indentSize = useStorage('json-prettify:indent-size', 3);
|
||||||
const sortKeys = useStorage('json-prettify:sort-keys', true);
|
const sortKeys = useStorage('json-prettify:sort-keys', true);
|
||||||
const unescapeUnicode = useStorage('json-prettify:unescape-unicode', false);
|
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({
|
const rawJsonValidation = useValidation({
|
||||||
source: rawJson,
|
source: rawJson,
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
validator: v => v === '' || JSON5.parse(v),
|
validator: v => v === '' || (get(repairJson) ? jsonrepair(v) : JSON5.parse(v)),
|
||||||
message: 'Provided JSON is not valid.',
|
get message() {
|
||||||
|
return `Provided JSON is not valid.${!get(repairJson) ? ` Try again with "${repairJsonLabel}"` : ''}`;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
watch: [repairJson],
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -37,6 +46,9 @@ const rawJsonValidation = useValidation({
|
|||||||
<n-form-item label="Indent size :" label-placement="left" label-width="100" :show-feedback="false">
|
<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-input-number v-model:value="indentSize" min="0" max="10" style="width: 100px" />
|
||||||
</n-form-item>
|
</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>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it } from 'vitest';
|
import { describe, expect, it } from 'vitest';
|
||||||
import { sortObjectKeys } from './json.models';
|
import { formatJson, sortObjectKeys } from './json.models';
|
||||||
|
|
||||||
describe('json models', () => {
|
describe('json models', () => {
|
||||||
describe('sortObjectKeys', () => {
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { type MaybeRef, get } from '@vueuse/core';
|
import { type MaybeRef, get } from '@vueuse/core';
|
||||||
import JSON5 from 'json5';
|
import JSON5 from 'json5';
|
||||||
|
import { jsonrepair } from 'jsonrepair';
|
||||||
|
|
||||||
export { sortObjectKeys, formatJson };
|
export { sortObjectKeys, formatJson };
|
||||||
|
|
||||||
@@ -31,14 +32,17 @@ function formatJson({
|
|||||||
sortKeys = true,
|
sortKeys = true,
|
||||||
indentSize = 3,
|
indentSize = 3,
|
||||||
unescapeUnicode = false,
|
unescapeUnicode = false,
|
||||||
|
repairJson = false,
|
||||||
}: {
|
}: {
|
||||||
rawJson: MaybeRef<string>
|
rawJson: MaybeRef<string>
|
||||||
sortKeys?: MaybeRef<boolean>
|
sortKeys?: MaybeRef<boolean>
|
||||||
indentSize?: MaybeRef<number>
|
indentSize?: MaybeRef<number>
|
||||||
unescapeUnicode?: MaybeRef<boolean>
|
unescapeUnicode?: MaybeRef<boolean>
|
||||||
|
repairJson?: MaybeRef<boolean>
|
||||||
}) {
|
}) {
|
||||||
const raw = get(rawJson);
|
const unwrappedJson = get(rawJson);
|
||||||
const parsedObject = JSON5.parse(get(unescapeUnicode) ? unescapeUnicodeJSON(raw) : raw);
|
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));
|
return JSON.stringify(get(sortKeys) ? sortObjectKeys(parsedObject) : parsedObject, null, get(indentSize));
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user