Skip to content

Commit

Permalink
- Optimized the validation of the result type in the jsonUtil packa…
Browse files Browse the repository at this point in the history
…ge (#9)

- Fixed the validation error of passing an empty string for the data type in `JsonToStruct`
- 优化了 `jsonUtil` 包中result类型的验证
- 修复了`JsonToStruct`中数据类型传入空字符串的校验错误
  • Loading branch information
jefferyjob authored Mar 31, 2023
1 parent cea73b8 commit db0ed90
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 165 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
- 优化了 `anyUtil` 包中关于指针类型的判断
- 优化了 `anyUtil``jsonUtil` 文档
- Fix:删除了 `AnyToInt` 方法中的debug代码 #5
- Fix:修复了身份证号验证存在的问题 #7
- Fix:修复了身份证号验证存在的问题 #7

## v1.0.2
- 优化了 `jsonUtil` 包中result类型的验证
- 修复了`JsonToStruct`中数据类型传入空字符串的校验错误
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
- Optimized the judgment of pointer type in `anyUti`l package
- Optimized the documentation of `anyUtil` and `jsonUtil`
- Fix: Removed debug code from the `AnyToInt` method #5
- Fix: Problems in ID number verification #7
- Fix: Problems in ID number verification #7

## v1.0.2
- Optimized the validation of the result type in the `jsonUtil` package
- Fixed the validation error of passing an empty string for the data type in `JsonToStruct`
83 changes: 50 additions & 33 deletions jsonUtil/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@

<details>
<summary>English</summary>
<pre>
JsonToStruct is a method that parses a JSON string into a struct. This method accepts two parameters:
- jsonData: the JSON string to be parsed
- result: a pointer to an instance of a struct used to store the parsed data
The method uses reflection to parse the struct and extracts the corresponding values from the JSON based on the fields defined in the struct and their corresponding JSON tags. If a field in the struct is a nested struct, it recursively parses the nested struct and stores the result in the parent struct.
This method can handle basic data types such as strings, integers, floating-point numbers, booleans, as well as nested structs and slices. If a value in the JSON string cannot be converted to the target type, the method will return an error.
</pre>
JsonToStruct is a method that parses a JSON string into a struct. This method accepts two parameters:

- jsonData: the JSON string to be parsed
- result: a pointer to an instance of a struct used to store the parsed data

The method uses reflection to parse the struct and extracts the corresponding values from the JSON based on the fields defined in the struct and their corresponding JSON tags. If a field in the struct is a nested struct, it recursively parses the nested struct and stores the result in the parent struct.

This method can handle basic data types such as strings, integers, floating-point numbers, booleans, as well as nested structs and slices. If a value in the JSON string cannot be converted to the target type, the method will return an error.
</details>

<details>
<summary>简体中文</summary>
<pre>
JsonToStruct 是一个将JSON字符串解析为结构体的方法。这个方法接受两个参数:
- jsonData:待解析的JSON字符串
- result:用于存储解析后数据的结构体实例的指针
该方法使用了反射机制来解析结构体,并根据结构体中定义的字段和对应的json标签从JSON中提取对应的值。如果结构体的字段是一个嵌套的结构体,它将递归解析嵌套的结构体,并将结果存储在父结构体中。
该方法可以处理基本数据类型,如字符串、整数、浮点数、布尔值以及嵌套的结构体和切片。如果JSON字符串中的值无法转换为目标类型,该方法将返回一个错误。
</pre>
JsonToStruct 是一个将JSON字符串解析为结构体的方法。这个方法接受两个参数:

- jsonData:待解析的JSON字符串
- result:用于存储解析后数据的结构体实例的指针

该方法使用了反射机制来解析结构体,并根据结构体中定义的字段和对应的json标签从JSON中提取对应的值。如果结构体的字段是一个嵌套的结构体,它将递归解析嵌套的结构体,并将结果存储在父结构体中。

该方法可以处理基本数据类型,如字符串、整数、浮点数、布尔值以及嵌套的结构体和切片。如果JSON字符串中的值无法转换为目标类型,该方法将返回一个错误。
</details>


Expand All @@ -46,24 +48,39 @@ func JsonToStruct(jsonData string, result interface{}) error
// age is defined as int, and the value of json is string
// is_use is defined as bool, the value of json is int
func TestDemo1(t *testing.T) {
jsonData := `{
"name": "make",
"age": "22",
"is_use": "1"
}`
type People struct {
Name string `json:"name,omitempty"`
Age int `json:"age"`
IsUse bool `json:"is_use"`
}
var people People
if err := JsonToStruct(jsonData, &people); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v \n", people)
// return
// {Name:make Age:22 IsUse:true}
jsonData := `{
"name": "make",
"age": "22",
"is_use": "1"
}`

var people1 struct {
Name string `json:"name,omitempty"`
Age int `json:"age"`
IsUse bool `json:"is_use"`
}

var people2 struct {
Name string `json:"name,omitempty"`
Age string `json:"age"`
IsUse bool `json:"is_use"`
}

if err := JsonToStruct(jsonData, &people1); err != nil {
fmt.Println(err)
return
}
fmt.Printf("people1:%#v \n", people1)
// return
// people1:struct { Name string "json:\"name,omitempty\""; Age int "json:\"age\""; IsUse bool "json:\"is_use\"" }{Name:"make", Age:22, IsUse:true}

if err := JsonToStruct(jsonData, &people2); err != nil {
fmt.Println(err)
return
}
fmt.Printf("people2:%#v \n", people2)
// return
// people2:struct { Name string "json:\"name,omitempty\""; Age string "json:\"age\""; IsUse bool "json:\"is_use\"" }{Name:"make", Age:"22", IsUse:true}
}
```

Expand Down Expand Up @@ -100,4 +117,4 @@ func TestJsonToStructDemo2(t *testing.T) {
fmt.Printf("%+v \n", person)
// {Name:Bob Age:25 Address:{City:Shanghai Country:China} Interests:[reading swimming]}
}
```
```
10 changes: 5 additions & 5 deletions jsonUtil/json_to_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsonUtil

import (
"encoding/json"
"errors"
"log"
"reflect"
"strings"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit db0ed90

Please sign in to comment.