2024-01-27 14:36:14 +08:00
|
|
|
package convutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2024-05-17 16:25:40 +08:00
|
|
|
strutil "tinyrdm/backend/utils/string"
|
2024-01-27 14:36:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type JsonConvert struct{}
|
|
|
|
|
2024-02-20 10:55:46 +08:00
|
|
|
func (JsonConvert) Enable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-01-27 14:36:14 +08:00
|
|
|
func (JsonConvert) Decode(str string) (string, bool) {
|
|
|
|
trimedStr := strings.TrimSpace(str)
|
|
|
|
if (strings.HasPrefix(trimedStr, "{") && strings.HasSuffix(trimedStr, "}")) ||
|
|
|
|
(strings.HasPrefix(trimedStr, "[") && strings.HasSuffix(trimedStr, "]")) {
|
2024-05-17 16:25:40 +08:00
|
|
|
return strutil.JSONBeautify(trimedStr, " "), true
|
2024-01-27 14:36:14 +08:00
|
|
|
}
|
|
|
|
return str, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (JsonConvert) Encode(str string) (string, bool) {
|
2024-05-17 16:25:40 +08:00
|
|
|
return strutil.JSONMinify(str), true
|
2024-01-27 14:36:14 +08:00
|
|
|
}
|