ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.86k stars 295 forks source link

struct with inlined interface{} toarray #149

Closed marshauf closed 8 years ago

marshauf commented 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.

ugorji commented 8 years ago

Works as expected. What you are trying to do cannot be done. This is not a bug.

marshauf commented 8 years ago

Sorry, I meant, I would like the result to be: [1,"Hello"]

What I wrote is possible but is not valid json.