Closed marshauf closed 8 years ago
I would like to inline an interface{} into a struct which should be encoded as an array.
Example code:
package test import ( "bytes" "github.com/ugorji/go/codec" "testing" ) type Msg interface{} type messageContainer struct { _struct bool `codec:",,toarray"` T uint32 Msg } type WelcomeMessage struct { Text string } func TestMessageContainer(t *testing.T) { b := bytes.NewBuffer(make([]byte, 1024)) h := &codec.JsonHandle{} enc := codec.NewEncoder(b, h) msgCt := &messageContainer{ false, 1, &WelcomeMessage{"Hello"}, } err := enc.Encode(msgCt) if err != nil { t.Fatal(err) } t.Log(b.String()) }
Result: messages_test.go:36: [1,{"Text":"Hello"}]
I would like it to be: messages_test.go:36: [1,"Text":"Hello"]
Thanks in advance.
Works as expected. What you are trying to do cannot be done. This is not a bug.
Sorry, I meant, I would like the result to be: [1,"Hello"]
What I wrote is possible but is not valid json.
I would like to inline an interface{} into a struct which should be encoded as an array.
Example code:
Result: messages_test.go:36: [1,{"Text":"Hello"}]
I would like it to be: messages_test.go:36: [1,"Text":"Hello"]
Thanks in advance.