Last active
January 13, 2019 00:22
-
-
Save Joisar/72629f74b3da368ca34b358e91bd34e7 to your computer and use it in GitHub Desktop.
Realm, GSON and primitive JSON arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//parsing with help of gson without storing via realm | |
UserInfo userInfo = (UserInfo)Utils.deserialize(userInfoJsonObject.toString(),new TypeToken<UserInfo>(){}.getType()); | |
//save data using realm | |
Realm realm = Realm.getDefaultInstance(); | |
realm.beginTransaction(); | |
realm.createOrUpdateObjectFromJson(UserInfo.class, userInfoJsonObject); | |
realm.commitTransaction(); | |
realm.close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserInfo extends RealmObject { | |
@PrimaryKey | |
private Integer id; | |
@JsonAdapter(Utils.ArrayToStringTypeAdapter.class) | |
private String hobbies; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": 1, | |
"hobbies": [ | |
"singing", | |
"dancing", | |
"coding" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Utils { | |
public static Gson getGson(){ | |
Gson gson = new GsonBuilder() | |
.setLenient() | |
.setExclusionStrategies(new ExclusionStrategy() { | |
@Override | |
public boolean shouldSkipField(FieldAttributes f) { | |
return f.getDeclaringClass().equals(RealmObject.class); | |
} | |
@Override | |
public boolean shouldSkipClass(Class<?> clazz) { | |
return false; | |
} | |
}) | |
.create(); | |
return gson; | |
} | |
public static String serialize(Object object) { | |
return getGson().toJson(object); | |
} | |
public static Object deserialize(String objectString, Type type) { | |
return getGson().fromJson(objectString, type); | |
} | |
public static class ArrayToStringTypeAdapter extends TypeAdapter<String> { | |
@Override | |
public void write(JsonWriter out, String value) throws IOException { | |
out.value(String.valueOf(value)); | |
} | |
@Override | |
public String read(JsonReader in) throws IOException { | |
String data = ""; | |
boolean isArray = (in.peek()== JsonToken.BEGIN_ARRAY)?true:false; | |
if(isArray){ | |
in.beginArray(); | |
while (in.hasNext()) { | |
data = data + in.nextString() +","; | |
} | |
if(data.endsWith(",")){ | |
data = data.substring(0,data.length()-1); | |
} | |
in.endArray(); | |
} | |
else{ | |
data = in.nextString(); | |
} | |
return data; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, Thanks for the trick.
I have an object like above one, but instead of String array, it is like jsonArray. I want to store that as a string to the realm. Have a look at below JSON structure for same.
I have made Class like below: (Here I want to store observations as String to realm)
AdditionalData.java
Now, when trying to store AdditionalData to the realm, it is not storing observations in the formate it is right now. Can you please help me here to resolve this query.
Thanks.