Last active
January 12, 2017 16:45
-
-
Save stuntgoat/0902eaf878030321d699bb050001f4b7 to your computer and use it in GitHub Desktop.
Raw JSON URL string from json marshal of a struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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