Skip to content

Instantly share code, notes, and snippets.

@naquad
Created June 29, 2014 13:17
Show Gist options
  • Save naquad/32164c99ba215110d560 to your computer and use it in GitHub Desktop.
Save naquad/32164c99ba215110d560 to your computer and use it in GitHub Desktop.
func encodeJSON(x interface{}) (b []byte, e error) {
buf := bytes.NewBufferString("")
enc := json.NewEncoder(buf)
if e = enc.Encode(x); e == nil {
b = buf.Bytes()
}
return
}
func (p *Payload) MarshalJSON() ([]byte, error) {
t := make(map[string]interface{})
for k, v := range *p.Values {
switch len(v) {
case 1:
t[k] = v[0]
case 0:
default:
t[k] = v
}
}
if len(p.Payload) > 0 {
t["Payload"] = p.Payload
}
return encodeJSON(t)
}
func (p *Payload) JSONEncode() (r string, e error) {
s, e := encodeJSON(p)
if e == nil {
r = string(s)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment