Skip nil values when marshaling.
This commit is contained in:
parent
b1f8c9d7a7
commit
f81cd34e34
1 changed files with 16 additions and 0 deletions
16
struct.go
16
struct.go
|
@ -355,6 +355,9 @@ func writeSVList(w io.Writer, svList StringValueArray) (err os.Error) {
|
|||
sort.Sort(svList)
|
||||
|
||||
for _, sv := range (svList) {
|
||||
if isValueNil(sv.value) {
|
||||
continue // Skip null values
|
||||
}
|
||||
s := sv.key
|
||||
_, err = fmt.Fprintf(w, "%d:%s", len(s), s)
|
||||
if err != nil {
|
||||
|
@ -468,6 +471,19 @@ func writeValue(w io.Writer, val reflect.Value) (err os.Error) {
|
|||
return
|
||||
}
|
||||
|
||||
func isValueNil(val reflect.Value) bool {
|
||||
if val == nil {
|
||||
return true
|
||||
}
|
||||
switch v := val.(type) {
|
||||
case *reflect.InterfaceValue:
|
||||
return isValueNil(v.Elem())
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Marshal(w io.Writer, val interface{}) os.Error {
|
||||
return writeValue(w, reflect.NewValue(val))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue