Go: workaround to json.Unmarshal being incapable of deserialising into custom interface type

in #go5 years ago

You may have noticed that json.Unmarshal always refuses to deserialise JSON into a custom interface type. For example, given a custom interface:

type Fruit interface {
    Name() string

... And to deserialise a Lemon:

type Lemon struct {
    KindOfLemon string
func (le Lemon) Name() string {
    return le.KindOfLemon

...But due to certain circumstances, the code cannot touch Lemon directly, all you have is a Fruit:

var deserialisedLime Fruit = Lemon{}
fmt.Println(json.Unmarshal(serialsedLime, &deserialisedLime))

Unmarshal function will complain that it "cannot unmarshal object into Go value of type INTERFACE-NAME".

Don't worry - the statement is simply false, the JSON package is in fact perfectly capable of doing it, just use a little reflection trick:

var limeReflectValue = reflect.New(reflect.TypeOf(Lemon{}))
var limeIface interface{} = limeReflectValue.Interface()
fmt.Println(json.Unmarshal(serialsedLime, &limeIface))

// Confirmation
var getLimeBack Fruit = limeIface.(Fruit)
fmt.Println(getLimeBack.Name()) // lime confirmed


Coin Marketplace

STEEM 0.22
TRX 0.06
JST 0.025
BTC 19475.45
ETH 1336.39
USDT 1.00
SBD 2.44