Parsing a String id with SugarORM and GSON
Asked Answered
A

6

11

I'm using GSON to create a SugarRecord object from a json response. The API I'm using returns a field called "id", but the type of "id" is a string, not a long (the backend is using mongo).

Below is the code I'm using:

Gson gson = new Gson(); // Or use new GsonBuilder().create();
NutritionPlan target = gson.fromJson(jsonObject.getJSONObject("nutrition_day").toString(), NutritionPlan.class);

Below is my json response:

{
"nutrition_day": {
    "id": "5342b4163865660012ab0000",
    "start_on": "2014-04-08",
    "protein_target": 157,
    "sodium_limit": 2000
}

Is there a good way to handle this scenario? I tried

@Ignore 
long id;

and

@SerializedName("id")
String nutrition_plan_id;

in my model, but neither helped. Anyone familiar with Sugar ORM, and know how to deal with an id field that isn't a long?

Aplanatic answered 14/4, 2014 at 20:31 Comment(2)
why don't you take long id out of your model?Gawky
tried that. the long type of the id automatically comes from SugarRecord. I'm trying to overwrite it and change it to a string typeAplanatic
M
4

Replace the key "id" in the string to be "nutrition_day_id". You can use the id json and the id sql.

jsonObject.getJSONObject("nutrition_day").toString().replace("\"id\"","\"nutrition_day_id\"")
Mayes answered 12/7, 2015 at 21:14 Comment(0)
W
1

It helped me to change id name to mid and add annotation @Expose to all fields which have to be serialized and add annotation @SerializedName to new id field.

@SerializedName("id")
@Expose
private final String mid;

@Expose
private final String street;
Welford answered 9/1, 2016 at 23:52 Comment(0)
S
1

No big changes need be made.

Just use transient statement before Long type.

    @SerializedName("id")
    @Expose
    private transient Long id;

Then, if you have getId and setId methods, should delete them.

And have fun!

Sardine answered 9/1, 2020 at 21:57 Comment(0)
D
0

I've been concerned with the same problem, and the only solution i've found was to rename the id name from my API. From example try to send nutrition_plan_id instead of id from your API and it would done the job.

Disentitle answered 24/10, 2014 at 0:42 Comment(1)
The only issue is, I'm using a third party API, so I can't change the id.Aplanatic
N
0

Here is my solution, but it won't be the best.

I just ignored id in my Feed

public class Feed extends SugarRecord<Feed>  {

  @Expose int idMember;
}

It works fine, however the real id is no more used.

Niggling answered 15/3, 2015 at 8:21 Comment(0)
P
0

I was looking for a solution to this problem for quite some time. Finally I found a solution to it. Simply add @Table annotation to the desired class instead of extending it from SugarRecord and manually add id attribute. You can exclude it from Gson serialization via transient keyword, or/and rename it, with @SerializedName annotation.

Example (works fine for me on SugarOrm v1.4):

@Table
public class MySugarOrmClass{

    @SerializedName("db_id")
    private transient Long id = null;

    @SerializedName("id")
    private Integer aid;

    // Add an empty constructor
    public MySugarClass(){}


    //Other fields and class attributes
    ...
}

But this means that you cannot use .save(), .delete(), etc. methods on this class. Instead you have to use SugarRecord's static methods: SugarRecord.save(), SugarRecord.delete(),...

Ploy answered 24/2, 2016 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.