Skip to content

Commit 30cc371

Browse files
committed
support comments for JSON files
1 parent dd7706b commit 30cc371

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

cmd/app/app.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (a *App) loadIPList(path string) error {
158158
return err
159159
}
160160

161-
return a.List.IP.LoadFromJSON(file)
161+
return a.List.IP.LoadFromJSON(utils.StandardJSON(file, file))
162162
}
163163

164164
func (a *App) loadMapList(path string) error {
@@ -167,7 +167,7 @@ func (a *App) loadMapList(path string) error {
167167
return err
168168
}
169169

170-
return a.List.Map.LoadFromJSON(file)
170+
return a.List.Map.LoadFromJSON(utils.StandardJSON(file, file))
171171
}
172172

173173
func (a *App) loadSystemAdminList(path string) error {
@@ -176,7 +176,7 @@ func (a *App) loadSystemAdminList(path string) error {
176176
return err
177177
}
178178

179-
return a.List.SystemAdmin.LoadFromJSON(file)
179+
return a.List.SystemAdmin.LoadFromJSON(utils.StandardJSON(file, file))
180180
}
181181

182182
func (a *App) Start(mux chi.Router) error {

pkg/utils/utils.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,78 @@ func Steam64To32(steamID int64) (steam32 string) {
3434

3535
steam32 = "STEAM_0-" + strconv.FormatInt(remainder, 10) + "-" + strconv.FormatInt(steamID, 10)
3636
return
37+
}
38+
39+
// Credit to https://github.com/tidwall/jsonc - MIT License https://github.com/tidwall/jsonc/blob/master/LICENSE
40+
func StandardJSON(src, dst []byte) []byte {
41+
dst = dst[:0]
42+
43+
for i := 0; i < len(src); i++ {
44+
if src[i] == '/' {
45+
if i < len(src)-1 {
46+
if src[i+1] == '/' {
47+
dst = append(dst, ' ', ' ')
48+
i += 2
49+
for ; i < len(src); i++ {
50+
if src[i] == '\n' {
51+
dst = append(dst, '\n')
52+
break
53+
} else if src[i] == '\t' || src[i] == '\r' {
54+
dst = append(dst, src[i])
55+
} else {
56+
dst = append(dst, ' ')
57+
}
58+
}
59+
continue
60+
}
61+
if src[i+1] == '*' {
62+
dst = append(dst, ' ', ' ')
63+
i += 2
64+
for ; i < len(src)-1; i++ {
65+
if src[i] == '*' && src[i+1] == '/' {
66+
dst = append(dst, ' ', ' ')
67+
i++
68+
break
69+
} else if src[i] == '\n' || src[i] == '\t' ||
70+
src[i] == '\r' {
71+
dst = append(dst, src[i])
72+
} else {
73+
dst = append(dst, ' ')
74+
}
75+
}
76+
continue
77+
}
78+
}
79+
}
80+
81+
dst = append(dst, src[i])
82+
if src[i] == '"' {
83+
for i = i + 1; i < len(src); i++ {
84+
dst = append(dst, src[i])
85+
if src[i] == '"' {
86+
j := i - 1
87+
for ; ; j-- {
88+
if src[j] != '\\' {
89+
break
90+
}
91+
}
92+
if (j-i)%2 != 0 {
93+
break
94+
}
95+
}
96+
}
97+
} else if src[i] == '}' || src[i] == ']' {
98+
for j := len(dst) - 2; j >= 0; j-- {
99+
if dst[j] <= ' ' {
100+
continue
101+
}
102+
if dst[j] == ',' {
103+
dst[j] = ' '
104+
}
105+
break
106+
}
107+
}
108+
}
109+
110+
return dst
37111
}

0 commit comments

Comments
 (0)