Add JSON serializer to every model class?

前端 未结 9 1444
南旧
南旧 2020-11-28 07:56

When it comes to JSON encoding in Dart, per Seth Ladd\'s accouncement the finally approved now official way to go is dart:convert + JSON.Encode.

9条回答
  •  再見小時候
    2020-11-28 08:23

    Another package solving this problem is built_value:

    https://github.com/google/built_value.dart

    With built_value your model classes look like this:

    abstract class Account implements Built {
      static Serializer get serializer => _$accountSerializer;
    
      int get id;
      String get name;
      BuiltMap get keyValues;
    
      factory Account([updates(AccountBuilder b)]) = _$Account;
      Account._();
    }
    

    Note that built_value isn't just about serialization -- it also provides operator==, hashCode, toString, and a builder class.

提交回复
热议问题