The JSON is:
{
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
I Have made four entity classes for User, Address, Geo, Company, and extensions of these entities UserModel, AddressModel, GeoModel, CompanyModel, where each of these models has @JsonSerializable annotation to convert JSON to models. Now lets take Address for example. The problem lies when I have to pass AddressModel to User entity class - first I don't have address field in User constructor, second address in User class is now final address = ToOne(), which is not Address type. So JsonSerializable is asking me to provide toJson and fromJson to ToOne class.
Are there any good examples, how can I use json_serializable package and objectbox with linked objects, for example this json, as I'm struggling to solve it?
Here are JSON serializable Models:
@JsonSerializable(explicitToJson: true)
class UserModel extends User {
final String name;
final String username;
final String email;
final AddressModel address;
final String phone;
final String website;
final CompanyModel company;
UserModel({
required this.name,
required this.username,
required this.email,
required this.address,
required this.phone,
required this.website,
required this.company,
}) : super(
name: name,
username: username,
email: email,
address: address,
phone: phone,
website: website,
company: company,
);
factory UserModel.fromJson(Map<String, dynamic> json) =>
_$UserModelFromJson(json);
Map<String, dynamic> toJson() => _$UserModelToJson(this);
}
// ADDRESS
@JsonSerializable(explicitToJson: true)
class AddressModel extends Address {
final String street;
final String suite;
final String city;
final String zipcode;
final GeoModel geo;
AddressModel({
required this.street,
required this.suite,
required this.city,
required this.zipcode,
required this.geo,
}) : super(
street: street,
suite: suite,
city: city,
zipcode: zipcode,
geo: geo,
);
factory AddressModel.fromJson(Map<String, dynamic> json) =>
_$AddressModelFromJson(json);
Map<String, dynamic> toJson() => _$AddressModelToJson(this);
}
// COMPANY
@JsonSerializable()
class CompanyModel extends Company {
final String name;
final String catchPhrase;
final String bs;
CompanyModel({
required this.name,
required this.catchPhrase,
required this.bs,
}) : super(name: name, catchPhrase: catchPhrase, bs: bs);
factory CompanyModel.fromJson(Map<String, dynamic> json) =>
_$CompanyModelFromJson(json);
Map<String, dynamic> toJson() => _$CompanyModelToJson(this);
}
// GEO
@JsonSerializable()
class GeoModel extends Geo {
final String lat;
final String lng;
GeoModel({
required this.lat,
required this.lng,
}) : super(lat: lat, lng: lng);
factory GeoModel.fromJson(Map<String, dynamic> json) =>
_$GeoModelFromJson(json);
Map<String, dynamic> toJson() => _$GeoModelToJson(this);
}
Here are Entity classes:
class User {
final String name;
final String username;
final String email;
final Address address;
final String phone;
final String website;
final Company company;
User({
required this.name,
required this.username,
required this.email,
required this.address,
required this.phone,
required this.website,
required this.company,
});
}
// ADDRESS
class Address {
final String street;
final String suite;
final String city;
final String zipcode;
final Geo geo;
Address({
required this.street,
required this.suite,
required this.city,
required this.zipcode,
required this.geo,
});
}
// COMPANY
class Company {
final String name;
final String catchPhrase;
final String bs;
Company({
required this.name,
required this.catchPhrase,
required this.bs,
});
}
// GEO
class Geo {
final String lat;
final String lng;
Geo({
required this.lat,
required this.lng,
});
}
While I can use entities inside my app, I don't know how to use ToOne so I could persist User object in Objectbox database with link to Address, Geo, and Company object.