MCPcopy
hub / github.com/wavetermdev/waveterm / repairJson

Function repairJson

pkg/util/utilfn/partial.go:59–165  ·  view source on GitHub ↗
(data []byte)

Source from the content-addressed store, hash-verified

57}
58
59func repairJson(data []byte) []byte {
60 if len(data) == 0 {
61 return data
62 }
63
64 var stack jsonStack
65 inString := false
66 escaped := false
67 lastComma := false
68
69 for i := 0; i < len(data); i++ {
70 b := data[i]
71
72 if escaped {
73 escaped = false
74 continue
75 }
76
77 if inString {
78 if b == '\\' {
79 escaped = true
80 continue
81 }
82 if b == '"' {
83 inString = false
84 }
85 continue
86 }
87
88 if b == ' ' || b == '\t' || b == '\n' || b == '\r' {
89 continue
90 }
91 valueStart := b == '{' || b == '[' || b == 'n' || b == 't' || b == 'f' || b == '"' || (b >= '0' && b <= '9') || b == '-'
92 if valueStart && lastComma {
93 lastComma = false
94 }
95 if valueStart && stack.isTop(stackKeyColon) {
96 stack.pop()
97 }
98 if valueStart && stack.isTop(stackBeforeKey) {
99 stack.replaceTop(stackKey)
100 }
101 switch b {
102 case '{':
103 stack.push(stackLBrace)
104 stack.push(stackBeforeKey)
105 case '[':
106 stack.push(stackLBrack)
107 case '}':
108 if stack.isTop(stackBeforeKey) {
109 stack.pop()
110 }
111 if stack.isTop(stackLBrace) {
112 stack.pop()
113 }
114 case ']':
115 if stack.isTop(stackLBrack) {
116 stack.pop()

Callers 2

ParsePartialJsonFunction · 0.85
TestRepairJsonFunction · 0.85

Calls 4

isTopMethod · 0.95
popMethod · 0.95
replaceTopMethod · 0.95
pushMethod · 0.95

Tested by 1

TestRepairJsonFunction · 0.68