You could also create a function type that implement JSONMarshaler and JSONUnmarshaler. But it has some drawbacks.
import "fmt"
import "encoding/json"
type IntFunc func() int
func (f IntFunc) MarshalJSON() ([]byte, error) {
return json.Marshal(f())
}
// NOTES you'll lose the original function here
func (f *IntFunc) UnmarshalJSON(b []byte) error {
var i int
err := json.Unmarshal(b, &i)
// you could either add dummy function or leave it nil by not assigning *f
*f = func() int { return i }
return err
}
type Deck struct {
Cards []int `json:"cards"`
Value IntFunc `json:"value"`
Size IntFunc `json:"size"`
}
func main() {
deck := Deck{
Cards: []int{1, 2, 3},
}
deck.Value = ValueOf(&deck)
deck.Size = SizeOf(&deck)
fmt.Printf("Size: %v, Cards: %v, Value: %v\n", deck.Size(), deck.Cards, deck.Value())
deck.Cards = append(deck.Cards, 8)
fmt.Printf("Size: %v, Cards: %v, Value: %v\n", deck.Size(), deck.Cards, deck.Value())
fmt.Println()
b, err := json.Marshal(deck)
fmt.Println("Marshal Error:", err)
fmt.Println("Marshal result:", string(b))
fmt.Println()
var d2 Deck
err = json.Unmarshal([]byte(`{"cards":[1,2,3,8],"value":14,"size":4}`), &d2)
fmt.Println("Unmarshal Error =>", err)
fmt.Printf("Unmarshal Result => Size: %v, Cards: %v, Value: %v\n", d2.Size(), d2.Cards, d2.Value()) // could throw error if Size() and Value() is nil
}
func SizeOf(d *Deck) IntFunc {
return func() int {
return len(d.Cards)
}
}
func ValueOf(d *Deck) IntFunc {
return func() int {
i := 0
for _, v := range d.Cards {
i += v
}
return i
}
}
Deck.Value
andDeck.Size
are fields, not methods; fields of function type. – Gauche