gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/jsonmap/map-to-jsonmap.go (about)

     1  package jsonmap
     2  
     3  func MapToJsonMap(from, to map[string]interface{}) {
     4  	mapToJsonMap(from, to)
     5  	return
     6  }
     7  
     8  func mapToJsonMap(from, to map[string]interface{}) {
     9  	for key, val := range from {
    10  		to[key] = copyAny(val, to[key])
    11  	}
    12  	return
    13  }
    14  
    15  func copyAny(fromVal, toVal interface{}) (r interface{}) {
    16  	switch fv := fromVal.(type) {
    17  	case map[string]interface{}:
    18  		if toVal == nil {
    19  			toVal = map[string]interface{}{}
    20  		}
    21  		switch tv := toVal.(type) {
    22  		case map[string]interface{}:
    23  			mapToJsonMap(fv, tv)
    24  			r = tv
    25  		default:
    26  			return
    27  		}
    28  	case []interface{}:
    29  		// 数组直接覆盖
    30  		return fv
    31  	default:
    32  		r = InterfaceToJSONValue(fv)
    33  	}
    34  	return
    35  }