Skip to content

Commit 1860f86

Browse files
committed
Fix #641 format JSON output
1 parent 7ceb74f commit 1860f86

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/app/services/Source.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NbtTag } from 'deepslate'
22
import yaml from 'js-yaml'
33
import { Store } from '../Store.js'
4-
import { jsonToNbt, safeJsonParse } from '../Utils.js'
4+
import { jsonToNbt, message, safeJsonParse } from '../Utils.js'
55

66
const INDENTS: Record<string, number | string | undefined> = {
77
'2_spaces': 2,
@@ -16,7 +16,15 @@ const FORMATS: Record<string, {
1616
}> = {
1717
json: {
1818
parse: (s) => s,
19-
stringify: (s) => s,
19+
stringify: (s, i) => {
20+
try {
21+
const data = JSON.parse(s)
22+
return JSON.stringify(data, null, i)
23+
} catch (e) {
24+
console.warn(`Failed to format JSON output. Falling back to source. ${message(e)}`)
25+
return s
26+
}
27+
},
2028
},
2129
snbt: {
2230
parse: (s) => JSON.stringify(NbtTag.fromString(s).toSimplifiedJson(), null, 2),

0 commit comments

Comments
 (0)