diff options
author | 2022-08-04 22:18:15 +0200 | |
---|---|---|
committer | 2022-08-04 22:18:15 +0200 | |
commit | 8089c60000000c42c821c6586c128d3d2b248885 (patch) | |
tree | 56a8590b679a9262cd8d59a1aee274656d40b9d6 | |
parent | d30cd8a9abc3298c0a0b05f249e54318bb4537f2 (diff) | |
download | it-tools-8089c60000000c42c821c6586c128d3d2b248885.tar.gz it-tools-8089c60000000c42c821c6586c128d3d2b248885.tar.zst it-tools-8089c60000000c42c821c6586c128d3d2b248885.zip |
refactor(json-prettifier): more permissive json parser
-rw-r--r-- | src/tools/json-viewer/json-viewer.vue | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/src/tools/json-viewer/json-viewer.vue b/src/tools/json-viewer/json-viewer.vue index 00af91d..a460643 100644 --- a/src/tools/json-viewer/json-viewer.vue +++ b/src/tools/json-viewer/json-viewer.vue @@ -24,25 +24,21 @@ <script setup lang="ts"> import TextareaCopyable from '@/components/TextareaCopyable.vue'; import { useValidation } from '@/composable/validation'; +import { withDefaultOnError } from '@/utils/defaults'; +import JSON5 from 'json5'; import { computed, ref } from 'vue'; const inputElement = ref<HTMLElement>(); const rawJson = ref('{"hello": "world"}'); -const cleanJson = computed(() => { - try { - return JSON.stringify(JSON.parse(rawJson.value), null, 3); - } catch (_) { - return ''; - } -}); +const cleanJson = computed(() => withDefaultOnError(() => JSON.stringify(JSON5.parse(rawJson.value), null, 3), '')); const rawJsonValidation = useValidation({ source: rawJson, rules: [ { - validator: (v) => v === '' || JSON.parse(v), - message: 'Invalid json', + validator: (v) => v === '' || JSON5.parse(v), + message: 'Provided JSON is not valid.', }, ], }); |