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

Array() and StringArray() do not work. #79

Open
brendesp opened this issue May 13, 2019 · 2 comments
Open

Array() and StringArray() do not work. #79

brendesp opened this issue May 13, 2019 · 2 comments

Comments

@brendesp
Copy link

brendesp commented May 13, 2019

I don't think the following test is correct.

func (j *Json) Array() ([]interface{}, error) {
  if a, ok := (j.data).([]interface{}); ok {
    return a, nil

j.data is already an interface{} so it will never be an []interface{}.

I think the correct test is:

func (j *Json) Array() ([]interface{}, error) {
  if reflect.TypeOf(j.data).Kind() == reflect.Array {
    v := reflect.ValueOf(j.data)
    a := make([]interface{}, v.Len())
    for i := 0; i < v.Len(); i++ {
      a[i] = v.Index(i)
    }
  return a, nil

I will test this locally.

Please let me know if I misunderstand what you are doing here.

@brendesp
Copy link
Author

Here is a test that fails with the current code.

a := [3]string{"one", "two", "three"}
j := sjson.New()
j.Set("myname", a)
_, err = j.Get("myname").Array()
if err != nil {fmt.Println(err)}

@brendesp
Copy link
Author

Turns out that Set() needed to be fixed and convert interface{} for arrays to []interface{}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant