Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the suggested way to decode into an array? #94

Closed
verytable opened this issue Jan 28, 2019 · 1 comment · Fixed by #96
Closed

What is the suggested way to decode into an array? #94

verytable opened this issue Jan 28, 2019 · 1 comment · Fixed by #96
Assignees
Labels
enhancement New feature or request

Comments

@verytable
Copy link
Contributor

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 function UnmarshalJSONArray which is called on each element of a collection.

Provided example for a slice (where user can simply append each new element to receiver):

type testSlice []string
// implement UnmarshalerJSONArray
func (t *testStringArr) UnmarshalJSONArray(dec *gojay.Decoder) error {
	str := ""
	if err := dec.String(&str); err != nil {
		return err
	}
	*t = append(*t, str)
	return nil
}

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:

type testArray struct {
	arr       [3]string
	decodeIdx int
}

func (a *testArray) UnmarshalJSONArray(dec *gojay.Decoder) error {
	str := ""
	if err := dec.String(&str); err != nil {
		return err
	}
	a.arr[a.decodeIdx] = str
	a.decodeIdx++
	// handle overflow
	return nil
}

Is this the suggested way or i'm missing more elegant/concise/idiomatic solution?

@verytable verytable changed the title What is the suggested way to decode array? What is the suggested way to decode into array? Jan 28, 2019
@verytable verytable changed the title What is the suggested way to decode into array? What is the suggested way to decode into an array? Jan 28, 2019
@francoispqt
Copy link
Owner

francoispqt commented Jan 29, 2019

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

@francoispqt francoispqt added the enhancement New feature or request label Jan 29, 2019
@francoispqt francoispqt self-assigned this Jan 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants