- 美团外卖满减,直降,运费活动API完成
This commit is contained in:
@@ -481,6 +481,10 @@ func Struct2MapWithIgnore(obj interface{}, ignoreValues map[string]interface{})
|
||||
return mapData
|
||||
}
|
||||
|
||||
// 注意:如下两个函数的行为与标准的json.Marshal的行为是有区别的
|
||||
// 这两个函数,只要成员是对象或map(不是指针),都会被展开
|
||||
// 而json.Marshal不是这样的,只会展开内嵌的(包括指针)
|
||||
// 所以原则是不用map的对象,行为就比较一致
|
||||
func MarshalJSONIgnoreValues(obj interface{}, ignoreValues map[string]interface{}) ([]byte, error) {
|
||||
return json.Marshal(Struct2MapWithIgnore(obj, ignoreValues))
|
||||
}
|
||||
@@ -488,3 +492,12 @@ func MarshalJSONIgnoreValues(obj interface{}, ignoreValues map[string]interface{
|
||||
func MustMarshalJSONIgnoreValues(obj interface{}, ignoreValues map[string]interface{}) []byte {
|
||||
return MustMarshal(Struct2MapWithIgnore(obj, ignoreValues))
|
||||
}
|
||||
|
||||
func StructList2MapListWithIgnore(obj interface{}, ignoreValues map[string]interface{}) (mapList []map[string]interface{}) {
|
||||
objList := Interface2Slice(obj)
|
||||
mapList = make([]map[string]interface{}, len(objList))
|
||||
for k, v := range objList {
|
||||
mapList[k] = Struct2MapWithIgnore(v, ignoreValues)
|
||||
}
|
||||
return mapList
|
||||
}
|
||||
|
||||
@@ -30,3 +30,42 @@ func TestConv(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
type InnerStruct struct {
|
||||
InnerIntData int
|
||||
}
|
||||
type OutStruct struct {
|
||||
InnerStruct
|
||||
IntData int
|
||||
StrData string
|
||||
InnerData InnerStruct
|
||||
PtrInnerData *InnerStruct
|
||||
}
|
||||
type OutStruct2 struct {
|
||||
*InnerStruct
|
||||
IntData int
|
||||
StrData string
|
||||
}
|
||||
|
||||
obj := OutStruct{
|
||||
InnerStruct: InnerStruct{
|
||||
InnerIntData: 3,
|
||||
},
|
||||
IntData: 1,
|
||||
StrData: "2",
|
||||
InnerData: InnerStruct{
|
||||
InnerIntData: 4,
|
||||
},
|
||||
}
|
||||
t.Log(Format4Output(obj, false))
|
||||
t.Log(Format4Output(Struct2FlatMap(obj), false))
|
||||
obj2 := OutStruct2{
|
||||
InnerStruct: &InnerStruct{
|
||||
InnerIntData: 2,
|
||||
},
|
||||
}
|
||||
|
||||
t.Log(Format4Output(obj2, false))
|
||||
t.Log(Format4Output(Struct2FlatMap(obj2), false))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user