From 4a2cf51a9c7aedcd09510d1f17e0a6728c6ac7b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 22 Jul 2023 21:59:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0JSON=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E4=BB=A5=E5=8F=8Amap=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json.go | 29 +++++++++++++++++++++++++++++ map.go | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/json.go b/json.go index c360d40..26e8fdb 100644 --- a/json.go +++ b/json.go @@ -46,6 +46,35 @@ func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiv return decoder.Decode(receiver) } +// 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) +} + // ConsoleOutput ... // // Author : go_developer@163.com<白茶清欢> diff --git a/map.go b/map.go index 0c65279..d60d5ed 100644 --- a/map.go +++ b/map.go @@ -214,3 +214,16 @@ func (om *ownMap) GetDataWithReceiver(source map[interface{}]interface{}, key in } return ConvertAssign(receiver, source[key]) } + +// ToStruct map转结构体 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:48 2023/7/22 +func (om *ownMap) ToStruct(source map[string]interface{}, receiver interface{}) error { + if nil == source { + return nil + } + byteData := JSON.MarshalForByte(source) + return JSON.UnmarshalWithNumber(byteData, receiver) +}