Skip to content

Instantly share code, notes, and snippets.

@stuntgoat
Last active January 12, 2017 16:45
Show Gist options
  • Save stuntgoat/0902eaf878030321d699bb050001f4b7 to your computer and use it in GitHub Desktop.
Save stuntgoat/0902eaf878030321d699bb050001f4b7 to your computer and use it in GitHub Desktop.
Raw JSON URL string from json marshal of a struct
package main
import (
`bytes`
`fmt`
`net/url`
`encoding/json`
)
type Ex struct {
Url url.URL
}
func (ex Ex) MarshalJSON() ([]byte, error) {
tmp := struct{
Url string `json:"url,"`
}{
ex.Url.String(),
}
b := bytes.NewBuffer([]byte(``))
e := json.NewEncoder(b)
e.SetEscapeHTML(false)
err := e.Encode(tmp)
return b.Bytes(), err
}
func main() {
u := "http://googl.ntt/chair?hi=bye&this=that"
urx, _ := url.Parse(u)
e := Ex{*urx}
b := bytes.NewBuffer([]byte(``))
enc := json.NewEncoder(b)
enc.SetEscapeHTML(false)
enc.Encode(e)
// {"url":"http://googl.ntt/chair?hi=bye&this=that"}
fmt.Println(b.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment