Files
public/tools/json.go
2025-04-22 21:58:27 +08:00

80 lines
1.6 KiB
Go

package tools
import (
// "encoding/json"
"io/ioutil"
"net/http"
"github.com/bytedance/sonic"
)
// JSONToForm tag json str to form
func JSONToForm(r *http.Request) {
//添加支持json 操作
r.ParseForm()
if len(r.Form) == 1 { //可能是json 支持json
for key, value := range r.Form {
if len(value[0]) == 0 {
delete(r.Form, key)
var m map[string]string
if err := sonic.Unmarshal([]byte(key), &m); err == nil {
for k, v := range m {
r.Form[k] = []string{v}
}
}
}
}
}
body, _ := ioutil.ReadAll(r.Body)
bodyStr := string(body)
if len(bodyStr) > 0 {
var m map[string]string
if err := sonic.Unmarshal(body, &m); err == nil {
for k, v := range m {
r.Form[k] = []string{v}
}
}
}
//-----------------------------end
return
}
// "github.com/ant0ine/go-json-rest/rest"
// func GetRequestJsonObj(r *rest.Request, v interface{}) error {
// //添加支持json 操作
// body, err := ioutil.ReadAll(r.Body)
// r.Body.Close()
// json.Unmarshal(body, &v)
// //-----------------------------end
// return err
// }
// GetJSONStr obj to json string
func GetJSONStr(obj interface{}, isFormat bool) string {
var b []byte
if isFormat {
b, _ = sonic.MarshalIndent(obj, "", " ")
} else {
b, _ = sonic.Marshal(obj)
}
return string(b)
}
// JSONDecode Json Decode
func JSONDecode(obj interface{}) string {
return GetJSONStr(obj, false)
}
// GetJSONObj string convert to obj
func GetJSONObj(str string, out interface{}) error {
return sonic.Unmarshal([]byte(str), out)
}
// JSONEncode string convert to obj
func JSONEncode(str string, out interface{}) error {
return GetJSONObj(str, out)
}