Skip to content

Commit b2dc94c

Browse files
committed
api [nfc]: Use JsonSerializable with MessageEvent
It will make it easier to add addditional fields on the event later
1 parent e524e6b commit b2dc94c

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

lib/api/model/events.dart

+8-10
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,7 @@ class UserTopicEvent extends Event {
665665
}
666666

667667
/// A Zulip event of type `message`: https://zulip.com/api/get-events#message
668-
// TODO use [JsonSerializable] here too, using its customization features,
669-
// in order to skip the boilerplate in [fromJson] and [toJson].
668+
@JsonSerializable(fieldRename: FieldRename.snake)
670669
class MessageEvent extends Event {
671670
@override
672671
@JsonKey(includeToJson: true)
@@ -680,24 +679,23 @@ class MessageEvent extends Event {
680679
// events and in the get-messages results is that `matchContent` and
681680
// `matchTopic` are absent here. Already [Message.matchContent] and
682681
// [Message.matchTopic] are optional, so no action is needed on that.
682+
@JsonKey(readValue: _readMessageValue, includeToJson: false)
683683
final Message message;
684684

685685
MessageEvent({required super.id, required this.message});
686686

687-
factory MessageEvent.fromJson(Map<String, dynamic> json) => MessageEvent(
688-
id: json['id'] as int,
689-
message: Message.fromJson({
690-
...json['message'] as Map<String, dynamic>,
691-
'flags': (json['flags'] as List<dynamic>).map((e) => e as String).toList(),
692-
}),
693-
);
687+
static Map<String, dynamic> _readMessageValue(Map<dynamic, dynamic> json, String key) =>
688+
{...json['message'] as Map<String, dynamic>, 'flags': json['flags']};
689+
690+
factory MessageEvent.fromJson(Map<String, dynamic> json) =>
691+
_$MessageEventFromJson(json);
694692

695693
@override
696694
Map<String, dynamic> toJson() {
697695
final messageJson = message.toJson();
698696
final flags = messageJson['flags'];
699697
messageJson.remove('flags');
700-
return {'id': id, 'type': type, 'message': messageJson, 'flags': flags};
698+
return {..._$MessageEventToJson(this), 'message': messageJson, 'flags': flags};
701699
}
702700
}
703701

lib/api/model/events.g.dart

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)