0

I have a problem with importing data from json to Android Realm. I get "Could not map | Expected JsonObject instead of Integer" error.

I know it works when I put the json object instead of foreign key but I want to use foreign key. This is my schema: Database

And this is my models:

Category:

@RealmClass
public class Category extends RealmObject {
    @PrimaryKey
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Product:

@RealmClass
public class Product extends RealmObject {
    @PrimaryKey
    private int id;
    private String name;
    private Category category;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
}

And this is my json file for importing file:

categories.json:

[
  {
    "id": 1,
    "name": "Price"
  },
  {
    "id": 2,
    "name": "Site"
  }
]

products.json:

[
  {
    "id": 1,
    "name": "Price Product",
    "category": 1
  },
  {
    "id": 2,
    "name": "Site Product",
    "category": 2
  }
]

Import.java

Realm db = Realm.getInstance(mContext);
db.beginTransaction();
try {
     db.createOrUpdateAllFromJson(Category.class, App.loadJSONFromAsset(mContext, "categories"));
     db.createOrUpdateAllFromJson(Product.class, App.loadJSONFromAsset(mContext, "products"));
     db.commitTransaction();
 } catch (Exception e) {
     db.cancelTransaction();
     e.printStackTrace();
 }
 db.close();

if I change the products.json to below json, it works.

[
  {
    "id": 1,
    "name": "Price Product",
    "category": {
      "id": 1,
      "name": "Price"
    }
  },
  {
    "id": 2,
    "name": "Site Product",
    "category": {
      "id": 1,
      "name": "Site"
    }
  }
]
navid_gh
  • 1,863
  • 3
  • 17
  • 30

1 Answers1

2

That is the expected error given your model classes. References to other RealmObjects must be represented as objects in JSON. That is because if you want to use Realms in-built JSON capabilities there must be a 1:1 mapping between your JSON and the model classes.

If you want to change that behaviour you will need a 3rd party JSON parser like GSON or Jackson. You can read more here: https://realm.io/docs/java/latest/#gson

Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
  • Thank you for the answer. Can you provide a example for clearance? I used GSON just for serializing and deserializing for transfer class between two devices. I never use it for importing data to database. @christian-melchior – navid_gh Aug 05 '15 at 10:19
  • any example? @ChristianMelchior – navid_gh Aug 07 '15 at 13:12
  • You can see an example of how to write a custom deserializer here: http://stackoverflow.com/questions/6096940/how-do-i-write-a-custom-json-deserializer-for-gson – Christian Melchior Aug 10 '15 at 05:57
  • I checked it out but this is just with a model not RealmObject. Realm has some limitations to define model classes. I don't want to write custom deserializer. Is there other way to deserialize without writing deserializer for each model? @ChristianMelchior – navid_gh Aug 10 '15 at 15:59