From 3e6fc826ee144768af2d58cb809d84d790f1f6d3 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Tue, 4 Apr 2023 18:09:08 +0530 Subject: [PATCH] chore(llc): json annotation changes Signed-off-by: xsahil03x --- .../lib/src/core/models/attachment.dart | 1 - .../lib/src/core/models/channel_model.dart | 1 - .../lib/src/core/models/message.dart | 9 ++--- .../lib/src/core/models/message.g.dart | 40 ++++++------------- .../lib/src/core/models/reaction.dart | 1 - .../stream_chat/lib/src/core/models/user.dart | 1 - .../lib/src/core/util/serializer.dart | 7 ---- 7 files changed, 17 insertions(+), 43 deletions(-) diff --git a/packages/stream_chat/lib/src/core/models/attachment.dart b/packages/stream_chat/lib/src/core/models/attachment.dart index c757a174..bceaaacc 100644 --- a/packages/stream_chat/lib/src/core/models/attachment.dart +++ b/packages/stream_chat/lib/src/core/models/attachment.dart @@ -135,7 +135,6 @@ class Attachment extends Equatable { late final UploadState uploadState; /// Map of custom channel extraData - @JsonKey(includeIfNull: false) final Map extraData; /// The attachment ID. diff --git a/packages/stream_chat/lib/src/core/models/channel_model.dart b/packages/stream_chat/lib/src/core/models/channel_model.dart index b3523743..00fa1fcc 100644 --- a/packages/stream_chat/lib/src/core/models/channel_model.dart +++ b/packages/stream_chat/lib/src/core/models/channel_model.dart @@ -122,7 +122,6 @@ class ChannelModel { } /// Map of custom channel extraData - @JsonKey(includeIfNull: false) final Map extraData; /// The team the channel belongs to diff --git a/packages/stream_chat/lib/src/core/models/message.dart b/packages/stream_chat/lib/src/core/models/message.dart index 6f3a5b13..f8631e1c 100644 --- a/packages/stream_chat/lib/src/core/models/message.dart +++ b/packages/stream_chat/lib/src/core/models/message.dart @@ -131,7 +131,7 @@ class Message extends Equatable { final String? parentId; /// A quoted reply message. - @JsonKey(toJson: Serializer.readOnly) + @JsonKey(includeToJson: false) final Message? quotedMessage; final String? _quotedMessageId; @@ -185,7 +185,7 @@ class Message extends Equatable { final bool pinned; /// Reserved field indicating when the message was pinned. - @JsonKey(toJson: Serializer.readOnly) + @JsonKey(includeToJson: false) final DateTime? pinnedAt; /// Reserved field indicating when the message will expire. @@ -194,11 +194,10 @@ class Message extends Equatable { final DateTime? pinExpires; /// Reserved field indicating who pinned the message. - @JsonKey(toJson: Serializer.readOnly) + @JsonKey(includeToJson: false) final User? pinnedBy; /// Message custom extraData. - @JsonKey(includeIfNull: false) final Map extraData; /// True if the message is a system info. @@ -211,7 +210,7 @@ class Message extends Equatable { bool get isEphemeral => type == 'ephemeral'; /// A Map of translations. - @JsonKey(includeIfNull: false) + @JsonKey(includeToJson: false) final Map? i18n; /// Known top level fields. diff --git a/packages/stream_chat/lib/src/core/models/message.g.dart b/packages/stream_chat/lib/src/core/models/message.g.dart index cbf849cc..506ac7a9 100644 --- a/packages/stream_chat/lib/src/core/models/message.g.dart +++ b/packages/stream_chat/lib/src/core/models/message.g.dart @@ -71,30 +71,16 @@ Message _$MessageFromJson(Map json) => Message( ), ); -Map _$MessageToJson(Message instance) { - final val = { - 'id': instance.id, - 'text': instance.text, - 'attachments': instance.attachments.map((e) => e.toJson()).toList(), - 'mentioned_users': User.toIds(instance.mentionedUsers), - 'parent_id': instance.parentId, - 'quoted_message': readonly(instance.quotedMessage), - 'quoted_message_id': instance.quotedMessageId, - 'show_in_channel': instance.showInChannel, - 'silent': instance.silent, - 'pinned': instance.pinned, - 'pinned_at': readonly(instance.pinnedAt), - 'pin_expires': instance.pinExpires?.toIso8601String(), - 'pinned_by': readonly(instance.pinnedBy), - 'extra_data': instance.extraData, - }; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('i18n', instance.i18n); - return val; -} +Map _$MessageToJson(Message instance) => { + 'id': instance.id, + 'text': instance.text, + 'attachments': instance.attachments.map((e) => e.toJson()).toList(), + 'mentioned_users': User.toIds(instance.mentionedUsers), + 'parent_id': instance.parentId, + 'quoted_message_id': instance.quotedMessageId, + 'show_in_channel': instance.showInChannel, + 'silent': instance.silent, + 'pinned': instance.pinned, + 'pin_expires': instance.pinExpires?.toIso8601String(), + 'extra_data': instance.extraData, + }; diff --git a/packages/stream_chat/lib/src/core/models/reaction.dart b/packages/stream_chat/lib/src/core/models/reaction.dart index 2bc6f52b..474b6dff 100644 --- a/packages/stream_chat/lib/src/core/models/reaction.dart +++ b/packages/stream_chat/lib/src/core/models/reaction.dart @@ -48,7 +48,6 @@ class Reaction { final String? userId; /// Reaction custom extraData - @JsonKey(includeIfNull: false) final Map extraData; /// Map of custom user extraData diff --git a/packages/stream_chat/lib/src/core/models/user.dart b/packages/stream_chat/lib/src/core/models/user.dart index ca60e614..a280d18c 100644 --- a/packages/stream_chat/lib/src/core/models/user.dart +++ b/packages/stream_chat/lib/src/core/models/user.dart @@ -131,7 +131,6 @@ class User extends Equatable { final String? language; /// Map of custom user extraData. - @JsonKey(includeIfNull: false) final Map extraData; /// List of users to list of userIds. diff --git a/packages/stream_chat/lib/src/core/util/serializer.dart b/packages/stream_chat/lib/src/core/util/serializer.dart index 2bf92e39..9c1cb71f 100644 --- a/packages/stream_chat/lib/src/core/util/serializer.dart +++ b/packages/stream_chat/lib/src/core/util/serializer.dart @@ -1,12 +1,5 @@ -/// Used to avoid to serialize properties to json -// ignore: prefer_void_to_null -Null readonly(_) => null; - /// Helper class for serialization to and from json class Serializer { - /// Used to avoid to serialize properties to json - static const Function readOnly = readonly; - /// Takes unknown json keys and puts them in the `extra_data` key static Map moveToExtraDataFromRoot( Map json,