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

Can't get List<Foo> from Hawk.get #61

Closed
dos1in opened this issue Jul 21, 2015 · 7 comments
Closed

Can't get List<Foo> from Hawk.get #61

dos1in opened this issue Jul 21, 2015 · 7 comments

Comments

@dos1in
Copy link

dos1in commented Jul 21, 2015

Hi there,
I defined a Class named Foo.

public class Foo {

    private String carSeries;
    private Uri carImageUri;
    private String carType;


    public String getCarSeries() {
        return carSeries;
    }

    public void setCarSeries(String carSeries) {
        this.carSeries = carSeries;
    }

    public Uri getCarImageUri() {
        return carImageUri;
    }

    public void setCarImageUri(Uri carImageUri) {
        this.carImageUri = carImageUri;
    }

    public String getCarType() {
        return carType;
    }

    public void setCarType(String carType) {
        this.carType = carType;
    }

    @Override
    public String toString() {
        return "Foo{" +
            "carSeries='" + carSeries + '\'' +
            ", carImageUri=" + carImageUri +
            ", carType='" + carType + '\'' +
            '}';
    }
}

List<Foo> foos = new ArrayList<Foo>();
foos.add(new Foo());
foos.add(new Foo());
Hawk.put(Constant.PREF_FOO, foos);

but Hawk.get(Constant.PREF_FOO, new ArrayList<Foo>()); always return list which size is 0(default value).

@LinkOut
Copy link

LinkOut commented Jul 21, 2015

Returns zero because of the "new"

Hawk.get(Constant.PREF_FOO, new ArrayList()); <- This is wrong, because the proper way to get back your "foos" is this:

List < Foo > foos = Hawk.get(Constant.PREF_FOO);

Check the doc.

@dos1in
Copy link
Author

dos1in commented Jul 21, 2015

Hawk.get(Constant.PREF_FOO, new ArrayList()); <- I need the default value.
Hawk.get(Constant.PREF_FOO); <- return null.

I think it's the Uriclass causes the Gson can't deserialize the Foo object.
When I removed it, it's gonna be right.

@LinkOut
Copy link

LinkOut commented Jul 21, 2015

hmm if i understand well you try to do something like this?

  • Hawk.get(Constant.PREF_FOO, new ArrayList()); <- get the default value
  • Maybe if the default value is 0 you just "put" your foo's Hawk.put(Constant.PREF_FOO, foos);
  • You try to retrieve the foos back with something like: List < Foo > foos = Hawk.get(Constant.PREF_FOO); (after you put them) and the size is 0?

@dos1in
Copy link
Author

dos1in commented Jul 21, 2015

yep, i put the list which size is 2.
The Uri field cause Hawk.get always return the 0 size list(default value).

@orhanobut
Copy link
Owner

I haven't tried yet but gson apparently can't serialize and deserialize everything due to immutability, I'll find another parse approach. I have the same problem with joda time.

Work around could be to use transient keyword for Uri to avoid it being serialized/deserialized for now.

@orhanobut orhanobut added the bug label Jul 22, 2015
@LinkOut
Copy link

LinkOut commented Jul 22, 2015

In alternative to GSON i recommend Jackson

@orhanobut
Copy link
Owner

I'll create another issue for different converters. I'm closing this issue.

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

No branches or pull requests

3 participants