12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package utils
- import (
- "encoding/json"
- "strconv"
- "strings"
- "zhiyuan/pkg/logger"
- )
- type JsonDecode string
- func (val JsonDecode) To(ret interface{}) {
- GetJsonDecode(val).Decode(&ret)
- }
- func (val JsonDecode) ToMap() map[string]interface{} {
- dest := make(map[string]interface{})
- GetJsonDecode(val).Decode(&dest)
- return dest
- }
- func (val JsonDecode) ToString() string {
- s, _ := strconv.Unquote(string(val))
- return s
- }
- func (val JsonDecode) ToMapSlice() []map[string]interface{} {
- dest := make([]map[string]interface{}, 0)
- GetJsonDecode(val).Decode(&dest)
- return dest
- }
- func (val JsonDecode) ToStrSlice() []string {
- dest := make([]string, 0)
- GetJsonDecode(val).Decode(&dest)
- return dest
- }
- func (val JsonDecode) ToIntSlice() []int {
- dest := make([]int, 0)
- GetJsonDecode(val).Decode(&dest)
- return dest
- }
- func (val JsonDecode) ToMapStr() string {
- var dest string
- GetJsonDecode(val).Decode(&dest)
- return dest
- }
- func GetJsonDecode(val JsonDecode) *json.Decoder {
- decoder := json.NewDecoder(strings.NewReader(string(val)))
- decoder.UseNumber()
- return decoder
- }
- func JsonEncode(data interface{}) string {
- jsonData, err := json.Marshal(data)
- if err != nil {
- logger.Sugar.Error(err)
- return ""
- }
- return string(jsonData)
- }
|