Support encoding for nil pointers

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Encoding a map[string]interface{} contains nil pointer as value or any struct which has nil pointer field causes runtime error.

      type Foo struct {
      	Bar *Bar
      }
      type Bar struct{}
      
      f := Foo{
      	Bar: nil,
      }
      //  causes "*bsoncodec.StructCodec can only process structs, but got a reflect.Value"
      _, err := bson.Marshal(f)
      	
      m := map[string]interface{}{
      	"foo": nil,
      }
      // causes "cannot perform an encoder or decoder lookup on <nil>"
      _, err = bson.Marshal(m)
      

      To address this problem(maybe a bug?), we should treat nil pointer as BSON null when encoding to BSON, in the same way as json.Marshal() does.

              Assignee:
              Unassigned
              Reporter:
              takumi fujiwara
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: