227 lines
5.3 KiB
Go
227 lines
5.3 KiB
Go
// 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)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// MarshalForByte 序列化并返回字节数组
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 21:56 2023/7/22
|
|
func (oj *ownJSON) MarshalForByte(input interface{}) []byte {
|
|
buffer := bytes.NewBuffer([]byte{})
|
|
encoder := json.NewEncoder(buffer)
|
|
encoder.SetEscapeHTML(false)
|
|
_ = encoder.Encode(input)
|
|
return buffer.Bytes()
|
|
}
|
|
|
|
// MarshalForString 序列化并返回字符串
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 21:56 2023/7/22
|
|
func (oj *ownJSON) MarshalForString(input interface{}) string {
|
|
return string(oj.MarshalForByte(input))
|
|
}
|
|
|
|
// Transition 数据结构转换 map/slice/struct => struct | struct => map/slice/struct
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 18:25 2023/12/29
|
|
func (oj *ownJSON) Transition(input interface{}, receiver interface{}) error {
|
|
return oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
|
|
}
|
|
|
|
// TransitionIgnoreError ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 16:57 2024/1/10
|
|
func (oj *ownJSON) TransitionIgnoreError(input interface{}, receiver interface{}) {
|
|
_ = oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
|
|
}
|
|
|
|
// MergeDataForMap 合并数据到map
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 16:48 2024/1/10
|
|
func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...interface{}) (map[string]interface{}, error) {
|
|
res := make(map[string]interface{})
|
|
for _, data := range dataList {
|
|
if nil == data {
|
|
continue
|
|
}
|
|
var (
|
|
err error
|
|
itemRes map[string]interface{}
|
|
)
|
|
|
|
if err = oj.Transition(data, &itemRes); nil != err {
|
|
if ignoreError {
|
|
continue
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
for k, v := range itemRes {
|
|
res[k] = v
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// MergeDataForMapIgnoreError 多个数据合并到一个Map
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 17:00 2024/1/10
|
|
func (oj *ownJSON) MergeDataForMapIgnoreError(dataList ...interface{}) map[string]interface{} {
|
|
res, _ := oj.MergeDataForMap(true, dataList)
|
|
return res
|
|
}
|
|
|
|
// 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
|
|
case reflect.Struct:
|
|
fallthrough
|
|
case reflect.Ptr:
|
|
_ = json.Indent(&out, []byte(oj.MarshalForString(data)+"\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("")
|
|
}
|
|
}
|