forked from FujiNetWIFI/servers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
52 lines (40 loc) · 1.38 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/goccy/go-json"
)
// Serializes the results, either as json (default), or raw (close to FujiNet json parsing result)
// raw=1 - or as key[char 0]value[char 0] pairs
// - fc=U/L - (may use with raw) force data case all upper or lower
func serializeResults(c *gin.Context, obj any) {
if c.Query("raw") == "1" {
lineDelimiter := "\u0000"
if c.Query("lf") == "1" {
lineDelimiter = "\n"
}
jsonBytes, _ := json.Marshal(obj)
jsonResult := string(jsonBytes)
// Strip out [,],{,}
jsonResult = strings.ReplaceAll(jsonResult, "{", "")
jsonResult = strings.ReplaceAll(jsonResult, "}", "")
jsonResult = strings.ReplaceAll(jsonResult, "[", "")
jsonResult = strings.ReplaceAll(jsonResult, "]", "")
// Convert : to new line
jsonResult = strings.ReplaceAll(jsonResult, ":", lineDelimiter)
// Convert commas to new line
jsonResult = strings.ReplaceAll(jsonResult, "\",", lineDelimiter)
jsonResult = strings.ReplaceAll(jsonResult, ",\"", lineDelimiter)
jsonResult = strings.ReplaceAll(jsonResult, "\"", "")
if c.Query("uc") == "1" {
jsonResult = strings.ToUpper(jsonResult)
}
if c.Query("lc") == "1" {
jsonResult = strings.ToLower(jsonResult)
}
c.String(http.StatusOK, jsonResult)
} else {
c.JSON(http.StatusOK, obj)
}
}