mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-29 23:10:35 +00:00
46 lines
755 B
Go
46 lines
755 B
Go
package common
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
)
|
|
|
|
func Unmarshal(data []byte, v any) error {
|
|
return json.Unmarshal(data, v)
|
|
}
|
|
|
|
func UnmarshalJsonStr(data string, v any) error {
|
|
return json.Unmarshal(StringToByteSlice(data), v)
|
|
}
|
|
|
|
func DecodeJson(reader io.Reader, v any) error {
|
|
return json.NewDecoder(reader).Decode(v)
|
|
}
|
|
|
|
func Marshal(v any) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func GetJsonType(data json.RawMessage) string {
|
|
trimmed := bytes.TrimSpace(data)
|
|
if len(trimmed) == 0 {
|
|
return "unknown"
|
|
}
|
|
firstChar := trimmed[0]
|
|
switch firstChar {
|
|
case '{':
|
|
return "object"
|
|
case '[':
|
|
return "array"
|
|
case '"':
|
|
return "string"
|
|
case 't', 'f':
|
|
return "boolean"
|
|
case 'n':
|
|
return "null"
|
|
default:
|
|
return "number"
|
|
}
|
|
}
|