Martin Marconcini
Martin Marconcini

Reputation: 27236

Using Reserved names with realm.io / Android

Imagine there's an API that returns something like this:

       "names":{  
           "short":"xxx",
           "medium":null,
           "long":"xxxxxx"
        },

(just an example)

Then imagine that you have a model to represent the above that looks like this:

public class Names extends RealmObject {
   private String short;
   private String medium;
   private String long;
   // getters/setters omitted for clarity
}

There's a problem, since both short and long are reserved keywords in Java.

Other ORMs present an annotation (usually @Key("othername")) to deal with these scenarios.

What would be realm.io's solution?

Thanks!

Upvotes: 3

Views: 225

Answers (1)

Christian Melchior
Christian Melchior

Reputation: 20126

Christian from Realm here. Our JSON support is still pretty simple, but we plan to address issues like those in the next iteration. Currently you have two options: Either convert the JSON to JSONObject and manually map the fields in a static methods like this:

public class Names extends RealmObject {

  ...

  public static Names fromJson(JSONObject json) {
    Names names = new Names();
    names.setShortField(json.getString("short");
    return names;
  }
}

or alternatively if you don't mind including other 3rd party libraries, you can use GSON which has a @SerializedName annotation.

Upvotes: 2

Related Questions