mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-20 14:56:17 -04:00
refactor(json-prettifier): more permissive json parser
This commit is contained in:
parent
d30cd8a9ab
commit
8089c60000
1 changed files with 5 additions and 9 deletions
|
@ -24,25 +24,21 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
||||||
import { useValidation } from '@/composable/validation';
|
import { useValidation } from '@/composable/validation';
|
||||||
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
|
import JSON5 from 'json5';
|
||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
const inputElement = ref<HTMLElement>();
|
const inputElement = ref<HTMLElement>();
|
||||||
|
|
||||||
const rawJson = ref('{"hello": "world"}');
|
const rawJson = ref('{"hello": "world"}');
|
||||||
const cleanJson = computed(() => {
|
const cleanJson = computed(() => withDefaultOnError(() => JSON.stringify(JSON5.parse(rawJson.value), null, 3), ''));
|
||||||
try {
|
|
||||||
return JSON.stringify(JSON.parse(rawJson.value), null, 3);
|
|
||||||
} catch (_) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const rawJsonValidation = useValidation({
|
const rawJsonValidation = useValidation({
|
||||||
source: rawJson,
|
source: rawJson,
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
validator: (v) => v === '' || JSON.parse(v),
|
validator: (v) => v === '' || JSON5.parse(v),
|
||||||
message: 'Invalid json',
|
message: 'Provided JSON is not valid.',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue