首页 > 解决方案 > (ObjectBox | Dart) 如何将 json 传递给实体类?

问题描述

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"
    }
  }

我为UserAddressGeoCompany以及这些实体的扩展创建了四个实体类UserModelAddressModelGeoModelCompanyModel,其中每个模型都有 @JsonSerializable 注释以将 JSON 转换为模型。现在让我们以地址为例。问题在于当我必须将 AddressModel 传递给 User 实体类时 - 首先我在 User 构造函数中没有地址字段,User 类中的第二个地址现在是最终地址 = ToOne(),它不是 Address 类型。所以 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 对象的链接。

标签: jsonflutterdartobjectbox

解决方案


这可能是我发现如何将它们组合在一起的方式,但这不是理想的方式,因为在构造函数中清除参数作为 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,
  });
}

推荐阅读