(ObjectBox | Dart) How to pass json to entity class?

558

This is probably the way I found how I could manage to put them together, but this is not ideal way, as clearing parameters in constructors is really odd as entity class structure for JSON serialization.

I would like to have maybe some clearer example how to parse JSON to entity class using ObjectBox. So far I couldn't find any, just abstract explanations.

@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,
  });
}
Share:
558
Edgars Beļevičs
Author by

Edgars Beļevičs

Updated on November 25, 2022

Comments

  • Edgars Beļevičs
    Edgars Beļevičs over 1 year

    The JSON is:

    {
        "name": "Leanne Graham",
        "username": "Bret",
        "email": "[email protected]",
        "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.

    • vaind
      vaind almost 3 years
      Can you add a minimal reproducible example? I've tried adding a ToOne relation to an entity annotated with JsonSerializable() but it seems to get ignored, instead of asking for converters.
    • Yeasin Sheikh
      Yeasin Sheikh almost 3 years
      make nullable using ? on Address. i prefer freezed for doing it
    • vaind
      vaind almost 3 years
      You're mentioning JsonSerializable in the question, that's why I referred to it. If you can provide a minimal code example I could have a look and maybe help you get it working.
    • Edgars Beļevičs
      Edgars Beļevičs over 2 years
      I have entered the code above with JsonSerializable.
    • Edgars Beļevičs
      Edgars Beļevičs over 2 years
      May I please get a help? I have provided code example for models