- Optimized the validation of the result type in the jsonUtil package (#9)

- Fixed the validation error of passing an empty string for the data type in `JsonToStruct`
- 优化了 `jsonUtil` 包中result类型的验证
- 修复了`JsonToStruct`中数据类型传入空字符串的校验错误
This commit is contained in:
jeffery
2023-03-31 17:23:54 +08:00
committed by GitHub
parent cea73b8e18
commit db0ed9068b
10 changed files with 363 additions and 165 deletions

View File

@@ -2,6 +2,7 @@ package jsonUtil
import (
"encoding/json"
"errors"
"log"
"reflect"
"strings"
@@ -10,6 +11,10 @@ import (
// JsonToStruct Parses JSON into a specified structure pointer
// 将JSON解析为指定的结构体指针
func JsonToStruct(jsonData string, result interface{}) error {
if reflect.ValueOf(result).Kind() != reflect.Pointer || reflect.ValueOf(result).IsNil() {
return errors.New("the argument to Result must be a non-nil pointer")
}
var data map[string]interface{}
err := json.Unmarshal([]byte(jsonData), &data)
if err != nil {
@@ -104,11 +109,6 @@ func JsonToStruct(jsonData string, result interface{}) error {
return nil
}
//func isJSON(jsonStr string) bool {
// var raw json.RawMessage
// return json.Unmarshal([]byte(jsonStr), &raw) == nil
//}
func convertToJSONString(data map[string]interface{}) string {
jsonBytes, _ := json.Marshal(data)
return string(jsonBytes)