// Make a custom Gson instance, with a custom TypeAdapter for each wrapper object. | |
// In this instance we only have RealmList<RealmInt> as a a wrapper for RealmList<Integer> | |
Type token = new TypeToken<RealmList<RealmInt>>(){}.getType(); | |
Gson gson = new GsonBuilder() | |
.setExclusionStrategies(new ExclusionStrategy() { | |
@Override | |
public boolean shouldSkipField(FieldAttributes f) { | |
return f.getDeclaringClass().equals(RealmObject.class); | |
} | |
@Override | |
public boolean shouldSkipClass(Class<?> clazz) { | |
return false; | |
} | |
}) | |
.registerTypeAdapter(token, new TypeAdapter<RealmList<RealmInt>>() { | |
@Override | |
public void write(JsonWriter out, RealmList<RealmInt> value) throws IOException { | |
// Ignore | |
} | |
@Override | |
public RealmList<RealmInt> read(JsonReader in) throws IOException { | |
RealmList<RealmInt> list = new RealmList<RealmInt>(); | |
in.beginArray(); | |
while (in.hasNext()) { | |
list.add(new RealmInt(in.nextInt())); | |
} | |
in.endArray(); | |
return list; | |
} | |
}) | |
.create(); | |
// Convert JSON to objects as normal | |
List<MainObject> objects = gson.fromJson(json, new TypeToken<List<MainObject>>(){}.getType()); | |
// Copy objects to Realm | |
realm.beginTransaction(); | |
realm.copyToRealm(objects); | |
realm.commitTransaction(); |
[ | |
{ "name" : "Foo", | |
"ints" : [1, 2, 3] | |
}, | |
{ "name" : "Bar", | |
"ints" : [] | |
} | |
] |
public class MainObject extends RealmObject { | |
private String name; | |
private RealmList<RealmInt> ints; | |
// Getters and setters | |
} |
public class RealmInt extends RealmObject { | |
private int val; | |
public RealmInt() { | |
} | |
public RealmInt(int val) { | |
this.val = val; | |
} | |
// Getters and setters | |
} |
This comment has been minimized.
This comment has been minimized.
if the primitive array can be null you might want to add this check to your read method: if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
} |
This comment has been minimized.
This comment has been minimized.
This was very helpful, thank you. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
@cmelchior any workaround if we use realm.createOrUpdateObjectFromJson(RecipeRealmObject.class, jsonObject) method. I have a json object as : {"recipe":{ "name":"Recipe name", cookingSteps": [ Any help would be greatly appreciated. |
This comment has been minimized.
This comment has been minimized.
I am facing the same problem about java.lang.String cannot be converted to JSONObject any workaround? |
This comment has been minimized.
This comment has been minimized.
@cmelchior, @RajendraSinghBohra : I have created a solution which will work fine without having issue as mentioned in previous comments. https://gist.github.com/Joisar/72629f74b3da368ca34b358e91bd34e7 |
This comment has been minimized.
This comment has been minimized.
How can this be used for I have a field like this : |
This comment has been minimized.
This comment has been minimized.
@cmelchior/@Joisar/ @RajendraSinghBohra, Plz pardon my ignorance.. Gson gson = new GsonBuilder()
Now in one of my APIs with large json structure, there are a couple of primitive arrays- String and ints. Thanks in advance.. |
This comment has been minimized.
This comment has been minimized.
Nevermind, I figured it out myself..
My Custom RealmParser:
In my model class I used this:
Thank you @Joisar for your gist, it helped me a lot. |
This comment has been minimized.
This comment has been minimized.
@cmelchior |
This comment has been minimized.
Don't we need to define primary key for RealmInt ? so it does not overwrite. Consider i have two RealmList in Model and they have same values, then i want to change one of the list's value and leave other RealmList same, is this possible with this implementation? @cmelchior