Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encodeStruct 应该读取json tag #51

Open
KevinGong2013 opened this issue May 27, 2022 · 2 comments
Open

encodeStruct 应该读取json tag #51

KevinGong2013 opened this issue May 27, 2022 · 2 comments

Comments

@KevinGong2013
Copy link

func encodeStruct(object interface{}, ignoreZero bool) map[string]interface{} {
v := reflect.Indirect(reflect.ValueOf(object))
t := v.Type()
if v.IsValid() && v.Kind() == reflect.Struct {
encodedMap := make(map[string]interface{})
for i := 0; i < v.NumField(); i++ {
encodedMap[t.Field(i).Name] = encode(v.Field(i).Interface(), ignoreZero)
if encodedMap[t.Field(i).Name] == nil {
delete(encodedMap, t.Field(i).Name)
}
}
return encodedMap
}
return nil
}

例如自定一个class 如下:

type Foo struct {
	leancloud.Object
	Rule               struct {
		Month     int `json:"month"`
		Day       int `json:"day"`
	} `json:"rule"`
}

这个Foo对象save以后 rule列应该是 {"month": 1, "day": 1} 而不是{"Month": 1, "Day": 1}

@bigzhu
Copy link

bigzhu commented Jun 20, 2022

我也遇到一样的问题, 某字段是一个 Array, Array 里放了一堆 struct 对象.

存入库中后发现, 这些 struct 对象都没有读取其 json tag, 而是用了大写开头的字段名.

@bigzhu
Copy link

bigzhu commented Jun 20, 2022

提了一个 pull request #52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants