Skip to content

Instantly share code, notes, and snippets.

Last active January 13, 2019 00:22
Show Gist options
  • Save Joisar/72629f74b3da368ca34b358e91bd34e7 to your computer and use it in GitHub Desktop.
Save Joisar/72629f74b3da368ca34b358e91bd34e7 to your computer and use it in GitHub Desktop.
Realm, GSON and primitive JSON arrays
//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.createOrUpdateObjectFromJson(UserInfo.class, userInfoJsonObject);
public class UserInfo extends RealmObject {
private Integer id;
private String hobbies;
"id": 1,
"hobbies": [
public class Utils {
public static Gson getGson(){
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
public boolean shouldSkipClass(Class<?> clazz) {
return false;
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> {
public void write(JsonWriter out, String value) throws IOException {
public String read(JsonReader in) throws IOException {
String data = "";
boolean isArray = (in.peek()== JsonToken.BEGIN_ARRAY)?true:false;
while (in.hasNext()) {
data = data + in.nextString() +",";
data = data.substring(0,data.length()-1);
data = in.nextString();
return data;
Copy link

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.

  "id": "aadessddsdss9bdfs69c-6f13-4417-b356-230dbb36c887",
  "propertyId": "49bbe89e-8709-4e0f-a680-3c73aa36e424",
  "rowId": "zzzadsaaedsd3fsdfsss33a-11fc-4396-8c69-d88001e813c5",
  "jobFormsId": "",
  "observation": [
      "FieldId": "ddbaesds33fsdf13a-11fc-4396-8c69-d88001e813c5",
      "Value": "sfshello"
      "FieldId": "e88dddfssdc4s7c4-73bc-45ba-900f-bf5286036003",
      "Value": "ritika"
      "FieldId": "b2ea8sdsdsfssdc144-812d-47a2-9239-8471558df189",
      "Value": "May"

I have made Class like below: (Here I want to store observations as String to realm)

public class AdditionalData extends RealmObject {
    private String id;
    private String jobFormId;
    private String propertyId;
    private String observations;
    private String rowId;
    public AdditionalData() {
        // Empty constructor

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.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment