Question

I want to use a local sqlite database to cache all gson objects. Therefore I created some Gson classes like this one:

package com.getbro.bro.Json;
import com.google.gson.annotations.SerializedName;
public class User  extends Item {

    public User(String Sex, String UserName, String[] Followed){
        this.Sex = Sex;
        this.UserName = UserName;
        this.Followed = Followed;
    }

    @SerializedName("sex")
    public String Sex;

    @SerializedName("username")
    public String UserName;

    @SerializedName("followed")
    public String[] Followed;

    @Override
    public String toString() {
        return UserName;
    }
}

Now I want to use this class as Sugar ORM model, but then, I have to rewrite the constructor to something like this:

public User(Context c, String Sex, String UserName, String[] Followed){
        this.Sex = Sex;
        this.UserName = UserName;
        this.Followed = Followed;
}

How can I get Gson to use this "special" constructor and select the wright context?

Was it helpful?

Solution

With 1.3 release, sugar doesn't require a Context parameter in the constructor. So, it makes easy for Gson classes to use Sugar.

Specifically with Gson, you could use their custom serializers in case you don't have the default constructor. More details here.. https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top