2023-08-11 10:54:40 +08:00
|
|
|
// Package serialize ...
|
|
|
|
//
|
|
|
|
// Description : json 工具函数
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-09-14 8:38 下午
|
|
|
|
package serialize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// JSON ...
|
|
|
|
JSON *ownJSON
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
JSON = &ownJSON{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ownJSON ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:01 2022/5/14
|
|
|
|
type ownJSON struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalWithNumber 解析json
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 8:39 下午 2021/9/14
|
|
|
|
func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver interface{}) error {
|
|
|
|
decoder := json.NewDecoder(bytes.NewReader(byteData))
|
|
|
|
decoder.UseNumber()
|
|
|
|
return decoder.Decode(receiver)
|
|
|
|
}
|
|
|
|
|
2023-12-24 20:54:39 +08:00
|
|
|
// UnmarshalWithNumberIgnoreError 反序列化且忽略error
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:46 2023/12/24
|
|
|
|
func (oj *ownJSON) UnmarshalWithNumberIgnoreError(byteData []byte, receiver interface{}) {
|
|
|
|
_ = oj.UnmarshalWithNumber(byteData, receiver)
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:54:40 +08:00
|
|
|
// UnmarshalWithNumberForIOReader ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 8:43 下午 2021/9/14
|
|
|
|
func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver interface{}) error {
|
|
|
|
decoder := json.NewDecoder(ioReader)
|
|
|
|
decoder.UseNumber()
|
|
|
|
return decoder.Decode(receiver)
|
|
|
|
}
|
|
|
|
|
2023-12-24 20:54:39 +08:00
|
|
|
// UnmarshalWithNumberForIOReaderIgnoreError 反序列化忽略Error
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:47 2023/12/24
|
|
|
|
func (oj *ownJSON) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver interface{}) {
|
|
|
|
_ = oj.UnmarshalWithNumberForIOReader(ioReader, receiver)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:54:40 +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)
|
|
|
|
}
|
|
|
|
|
2023-12-24 20:54:39 +08:00
|
|
|
// UnmarshalWithNumberForStringIgnoreError 反序列化并且忽略error
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:48 2023/12/24
|
|
|
|
func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receiver interface{}) {
|
|
|
|
oj.UnmarshalWithNumberIgnoreError([]byte(input), receiver)
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:54:40 +08:00
|
|
|
// MarshalForByte 序列化并返回字节数组
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:56 2023/7/22
|
|
|
|
func (oj *ownJSON) MarshalForByte(input interface{}) []byte {
|
2023-09-01 18:21:38 +08:00
|
|
|
buffer := bytes.NewBuffer([]byte{})
|
2023-09-01 18:32:03 +08:00
|
|
|
encoder := json.NewEncoder(buffer)
|
2023-12-01 18:47:03 +08:00
|
|
|
encoder.SetEscapeHTML(false)
|
2023-09-01 18:32:03 +08:00
|
|
|
_ = encoder.Encode(input)
|
2023-09-01 18:21:38 +08:00
|
|
|
return buffer.Bytes()
|
2023-08-11 10:54:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalForString 序列化并返回字符串
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:56 2023/7/22
|
|
|
|
func (oj *ownJSON) MarshalForString(input interface{}) string {
|
2023-09-01 18:32:03 +08:00
|
|
|
return string(oj.MarshalForByte(input))
|
2023-08-11 10:54:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConsoleOutput ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:45 下午 2021/11/5
|
|
|
|
func (oj *ownJSON) ConsoleOutput(data interface{}) {
|
|
|
|
var out bytes.Buffer
|
|
|
|
switch reflect.TypeOf(data).Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Array:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Map:
|
|
|
|
fallthrough
|
2023-09-09 14:51:33 +08:00
|
|
|
case reflect.Struct:
|
|
|
|
fallthrough
|
2023-08-11 10:54:40 +08:00
|
|
|
case reflect.Ptr:
|
2023-09-09 14:51:33 +08:00
|
|
|
_ = json.Indent(&out, []byte(oj.MarshalForString(data)+"\n"), "", "\t")
|
2023-08-11 10:54:40 +08:00
|
|
|
_, _ = 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("")
|
|
|
|
}
|
|
|
|
}
|