是否可以使用方法返回作为字段封送结构?例如,我想要这个JSON
{
"cards": [1,2,3],
"value": 6,
"size": 3
}
使用这种结构
type Deck struct {
Cards []int `json:"cards"`
Value func() int `json:"value"`
Size func() int `json:"size"`
}
任何?
答案 0 :(得分:3)
您可以实现Marshaler这样的http://play.golang.org/p/ySUFcUOHCZ(或此http://play.golang.org/p/ndwKu-7Y5m)
package main
import "fmt"
import "encoding/json"
type Deck struct {
Cards []int
}
func (d Deck) Value() int {
value := 0
for _, v := range d.Cards {
value = value + v
}
return value
}
func (d Deck) Size() int {
return len(d.Cards)
}
func (d Deck) MarshalJSON() ([]byte, error) {
value := 0
for _, v := range d.Cards {
value = value + v
}
return json.Marshal(struct {
Cards []int `json:"cards"`
Value int `json:"value"`
Size int `json:"size"`
}{
Cards: d.Cards,
Value: d.Value(),
Size: d.Size(),
})
}
func main() {
deck := Deck{
Cards: []int{1, 2, 3},
}
b, r := json.Marshal(deck)
fmt.Println(string(b))
fmt.Println(r)
}
答案 1 :(得分:1)
您还可以创建一个实现 JSONMarshaler 和 JSONUnmarshaler 的函数类型。但它有一些缺点。
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
}
}
答案 2 :(得分:0)
总的来说,是的。你必须使用reflect package并且基本上编写自己的编组。
使用Go的编码/ json包,没有。