I have this type with a value member called "Url". When encoding this type to json, I want to prepend the HOST value in front of the Url. What is the best way to achieve that?
In this example, the Println
statement prints {"url":"/thisurl"}
, I want it to print {"url":"http://myhost.com/thisurl"}
package main
import "fmt"
import "encoding/json"
type Post struct {
Url string `json:"url"`
}
const (
HOST = "http://myhost.com"
)
func main() {
post := Post{"/thisurl"}
marshaled, _ := json.Marshal(post)
fmt.Println(string(marshaled))
//{"url":"/thisurl"}
}
I could re-assign post.Url right before the json.Marshal
line
post.Url = fmt.Sprintf("%s%s", HOST, post.Url)
But that feels a little messy, if I have to remember to do a reassignment every time I want to encode to json.
I don't want to change the value of post.Url
, I simply want to change how it is represented as json.
If you want to change the (un)marshalling behaviour then use a custom type that implements json.Marshaler
(and possibly json.Unmarshaler
).
package main
import (
"encoding/json"
"fmt"
)
type Post struct {
URL URLString `json:"url"`
}
const (
Host = "http://myhost.com"
)
type URLString string
func (u URLString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s%s"`, Host, u)), nil
}
func main() {
post := Post{"/thisurl"}
marshaled, _ := json.Marshal(post)
fmt.Println(string(marshaled))
//{"url":"http://myhost.com/thisurl"}
}
(also note, using all caps for constants is not idiomatic, and golint
recommends URL
, MyURL
, HTTP
, ID
vs Url
, MyUrl
, or Id
).