Reputation: 95
I have an entity, Employee:
public class Employee extends RealmObject implements Serializable {
@PrimaryKey
@SerializedName("id") public long id;
@SerializedName("name") public String firstName;
@SerializedName("lastName") public String lastName;
@SerializedName("profilePictureSmall") public String profilePictureSmallUrl;
@SerializedName("contact") public Contact contact;
}
public class Contact extends RealmObject implements Serializable {
@PrimaryKey
@SerializedName("id") public long id;
@SerializedName("workMail") public String workEmail;
... Some other fields ...
}
When deserializing Employee
from json, all String
fields and long
field are deserialized properly, but my contact
field is left null
.
I've tried writing a custom deserializer for Employee:
public class DeserializeEmployee implements JsonDeserializer<Employee> {
@Override
public Employee deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Log.d(TAG, "Deserializing Employee");
Employee employee = new Employee();
JsonObject o = json.getAsJsonObject();
employee.id = o.get("id").getAsLong();
employee.firstName = o.get("name").getAsString();
employee.lastName = o.get("lastName").getAsString();
employee.profilePictureSmallUrl = o.get("profilePictureSmall").getAsString();
employee.contact = context.deserialize(o.get("contact"), Contact.class);
return employee;
}
}
and a custom deserializer for Contact:
public class DeserializeContact implements JsonDeserializer<Contact> {
@Override
public Contact deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Log.d(TAG, "Deserializing Contact");
Contact contact = new Contact();
JsonObject o = json.getAsJsonObject();
contact.id = o.get("id").getAsLong();
contact.workEmail = o.get("workMail").getAsString();
return contact;
}
}
I've also registered them both:
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, (JsonSerializer<Date>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getTime()))
.registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()))
.registerTypeAdapter(Contact.class, new DeserializeContact())
.registerTypeAdapter(Employee.class, new DeserializeEmployee())
.create();
My Employee deserializer is called and its primitive fields are set properly, but my Contact deserializer isn't called at all, and field contact
is therefore null
.
JSON:
{
"id": 5,
"name": "John",
"lastName": "Doe",
"profilePictureSmall": "http://example.com/fjlsjf",
"contact": {
"id": 9,
"workMail": "[email protected]"
}
}
Edit Added sample json if somebody needs, but pretty sure it isn't the problem.
Upvotes: 0
Views: 422
Reputation: 95
I was sending wrong JSON from the server, which didn't include contact
field. When I've actually sent proper data, everything worked, even without custom deserializers. Sorry for wasting everyone's time.
Upvotes: 1
Reputation: 201
Try doing this:
employeeObject = realm.copyFromRealm(employeeObject);
Upvotes: 0