util/json.go

129 lines
2.8 KiB
Go
Raw Normal View History

2022-05-14 13:45:51 +08:00
// Package util...
//
// Description : json 工具函数
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-09-14 8:38 下午
package util
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
)
2022-05-14 15:05:20 +08:00
// ownJSON ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:01 2022/5/14
type ownJSON struct {
}
// UnmarshalWithNumber 解析json
2022-05-14 13:45:51 +08:00
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:39 下午 2021/9/14
2022-05-14 15:05:20 +08:00
func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver interface{}) error {
2022-05-14 13:45:51 +08:00
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
}
2022-05-14 15:05:20 +08:00
// UnmarshalWithNumberForIOReader ...
2022-05-14 13:45:51 +08:00
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:43 下午 2021/9/14
2022-05-14 15:05:20 +08:00
func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver interface{}) error {
2022-05-14 13:45:51 +08:00
decoder := json.NewDecoder(ioReader)
decoder.UseNumber()
return decoder.Decode(receiver)
}
2023-07-22 21:59:15 +08:00
// UnmarshalWithNumberForString 字符串转结构体
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:50 2023/7/22
func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver interface{}) error {
return oj.UnmarshalWithNumber([]byte(input), receiver)
}
// MarshalForByte 序列化并返回字节数组
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:56 2023/7/22
func (oj *ownJSON) MarshalForByte(input interface{}) []byte {
byteData, _ := json.Marshal(input)
return byteData
}
// MarshalForString 序列化并返回字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:56 2023/7/22
func (oj *ownJSON) MarshalForString(input interface{}) string {
byteData := oj.MarshalForByte(input)
return string(byteData)
}
2022-05-14 15:05:20 +08:00
// ConsoleOutput ...
2022-05-14 13:45:51 +08:00
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:45 下午 2021/11/5
2022-05-14 15:05:20 +08:00
func (oj *ownJSON) ConsoleOutput(data interface{}) {
2022-05-14 13:45:51 +08:00
var out bytes.Buffer
switch reflect.TypeOf(data).Kind() {
case reflect.Slice:
fallthrough
case reflect.Array:
fallthrough
case reflect.Map:
fallthrough
case reflect.Ptr:
byteData, _ := json.Marshal(data)
_ = json.Indent(&out, []byte(string(byteData)+"\n"), "", "\t")
_, _ = out.WriteTo(os.Stdout)
return
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
fallthrough
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
fallthrough
case reflect.Float32:
fallthrough
case reflect.Float64:
fallthrough
case reflect.String:
_ = json.Indent(&out, []byte(fmt.Sprintf("%v\n", data)), "", "\t")
_, _ = out.WriteTo(os.Stdout)
return
default:
fmt.Println("")
}
}