JSON Marshal struct with method return as field
Asked Answered
J

3

12

Is it possible to marshal a struct with method return as field? For example, I want this JSON

{
  "cards": [1,2,3],
  "value": 6,
  "size": 3
}

With this kind of struct

type Deck struct {
   Cards []int    `json:"cards"`
   Value func() int `json:"value"`
   Size  func() int `json:"size"`
}

Anyone?

Jegger answered 6/8, 2015 at 6:59 Comment(3)
No, this requires programming. Maybe a formater in github.com/vdobler/export would be enough.Responsible
Ok, thanks. So, what is the best way to do that whitout including dependencies?Jegger
Deck.Value and Deck.Size are fields, not methods; fields of function type.Gauche
H
15

You can implement a Marshaler like this http://play.golang.org/p/ySUFcUOHCZ (or this 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) {
    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)
}
Hottentot answered 6/8, 2015 at 8:0 Comment(3)
Oh, that's Nice ! But... Not perfectly usefull because I want to calculate the value of the cards inside the same struct. IntFuncJson must be a method not a classic function. func (d *Deck) Value() intJegger
Less beautiful solution than the first but certainly the best way to do that. Thank you ! (value calculation in MarshalJSON function is useless isn't it?)Jegger
I would suggest a little more abstract approach of this method: play.golang.org/p/fdlwiPA_K2YCovington
C
1

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

    }
}
Costotomy answered 2/4, 2021 at 5:2 Comment(0)
M
0

In general, yes. You'd have to use the the reflect package and essentially write your own marshaller.

With Go's encoding/json package, no.

Maxfield answered 6/8, 2015 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.