Closed verytable closed 5 years ago
Hi,
This is probably the best way, but it makes me think we could add a Index()
method on the decoder which would return the current index of the array being decoded.
With such solution, you'd do it this way:
type testArray [3]string
func (a *testArray) UnmarshalJSONArray(dec *gojay.Decoder) error {
var str string
if err := dec.String(&str); err != nil {
return err
}
a.arr[dec.Index()] = str
return nil
}
I can create a milestone but I won't be able to do it before 2 weeks. You're welcome to submit a PR.
Let me know
There is a section in docs https://github.com/francoispqt/gojay#arrays-slices-and-channels describing how to decode JSON object to a slice, an array or a channel with examples for a slice and a channel.
User has to implement
UnmarshalerJSONArray
interface consisting of a single functionUnmarshalJSONArray
which is called on each element of a collection.Provided example for a slice (where user can simply append each new element to receiver):
The problem with an array is that there is no (or is there?) way to get current index of unmarshalled array element.
Of course user can maintain this index himself:
Is this the suggested way or i'm missing more elegant/concise/idiomatic solution?