I have a struct as this:
type Int64Slice []int64
type DataWrapper struct {
ListId Int64Slice `json:"listid" required`
Domain string `json:"domain" required`
Name string `json:"name,omitempty"`
}
And I need it become:
{
"listid": "1 2 3 4 5",
"domain": "mydomain"
}
I have wrote custom MarshalJSON:
func (u Int64Slice) MarshalJSON() ([]byte, error) {
var result string
if u == nil {
result = "null"
} else {
result = strings.Trim(strings.Join(strings.Fields(fmt.Sprint(u)), " "), "[]")
Logger.Debugln(result)
}
return []byte(result), nil
}
func (d *DataWrapper) ToJSON() []byte {
result, err := json.Marshal(d)
if err != nil {
log.Fatalln(err)
panic(err)
}
return result
}
At the line Logger.Debugln(result)
, it prints this result:
20170830090317506 20170830090026319 20170830111023194 201708301043081 ...
json: error calling MarshalJSON for type models.Int64Slice: invalid character '2' after top-level value