francoispqt / gojay

high performance JSON encoder/decoder with stream API for Golang
MIT License
2.11k stars 112 forks source link

Usage for structs that has interfaces as fields (`interface{}`) #62

Closed afiune closed 6 years ago

afiune commented 6 years ago

There are some cases where the JSON data could have different shapes, in those cases we would use an interface{} to define a field within a struct.

Is it possible for gojay to Decode fields of a struct that are interfaces{}?

As for instance, this example of an error that has a description with a number of sections that could have different shapes:

type Error struct {
    Class       string               `json:"class"`
    Message     string               `json:"message"`
    Backtrace   []string             `json:"backtrace"`
    Description Description          `json:"description"`
}
type Description struct {
    Title    string                   `json:"title"`
    Sections []map[string]interface{} `json:"sections"`
}

How would you implement the UnmarshalJSONObject() function?

afiune commented 6 years ago

Uhhhm I think this answers my question: https://github.com/francoispqt/gojay/blob/master/decode.go#L68

😭

francoispqt commented 6 years ago

So, yes there is no such way at the moment but we could add something that would use standard encoding/json to decode interface values.

We could also write our own implementation to unmarshal interfaces, performance would be much better but it would take longer.

afiune commented 6 years ago

I thought about both alternatives, let me try to work on the first one since the second one, as you said, will require more time for its implementation.

Is this something you are accepting pull requests?

francoispqt commented 6 years ago

Yes of course, I'm happy to have contributions :)

afiune commented 6 years ago

Oh cool, I'll work on it.

tenor-227593374