// 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 any) 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 any) { _ = oj.UnmarshalWithNumber(byteData, receiver) } // UnmarshalWithNumberForIOReader ... // // Author : go_developer@163.com<白茶清欢> // // Date : 8:43 下午 2021/9/14 func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) 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 any) { _ = oj.UnmarshalWithNumberForIOReader(ioReader, receiver) return } // UnmarshalWithNumberForString 字符串转结构体 // // Author : go_developer@163.com<白茶清欢> // // Date : 21:50 2023/7/22 func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver any) 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 any) { oj.UnmarshalWithNumberIgnoreError([]byte(input), receiver) } // MarshalForByte 序列化并返回字节数组 // // Author : go_developer@163.com<白茶清欢> // // Date : 21:56 2023/7/22 func (oj *ownJSON) MarshalForByte(input any) ([]byte, error) { buffer := bytes.NewBuffer([]byte{}) encoder := json.NewEncoder(buffer) encoder.SetEscapeHTML(false) if err := encoder.Encode(input); nil != err { return nil, err } return buffer.Bytes(), nil } // MarshalForByteIgnoreError 序列化并忽略异常 // // Author : go_developer@163.com<白茶清欢> // // Date : 17:16 2024/10/23 func (oj *ownJSON) MarshalForByteIgnoreError(input any) []byte { byteData, _ := oj.MarshalForByte(input) return byteData } // MarshalForString 序列化并返回字符串 // // Author : go_developer@163.com<白茶清欢> // // Date : 21:56 2023/7/22 func (oj *ownJSON) MarshalForString(input any) (string, error) { byteData, err := oj.MarshalForByte(input) if nil != err { return "", err } return string(byteData), nil } // MarshalForStringIgnoreError ... // // Author : go_developer@163.com<白茶清欢> // // Date : 17:19 2024/10/23 func (oj *ownJSON) MarshalForStringIgnoreError(input any) string { return string(oj.MarshalForByteIgnoreError(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 any, receiver any) error { return oj.UnmarshalWithNumber(oj.MarshalForByteIgnoreError(input), receiver) } // TransitionIgnoreError ... // // Author : go_developer@163.com<白茶清欢> // // Date : 16:57 2024/1/10 func (oj *ownJSON) TransitionIgnoreError(input any, receiver any) { _ = oj.UnmarshalWithNumber(oj.MarshalForByteIgnoreError(input), receiver) } // MergeDataForMap 合并数据到map // // Author : go_developer@163.com<白茶清欢> // // Date : 16:48 2024/1/10 func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...any) (map[string]any, error) { res := make(map[string]any) for _, data := range dataList { if nil == data { continue } var ( err error itemRes map[string]any ) 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 ...any) map[string]any { res, _ := oj.MergeDataForMap(true, dataList) return res } // MergeDataForReceiver 合并数据并转换 // // Author : go_developer@163.com<白茶清欢> // // Date : 17:07 2024/1/10 func (oj *ownJSON) MergeDataForReceiver(receiver any, dataList ...any) error { res, err := oj.MergeDataForMap(false, dataList...) if nil != err { return err } if err = oj.Transition(res, receiver); nil != err { return err } return nil } // ConsoleOutput ... // // Author : go_developer@163.com<白茶清欢> // // Date : 5:45 下午 2021/11/5 func (oj *ownJSON) ConsoleOutput(data any) { 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.MarshalForStringIgnoreError(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("") } }