Skip to content

Instantly share code, notes, and snippets.

@Joisar
Last active January 13, 2019 00:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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.beginTransaction();
realm.createOrUpdateObjectFromJson(UserInfo.class, userInfoJsonObject);
realm.commitTransaction();
realm.close();
public class UserInfo extends RealmObject {
@PrimaryKey
private Integer id;
@JsonAdapter(Utils.ArrayToStringTypeAdapter.class)
private String hobbies;
}
{
"id": 1,
"hobbies": [
"singing",
"dancing",
"coding"
]
}
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;
}
};
}
@ShreyashPromact
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)
AdditionalData.java

public class AdditionalData extends RealmObject {
    @PrimaryKey
    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.

Thanks.

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