Question

I'm writing a wrapper to map with some additional functionality I need. Some of the most important things is the ability to marshal and unmarshal the data while retaining genericity. I managed to write an marshaller using the encoding/gob encoder, but since it would be nice if the marshalled data was human readable, I decided to code another implementation with JSON.

I got gob to encode from and decode to generic interface variables neatly by passing it a implementation object instance with Register(). (This resource helped me with the details! http://www.funcmain.com/gob_encoding_an_interface)

However, JSON doesn't have Register(). Let's say we have a value of type

type ConcreteImplementation struct {
    FieldA string
    FieldB string
}

func (c ConcreteImplementation) Key() string {
    return c.FieldA   // ConcreteImplementation implements genericValue
}

in a variable of type

type genericValue interface {
    Key() string
}

When I marshal that, it outputs JSON like this:

{"FieldA":"foo","FieldB":"bar"}

And when I try to unmarshal that again into a variable of type genericValue, it says:

panic: interface conversion: map[string]interface {} is not genericValue: missing method Key EDIT: Oops, actually it says this!

Error with decoding! json: cannot unmarshal object into Go value of genericValue

Quite obviously, it tries to marshal the data like it says here: http://blog.golang.org/json-and-go (See 'Generic JSON with interface{}')

How can I get it to try to fit the data to an specific implementation, like gob decoder would try if the implementation is Register()ed? Register() was godsend, it allowed to marshal and unmarshal generically like it was nothing. How do I get JSON to do the same thing?

Was it helpful?

Solution 2

Allright, got it to work finally. This question provided the answer. Why does json.Unmarshal return a map instead of the expected struct?

"You've passed to json a pointer to an abstract interface. You should simply pass a pointer to Ping as an abstract interface" - This applied to my situation too. (For some reason a pointer TO an abstract interface was enough for gob package. It seems that I have to study Go interfaces and reflection some more to understand why...)

I won't still mark this as solved question, if someone has a better answer.

OTHER TIPS

What if your types implemented the Unmarshaler?

Here is a small demo.

Or the same code here:

type ConcreteImplementation struct {
    FieldA string
    FieldB string
}

func (c ConcreteImplementation) Key() string {
    return c.FieldA // ConcreteImplementation implements genericValue
}

// implementing json.Unmarshaler
func (c *ConcreteImplementation) UnmarshalJSON(j []byte) error {
    m := make(map[string]string)
    err := json.Unmarshal(j, &m)
    if err != nil {
        return err
    }
    if v, ok := m["FieldA"]; ok {
        c.FieldA = v
    }
    if v, ok := m["FieldB"]; ok {
        c.FieldB = v
    }
    return nil
}

type genericValue interface {
    Key() string
    json.Unmarshaler
}

func decode(jsonStr []byte, v genericValue) error {
    return json.Unmarshal(jsonStr, v)
}

With this you can pass a genericValue to json.Unmarshal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top