diff --git a/src/app/services/Source.ts b/src/app/services/Source.ts index 88b3a49d..c202d3e9 100644 --- a/src/app/services/Source.ts +++ b/src/app/services/Source.ts @@ -1,7 +1,7 @@ import { NbtTag } from 'deepslate' import yaml from 'js-yaml' import { Store } from '../Store.js' -import { jsonToNbt, safeJsonParse } from '../Utils.js' +import { jsonToNbt, message, safeJsonParse } from '../Utils.js' const INDENTS: Record = { '2_spaces': 2, @@ -16,7 +16,15 @@ const FORMATS: Record = { json: { parse: (s) => s, - stringify: (s) => s, + stringify: (s, i) => { + try { + const data = JSON.parse(s) + return JSON.stringify(data, null, i) + } catch (e) { + console.warn(`Failed to format JSON output. Falling back to source. ${message(e)}`) + return s + } + }, }, snbt: { parse: (s) => JSON.stringify(NbtTag.fromString(s).toSimplifiedJson(), null, 2),