BuiltValueGenerator — класс с большим количеством кода

#flutter #built-value

Вопрос:

Я создал соединение с базой данных, используя пакеты built_value и built_value_generator . Вот как выглядит структура JSON:

 {
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "color": "gold",
  "null": null,
  "number": 123,
  "object": {
    "a": "b",
    "c": "d"
  },
   "object1": {
    "a": "b",
    "c": "d"
  },
   "object2": {
    "a": "b",
    "c": "d"
  },
   "object3": {
    "a": "b",
    "c": "d"
  },
   "object4": {
    "a": "b",
    "c": "d"
  },
  "string": "Hello World"
}
 

Я использовал этот конвертер:
https://charafau.github.io/json2builtvalue /

Что я получил:

 library root_dto;

import 'dart:convert';

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'root_dto.g.dart';

abstract class RootDto implements Built<RootDto, RootDtoBuilder> {
  RootDto._();

  factory RootDto([updates(RootDtoBuilder b)]) = _$RootDto;

  @BuiltValueField(wireName: 'array')
  BuiltList<int> get array;
  @BuiltValueField(wireName: 'boolean')
  bool get boolean;
  @BuiltValueField(wireName: 'color')
  String get color;
  @BuiltValueField(wireName: 'number')
  int get number;
  @BuiltValueField(wireName: 'object')
  Object get object;
  @BuiltValueField(wireName: 'object1')
  Object1 get object1;
  @BuiltValueField(wireName: 'object2')
  Object2 get object2;
  @BuiltValueField(wireName: 'object3')
  Object3 get object3;
  @BuiltValueField(wireName: 'object4')
  Object4 get object4;
  @BuiltValueField(wireName: 'object5')
  Object5 get object5;
  @BuiltValueField(wireName: 'object6')
  Object6 get object6;
  @BuiltValueField(wireName: 'string')
  String get string;
  String toJson() {
    return json.encode(serializers.serializeWith(RootDto.serializer, this));
  }

  static RootDto fromJson(String jsonString) {
    return serializers.deserializeWith(
        RootDto.serializer, json.decode(jsonString));
  }

  static Serializer<RootDto> get serializer => _$rootDtoSerializer;
}

abstract class Object implements Built<Object, ObjectBuilder> {
  Object._();

  factory Object([updates(ObjectBuilder b)]) = _$Object;

  @BuiltValueField(wireName: 'a')
  String get a;
  @BuiltValueField(wireName: 'c')
  String get c;
  String toJson() {
    return json.encode(serializers.serializeWith(Object.serializer, this));
  }

  static Object fromJson(String jsonString) {
    return serializers.deserializeWith(
        Object.serializer, json.decode(jsonString));
  }

  static Serializer<Object> get serializer => _$objectSerializer;
}

abstract class Object1 implements Built<Object1, Object1Builder> {
  Object1._();

  factory Object1([updates(Object1Builder b)]) = _$Object1;

  @BuiltValueField(wireName: 'a')
  String get a;
  @BuiltValueField(wireName: 'c')
  String get c;
  String toJson() {
    return json.encode(serializers.serializeWith(Object1.serializer, this));
  }

  static Object1 fromJson(String jsonString) {
    return serializers.deserializeWith(
        Object1.serializer, json.decode(jsonString));
  }

  static Serializer<Object1> get serializer => _$object1Serializer;
}

abstract class Object2 implements Built<Object2, Object2Builder> {
  Object2._();

  factory Object2([updates(Object2Builder b)]) = _$Object2;

  @BuiltValueField(wireName: 'a')
  String get a;
  @BuiltValueField(wireName: 'c')
  String get c;
  String toJson() {
    return json.encode(serializers.serializeWith(Object2.serializer, this));
  }

  static Object2 fromJson(String jsonString) {
    return serializers.deserializeWith(
        Object2.serializer, json.decode(jsonString));
  }

  static Serializer<Object2> get serializer => _$object2Serializer;
}

abstract class Object3 implements Built<Object3, Object3Builder> {
  Object3._();

  factory Object3([updates(Object3Builder b)]) = _$Object3;

  @BuiltValueField(wireName: 'a')
  String get a;
  @BuiltValueField(wireName: 'c')
  String get c;
  String toJson() {
    return json.encode(serializers.serializeWith(Object3.serializer, this));
  }

  static Object3 fromJson(String jsonString) {
    return serializers.deserializeWith(
        Object3.serializer, json.decode(jsonString));
  }

  static Serializer<Object3> get serializer => _$object3Serializer;
}

abstract class Object4 implements Built<Object4, Object4Builder> {
  Object4._();

  factory Object4([updates(Object4Builder b)]) = _$Object4;

  @BuiltValueField(wireName: 'a')
  String get a;
  @BuiltValueField(wireName: 'c')
  String get c;
  String toJson() {
    return json.encode(serializers.serializeWith(Object4.serializer, this));
  }

  static Object4 fromJson(String jsonString) {
    return serializers.deserializeWith(
        Object4.serializer, json.decode(jsonString));
  }

  static Serializer<Object4> get serializer => _$object4Serializer;
}

abstract class Object5 implements Built<Object5, Object5Builder> {
  Object5._();

  factory Object5([updates(Object5Builder b)]) = _$Object5;

  @BuiltValueField(wireName: 'a')
  String get a;
  @BuiltValueField(wireName: 'c')
  String get c;
  String toJson() {
    return json.encode(serializers.serializeWith(Object5.serializer, this));
  }

  static Object5 fromJson(String jsonString) {
    return serializers.deserializeWith(
        Object5.serializer, json.decode(jsonString));
  }

  static Serializer<Object5> get serializer => _$object5Serializer;
}

abstract class Object6 implements Built<Object6, Object6Builder> {
  Object6._();

  factory Object6([updates(Object6Builder b)]) = _$Object6;

  @BuiltValueField(wireName: 'a')
  String get a;
  @BuiltValueField(wireName: 'c')
  String get c;
  String toJson() {
    return json.encode(serializers.serializeWith(Object6.serializer, this));
  }

  static Object6 fromJson(String jsonString) {
    return serializers.deserializeWith(
        Object6.serializer, json.decode(jsonString));
  }

  static Serializer<Object6> get serializer => _$object6Serializer;
}
 

In my case I use a few other abstract classes. The problem is that BuiltValueGenerator made a very long generated class — root_dto.g.dart, 3000 lines of code in my case!
Is such an implementation appropriate?
Does this affect performance?
And does it significantly increase the size of the application?