I want to store a custom color palette inside a JSON file, but the palette has the type []color.Color
(that is an interface and not a concrete type). When I marshal the palette I get something like this:
[{"R":0,"G":0,"B":0,"A":255},{"R":0,"G":0,"B":51,"A":255}...]
The problem is, when I unmarshal that JSON the type []color.Color
does not work, because Go cannot create a concrete type underneath that interface.
I have simplified my code to following example:
type myT struct {
P []color.Color
}
func main() {
t := myT{palette.WebSafe}
b, err := json.Marshal(t)
e("json.Marshal", err)
t2 := myT{}
err = json.Unmarshal(b, &t2)
e("json.Unmarshal", err)
fmt.Println(string(b))
}
func e(s string, err error) {
if err != nil {
fmt.Println(s, err)
}
}
https://play.golang.org/p/QYIpJ7L1ete
Is there a simple solution or do I have to transform []color.Color
to []color.RGBA
?