Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't encode []map[string]*mapval correctly #57

Closed
clbanning opened this issue Mar 11, 2015 · 2 comments
Closed

Doesn't encode []map[string]*mapval correctly #57

clbanning opened this issue Mar 11, 2015 · 2 comments

Comments

@clbanning
Copy link

Needs to separate map k:v pairs with commas - ',' - use case below w/o codecgen output. To complete "go test" case run "codecgen -o codecdemo_codec.go codecdemo.go" first.

codecdemo.go:

package codecdemo

import (
"github.com/ugorji/go/codec"
"strconv"
"time"
)

type MapVal struct {
Name string
Time time.Time
}

type Entry struct {
Vals map[string]*MapVal
}

type Wrapper struct {
Entries []*Entry
}

func Codecdemo() ([]byte, error) {
w := new(Wrapper)
w.Entries = make([]_Entry, 1)
e := new(Entry)
w.Entries[0] = e
w.Entries[0].Vals = make(map[string]_MapVal, 0)

for i := 0; i < 4; i++ {
    m := new(MapVal)
    m.Name = strconv.Itoa(i + 1)
    m.Time = time.Now()
    w.Entries[0].Vals[m.Name] = m
}

b := make([]byte, 0)
h := new(codec.JsonHandle)
enc := codec.NewEncoderBytes(&b, h)
if err := enc.Encode(w); err != nil {
    return nil, err
}

return b, nil

}

codecdemo_test.go:

import (
"encoding/json"
"testing"
)

func TestCodecDemo(t *testing.T) {
val, err := Codecdemo()
if err != nil {
t.Fatal(err)
}

m := new(Wrapper)
if err = json.Unmarshal(val, m); err != nil {
    println("val:", string(val))
    t.Fatal(err)
}

println("val:", string(val))

}

@clbanning clbanning changed the title Doesn't encode map[string]*mapval correctly Doesn't encode []map[string]*mapval correctly Mar 11, 2015
@clbanning clbanning changed the title Doesn't encode []map[string]*mapval correctly Doesn't encode map[string]*mapval correctly Mar 11, 2015
@clbanning clbanning changed the title Doesn't encode map[string]*mapval correctly Doesn't encode []map[string]*mapval correctly Mar 11, 2015
@ugorji
Copy link
Owner

ugorji commented Mar 11, 2015

It's not clear what your issue is.

I reproduced this, and it worked fine.

https://gist.github.com/ugorji/8bb851bf49cff1c3d4d9

@ugorji ugorji closed this as completed Mar 11, 2015
@clbanning
Copy link
Author

Does not work with codecgen code generation on a package.

@ugorji ugorji reopened this Mar 12, 2015
@ugorji ugorji closed this as completed in 141348d Mar 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants