interface to json Marshal to add object id

568 views
Skip to first unread message

RoboTamer

unread,
Sep 11, 2012, 8:06:52 PM9/11/12
to golan...@googlegroups.com

I can't figure out how to pass an interface to json Marshal to add the object id to the json file.
Actually it does add the id but not the rest of the fields.
I don't get an error
Below is a sample of what I been playing with. 


Copy of Playground code:

package main

import (
"encoding/json"
"fmt"
)

var merchants map[uint]Var = make(map[uint]Var)
var count uint

type Var interface{}

type Merchant struct {
Name string
}

type jsonObject struct {
Id uint
Var
}

func (v *Merchant) Add(name string) error {
v.Name = name
count++
merchants[count] = v

j := new(jsonObject)
j.Id = count
j.Var = *v

json, err := json.Marshal(&j)
if err != nil {
return err
}
fmt.Printf("\nJson: %s\n", json)

return nil
}

func main() {
fmt.Println("Hello, playground")
m := new(Merchant)
err := m.Add("Gopher Land")
if err != nil {
println(err.Error())
}
}

Kyle Lemons

unread,
Sep 11, 2012, 9:07:21 PM9/11/12
to RoboTamer, golan...@googlegroups.com
The JSON package elides embedded fields (for now).  I believe this is slated to come back in Go 1.1

RoboTamer

unread,
Sep 11, 2012, 10:40:23 PM9/11/12
to golan...@googlegroups.com, RoboTamer
Reply all
Reply to author
Forward
0 new messages