增加JSON操作以及map操作

This commit is contained in:
白茶清欢 2023-07-22 21:59:15 +08:00
parent 7ed8ccbd87
commit 4a2cf51a9c
2 changed files with 42 additions and 0 deletions

29
json.go
View File

@ -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<白茶清欢>

13
map.go
View File

@ -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)
}