(ObjectBox | Dart) 如何将 json 传递给实体类?
Posted
技术标签:
【中文标题】(ObjectBox | Dart) 如何将 json 传递给实体类?【英文标题】:(ObjectBox | Dart) How to pass json to entity class? 【发布时间】:2021-10-03 01:13:04 【问题描述】:JSON 是:
"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"
我为 User、Address、Geo、Company 以及这些实体类的扩展做了四个实体类实体 UserModel、AddressModel、GeoModel、CompanyModel,其中每个模型都有 @JsonSerializable 注释以将 JSON 转换为楷模。 现在让我们以地址为例。问题出在我必须将 AddressModel 传递给 User 实体类时 - 首先我在 User 构造函数中没有地址字段,User 类中的第二个地址现在是 final address = ToOne(),这不是地址类型。所以 JsonSerializable 要求我提供 toJson 和 fromJson 到 ToOne 类。
有什么好的例子,我如何使用 json_serializable 包和带有链接对象的对象框,例如这个 json,因为我正在努力解决它?
这里是 JSON 可序列化模型:
@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);
这里是实体类:
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,
);
虽然我可以在我的应用程序中使用实体,但我不知道如何使用 ToOne,因此我可以将 User 对象保存在 Objectbox 数据库中,并带有指向 Address、Geo 和 Company 对象的链接。
【问题讨论】:
你能添加一个最小的可重现示例吗?我尝试将 ToOne 关系添加到使用JsonSerializable()
注释的实体,但它似乎被忽略了,而不是要求转换器。
在Address
上使用?
使可空。我更喜欢freezed
这样做
你在问题中提到了JsonSerializable
,这就是我提到它的原因。如果你能提供一个最小的代码示例,我可以看看,也许可以帮助你让它工作。
我已经用 JsonSerializable 输入了上面的代码。
我可以帮忙吗?我已经为模型提供了代码示例
【参考方案1】:
这可能是我发现如何将它们组合在一起的方式,但这不是理想的方式,因为在构造函数中清除参数作为 JSON 序列化的实体类结构确实很奇怪。
我希望有一些更清晰的示例,如何使用 ObjectBox 将 JSON 解析为实体类。到目前为止我找不到任何,只是抽象的解释。
@JsonSerializable(explicitToJson: true)
class UserModel extends User
final String name;
final String username;
final String email;
final String phone;
final String website;
@JsonKey(name: "address")
final AddressModel ? addressModel;
@JsonKey(name: "company")
final CompanyModel ? companyModel;
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,
phone: phone,
website: website,
)
this.address.target = addressModel;
this.company.target = companyModel;
factory UserModel.fromJson(Map<String, dynamic> json) =>
_$UserModelFromJson(json);
Map<String, dynamic> toJson() => _$UserModelToJson(this);
@Entity()
class User
@JsonKey(ignore: true)
int id = 0;
final String name;
final String username;
final String email;
final String phone;
final String website;
@JsonKey(ignore: true)
final address= ToOne<Address>();
@JsonKey(ignore: true)
final company= ToOne<Company>();
User(
required this.name,
required this.username,
required this.email,
required this.phone,
required this.website,
);
【讨论】:
以上是关于(ObjectBox | Dart) 如何将 json 传递给实体类?的主要内容,如果未能解决你的问题,请参考以下文章
在模块 objectbox-android-2.9.1-runtime 中找到重复的类 io.objectbox.android.AndroidObjectBrowser
Android 安卓超级强劲的轻量级数据库ObjectBox,快的飞起
Android 安卓超级强劲的轻量级数据库ObjectBox,快的飞起