Skip to content

Instantly share code, notes, and snippets.

@cmelchior
Created April 9, 2015 06:35
Show Gist options
  • Save cmelchior/1a97377df0c49cd4fca9 to your computer and use it in GitHub Desktop.
Save cmelchior/1a97377df0c49cd4fca9 to your computer and use it in GitHub Desktop.
Realm, GSON and primitive JSON arrays
// 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
}
@Joisar
Copy link

Joisar commented Jun 20, 2017

I am facing the same problem about java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:100)

any workaround?

@Joisar
Copy link

Joisar commented Jun 21, 2017

@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

@VivekNeel
Copy link

VivekNeel commented Jul 11, 2017

How can this be used for ArrayList<ArrayList> ?

I have a field like this : @SerializedName("matches") private ArrayList<ArrayList> matches;

@trrkiran007
Copy link

trrkiran007 commented Sep 21, 2017

@cmelchior/@Joisar/ @RajendraSinghBohra, Plz pardon my ignorance..
I am using retrofit and gson to process my response. I'm using it this way:

Gson gson = new GsonBuilder()
.create();

        retrofit = new Retrofit.Builder()
                .baseUrl(Res.string(R.string.appconfig_api_base_url))
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(builder.build())
                .build();

Now in one of my APIs with large json structure, there are a couple of primitive arrays- String and ints.
By using the above workaround to convert these primitives, do I have to process the rest of the response elements with the same custom type adapter? or can I just process the primitive arrays with this method? I'm not sure how to take just the portion of json structure and input it to the deserializer and process rest of them with my default gson.

Thanks in advance..

@trrkiran007
Copy link

trrkiran007 commented Sep 21, 2017

Nevermind, I figured it out myself..
instead of using default gson builder, I used like below:

 Gson gson = RealmParserUtil.getGson();
        retrofit = new Retrofit.Builder()
                .baseUrl(Res.string(R.string.appconfig_api_base_url))
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(builder.build())
                .build();

My Custom RealmParser:

public class RealmParserUtil {
    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) {
        Log.i("SuperCustomer", "Deserializing json response");
        return getGson().fromJson(objectString, type);
    }


    public static class ArrayToStringTypeAdapter extends TypeAdapter<RealmList<RealmString>> {

        @Override
        public void write(JsonWriter out, RealmList<RealmString> value) throws IOException {
            //out.value(String.valueOf(value));
        }

        @Override
        public RealmList<RealmString> read(JsonReader in) throws IOException {
            RealmList<RealmString> list = new RealmList<RealmString>();

            boolean isArray = (in.peek() == JsonToken.BEGIN_ARRAY);

            if(isArray) {
                in.beginArray();
                while (in.hasNext()) {
                    list.add(new RealmString(in.nextString()));
                }
                in.endArray();
            }
            return list;
        }
    };
}

In my model class I used this:

    @JsonAdapter(RealmParserUtil.ArrayToStringTypeAdapter.class)
    private RealmList<RealmString> strInterests;

Thank you @Joisar for your gist, it helped me a lot.

@egpayawal
Copy link

egpayawal commented Jul 9, 2018

@cmelchior
I implemented your solution but its not working for me. Do you have an update code for parsing primitive array string or int?
thanks!

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