public static JsonSerializerOptions AddCoreConverters(this JsonSerializerOptions options) { options.AddConverter <NullStringAsEmptyJsonConverter>(); options.AddConverter <NumericValueJsonConverterFactory>(); options.AddConverter <TextValueJsonConverterFactory>(); options.AddConverter <TimeSpanJsonConverter>(); return(options); }
/// <summary> /// Adds the JSON converters that handle gateway command payloads. /// </summary> /// <param name="options">The serializer options.</param> /// <returns>The options, with the converters added.</returns> private static JsonSerializerOptions AddGatewayCommandConverters(this JsonSerializerOptions options) { options.AddDataObjectConverter <IIdentify, Identify>(); options.AddDataObjectConverter <IConnectionProperties, ConnectionProperties>() .WithPropertyName(p => p.OperatingSystem, "$os") .WithPropertyName(p => p.Browser, "$browser") .WithPropertyName(p => p.Device, "$device"); options.AddConverter <ShardIdentificationConverter>(); options.AddDataObjectConverter <IRequestGuildMembers, RequestGuildMembers>(); options.AddDataObjectConverter <IResume, Resume>() .WithPropertyName(r => r.SequenceNumber, "seq"); options.AddDataObjectConverter <IUpdateStatus, UpdateStatus>() .WithPropertyName(u => u.IsAFK, "afk") .WithPropertyConverter ( u => u.Status, new StringEnumConverter <ClientStatus>(new SnakeCaseNamingPolicy()) ) .WithPropertyConverter(u => u.Since, new UnixDateTimeConverter()); options.AddDataObjectConverter <IUpdateVoiceState, UpdateVoiceState>() .WithPropertyName(u => u.IsSelfMuted, "self_mute") .WithPropertyName(u => u.IsSelfDeafened, "self_deaf"); return(options); }
/// <summary> /// Adds the JSON converters that handle interaction objects. /// </summary> /// <param name="options">The serializer options.</param> /// <returns>The options, with the converters added.</returns> private static JsonSerializerOptions AddInteractionObjectConverters(this JsonSerializerOptions options) { options.AddDataObjectConverter <IApplicationCommandInteractionData, ApplicationCommandInteractionData>(); options.AddDataObjectConverter < IApplicationCommandInteractionDataOption, ApplicationCommandInteractionDataOption >(); options.AddDataObjectConverter <IInteraction, Interaction>(); options.AddDataObjectConverter < IInteractionApplicationCommandCallbackData, InteractionApplicationCommandCallbackData >() .WithPropertyName(d => d.IsTTS, "tts"); options.AddDataObjectConverter <IInteractionResponse, InteractionResponse>(); options.AddDataObjectConverter <IApplicationCommand, ApplicationCommand>(); options.AddDataObjectConverter <IApplicationCommandOption, ApplicationCommandOption>() .WithPropertyName(o => o.IsDefault, "default") .WithPropertyName(o => o.IsRequired, "required"); options.AddDataObjectConverter <IApplicationCommandOptionChoice, ApplicationCommandOptionChoice>(); options.AddDataObjectConverter <IMessageInteraction, MessageInteraction>(); options.AddDataObjectConverter < IApplicationCommandInteractionDataResolved, ApplicationCommandInteractionDataResolved >() .WithPropertyConverter(r => r.Users, new SnowflakeDictionaryConverter <IUser>()) .WithPropertyConverter(r => r.Members, new SnowflakeDictionaryConverter <IPartialGuildMember>()) .WithPropertyConverter(r => r.Roles, new SnowflakeDictionaryConverter <IRole>()) .WithPropertyConverter(r => r.Channels, new SnowflakeDictionaryConverter <IPartialChannel>()); options.AddDataObjectConverter <IGuildApplicationCommandPermissions, GuildApplicationCommandPermissions>(); options.AddDataObjectConverter < IPartialGuildApplicationCommandPermissions, PartialGuildApplicationCommandPermissions >(); options.AddDataObjectConverter <IApplicationCommandPermissions, ApplicationCommandPermissions>() .WithPropertyName(p => p.HasPermission, "permission"); options.AddConverter <MessageComponentConverter>(); options.AddDataObjectConverter <IComponent, Component>() .WithPropertyName(c => c.IsDisabled, "disabled"); options.AddDataObjectConverter <IActionRowComponent, ActionRowComponent>(); options.AddDataObjectConverter <IButtonComponent, ButtonComponent>() .WithPropertyName(c => c.IsDisabled, "disabled"); options.AddDataObjectConverter <ISelectMenuComponent, SelectMenuComponent>() .WithPropertyName(c => c.IsDisabled, "disabled"); options.AddDataObjectConverter <ISelectOption, SelectOption>() .WithPropertyName(o => o.IsDefault, "default"); return(options); }
public static JsonSerializerOptions NormalizeDecimals(this JsonSerializerOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } Contract.EndContractBlock(); JsonConverter?existing = options.Converters.FirstOrDefault(c => c is JsonConverter <decimal>); var existingNullable = options.Converters.FirstOrDefault(c => c is JsonConverter <decimal?>); if (existing is JsonDecimalConverter && existingNullable is JsonNullableDecimalConverter) { return(options); } if (existing == null && existingNullable == null) { return(options .AddConverter(JsonDecimalConverter.Instance) .AddConverter(JsonNullableDecimalConverter.Instance)); } if (existing != null) { throw new InvalidOperationException("A specific decimal converter already exists."); } throw new InvalidOperationException("A specific Nullable<decimal> converter already exists."); }
/// <summary> /// Adds the JSON converters that handle bidirectional gateway payloads. /// </summary> /// <param name="options">The serializer options.</param> /// <returns>The options, with the converters added.</returns> private static JsonSerializerOptions AddGatewayBidirectionalConverters(this JsonSerializerOptions options) { options .AddConverter <HeartbeatConverter>() .AddDataObjectConverter <IHeartbeatAcknowledge, HeartbeatAcknowledge>(); return(options); }
public static JsonSerializerOptions AddCentComOptions(this JsonSerializerOptions options) { options .AddConverter <CKeyStringConverter>() .AddConverter <JobBanCollectionConverter>(); return(options); }
/// <summary> /// Adds the JSON converters that handle activity objects. /// </summary> /// <param name="options">The serializer options.</param> /// <returns>The options, with the converters added.</returns> private static JsonSerializerOptions AddActivityObjectConverters(this JsonSerializerOptions options) { options.AddDataObjectConverter <IActivity, Activity>(); options.AddDataObjectConverter <IActivityAssets, ActivityAssets>(); options.AddDataObjectConverter <IActivityEmoji, ActivityEmoji>(); options.AddDataObjectConverter <IActivityParty, ActivityParty>(); options.AddConverter <PartySizeConverter>(); options.AddDataObjectConverter <IActivitySecrets, ActivitySecrets>(); options.AddDataObjectConverter <IActivityTimestamps, ActivityTimestamps>() .WithPropertyConverter(t => t.Start, new UnixDateTimeConverter()) .WithPropertyConverter(t => t.End, new UnixDateTimeConverter()); return(options); }
static JsonSerializerOptions RoundDoublesCore(this JsonSerializerOptions options, int maxDecimals) { var existing = options.Converters.FirstOrDefault(c => c is JsonConverter <double>); if (existing == null) { return(options.AddConverter(new JsonDoubleRoundingConverter(maxDecimals))); } if (existing is JsonDoubleRoundingConverter e && e.Maximum == maxDecimals) { return(options); } throw new InvalidOperationException("A specific double converter already exists."); }
/// <summary> /// Adds the JSON converters that handle audit log objects. /// </summary> /// <param name="options">The serializer options.</param> /// <returns>The options, with the converters added.</returns> private static JsonSerializerOptions AddAuditLogObjectConverters(this JsonSerializerOptions options) { options.AddDataObjectConverter <IAuditLog, AuditLog>(); options.AddDataObjectConverter <IAuditLogEntry, AuditLogEntry>(); options.AddDataObjectConverter <IOptionalAuditEntryInfo, OptionalAuditEntryInfo>() .WithPropertyConverter ( ae => ae.Type, new StringEnumConverter <PermissionOverwriteType>(asInteger: true) ); options.AddConverter <AuditLogChangeConverter>(); return(options); }
static JsonSerializerOptions RoundNullableDecimalsCore(this JsonSerializerOptions options, int maxDecimals) { JsonConverter?existing = options.Converters.FirstOrDefault(c => c is JsonConverter <decimal?>); if (existing is JsonNullableDecimalConverter && existing.GetType() == typeof(JsonNullableDecimalConverter)) { options.Converters.Remove(existing); existing = null; } if (existing == null) { return(options.AddConverter(new JsonNullableDecimalRoundingConverter(maxDecimals))); } if (existing is JsonNullableDecimalRoundingConverter e && e.Maximum == maxDecimals) { return(options); } throw new InvalidOperationException("A specific Nullable<decimal> converter already exists."); }
/// <summary> /// Adds the JSON converters that handle permission objects. /// </summary> /// <param name="options">The serializer options.</param> /// <returns>The options, with the converters added.</returns> private static JsonSerializerOptions AddPermissionObjectConverters(this JsonSerializerOptions options) { options.AddConverter <DiscordPermissionSetConverter>(); options.AddDataObjectConverter <IPermissionOverwrite, PermissionOverwrite>(); options.AddDataObjectConverter <IRole, Role>() .WithPropertyName(r => r.Colour, "color") .WithPropertyName(r => r.IsHoisted, "hoist") .WithPropertyName(r => r.IsManaged, "managed") .WithPropertyName(r => r.IsMentionable, "mentionable"); options.AddDataObjectConverter <IPartialRole, PartialRole>() .WithPropertyName(r => r.Colour, "color") .WithPropertyName(r => r.IsHoisted, "hoist") .WithPropertyName(r => r.IsManaged, "managed") .WithPropertyName(r => r.IsMentionable, "mentionable"); options.AddDataObjectConverter <IRoleTags, RoleTags>() .WithPropertyName(t => t.IsPremiumSubscriberRole, "premium_subscriber"); return(options); }
/// <summary> /// Adds the JSON converters that handle image objects. /// </summary> /// <param name="options">The serializer options.</param> /// <returns>The options, with the converters added.</returns> private static JsonSerializerOptions AddImageObjectConverters(this JsonSerializerOptions options) { options.AddConverter <ImageHashConverter>(); return(options); }
/// <summary> /// Adds the JSON converters that handle gateway event payloads. /// </summary> /// <param name="options">The serializer options.</param> /// <returns>The options, with the converters added.</returns> private static JsonSerializerOptions AddGatewayEventConverters(this JsonSerializerOptions options) { // Connecting and resuming options.AddConverter <InvalidSessionConverter>(); options.AddDataObjectConverter <IHello, Hello>() .WithPropertyConverter(h => h.HeartbeatInterval, new UnitTimeSpanConverter(TimeUnit.Milliseconds)); options.AddDataObjectConverter <IReady, Ready>() .WithPropertyName(r => r.Version, "v"); options.AddDataObjectConverter <IReconnect, Reconnect>(); options.AddDataObjectConverter <IResumed, Resumed>(); // Channels options.AddDataObjectConverter <IChannelCreate, ChannelCreate>() .WithPropertyName(c => c.IsNsfw, "nsfw") .WithPropertyConverter(c => c.RateLimitPerUser, new UnitTimeSpanConverter(TimeUnit.Seconds)); options.AddDataObjectConverter <IChannelUpdate, ChannelUpdate>() .WithPropertyName(c => c.IsNsfw, "nsfw") .WithPropertyConverter(c => c.RateLimitPerUser, new UnitTimeSpanConverter(TimeUnit.Seconds)); options.AddDataObjectConverter <IChannelDelete, ChannelDelete>() .WithPropertyName(c => c.IsNsfw, "nsfw") .WithPropertyConverter(c => c.RateLimitPerUser, new UnitTimeSpanConverter(TimeUnit.Seconds)); options.AddDataObjectConverter <IChannelPinsUpdate, ChannelPinsUpdate>(); // Guilds options.AddDataObjectConverter <IGuildCreate, GuildCreate>() .WithPropertyName(g => g.IsOwner, "owner") .WithPropertyName(g => g.GuildFeatures, "features") .WithPropertyConverter ( g => g.GuildFeatures, new StringEnumConverter <GuildFeature>(new SnakeCaseNamingPolicy(true)) ) .WithPropertyName(g => g.IsLarge, "large") .WithPropertyName(g => g.IsUnavailable, "unavailable") .WithPropertyName(g => g.IsWidgetEnabled, "widget_enabled") .WithPropertyConverter(g => g.AFKTimeout, new UnitTimeSpanConverter(TimeUnit.Seconds)); options.AddDataObjectConverter <IGuildUpdate, GuildUpdate>() .WithPropertyName(g => g.IsOwner, "owner") .WithPropertyName(g => g.GuildFeatures, "features") .WithPropertyConverter ( g => g.GuildFeatures, new StringEnumConverter <GuildFeature>(new SnakeCaseNamingPolicy(true)) ) .WithPropertyName(g => g.IsLarge, "large") .WithPropertyName(g => g.IsUnavailable, "unavailable") .WithPropertyName(g => g.IsWidgetEnabled, "widget_enabled") .WithPropertyConverter(g => g.AFKTimeout, new UnitTimeSpanConverter(TimeUnit.Seconds)); options.AddDataObjectConverter <IGuildDelete, GuildDelete>() .WithPropertyName(d => d.IsUnavailable, "unavailable"); options.AddDataObjectConverter <IGuildBanAdd, GuildBanAdd>(); options.AddDataObjectConverter <IGuildBanRemove, GuildBanRemove>(); options.AddDataObjectConverter <IGuildEmojisUpdate, GuildEmojisUpdate>(); options.AddDataObjectConverter <IGuildIntegrationsUpdate, GuildIntegrationsUpdate>(); options.AddDataObjectConverter <IGuildMemberAdd, GuildMemberAdd>() .WithPropertyName(m => m.Nickname, "nick") .WithPropertyName(m => m.IsDeafened, "deaf") .WithPropertyName(m => m.IsMuted, "mute") .WithPropertyName(m => m.IsPending, "pending"); options.AddDataObjectConverter <IGuildMemberRemove, GuildMemberRemove>(); options.AddDataObjectConverter <IGuildMemberUpdate, GuildMemberUpdate>() .WithPropertyName(u => u.Nickname, "nick") .WithPropertyName(u => u.IsPending, "pending"); options.AddDataObjectConverter <IGuildMembersChunk, GuildMembersChunk>(); options.AddDataObjectConverter <IGuildRoleCreate, GuildRoleCreate>(); options.AddDataObjectConverter <IGuildRoleUpdate, GuildRoleUpdate>(); options.AddDataObjectConverter <IGuildRoleDelete, GuildRoleDelete>(); // Invites options.AddDataObjectConverter <IInviteCreate, InviteCreate>() .WithPropertyName(c => c.IsTemporary, "temporary") .WithPropertyConverter(c => c.MaxAge, new UnitTimeSpanConverter(TimeUnit.Seconds)); options.AddDataObjectConverter <IInviteDelete, InviteDelete>(); // Messages options.AddDataObjectConverter <IMessageCreate, MessageCreate>() .WithPropertyName(m => m.MentionsEveryone, "mention_everyone") .WithPropertyName(m => m.MentionedRoles, "mention_roles") .WithPropertyName(m => m.MentionedChannels, "mention_channels") .WithPropertyName(m => m.IsTTS, "tts") .WithPropertyName(m => m.IsPinned, "pinned"); options.AddDataObjectConverter <IMessageUpdate, MessageUpdate>() .WithPropertyName(m => m.MentionsEveryone, "mention_everyone") .WithPropertyName(m => m.MentionedRoles, "mention_roles") .WithPropertyName(m => m.MentionedChannels, "mention_channels") .WithPropertyName(m => m.IsTTS, "tts") .WithPropertyName(m => m.IsPinned, "pinned"); options.AddDataObjectConverter <IMessageDelete, MessageDelete>(); options.AddDataObjectConverter <IMessageDeleteBulk, MessageDeleteBulk>() .WithPropertyName(d => d.MessageIDs, "ids"); options.AddDataObjectConverter <IMessageReactionAdd, MessageReactionAdd>(); options.AddDataObjectConverter <IMessageReactionRemove, MessageReactionRemove>(); options.AddDataObjectConverter <IMessageReactionRemoveAll, MessageReactionRemoveAll>(); options.AddDataObjectConverter <IMessageReactionRemoveEmoji, MessageReactionRemoveEmoji>(); // Presences options.AddDataObjectConverter <IPresenceUpdate, PresenceUpdate>() .WithPropertyConverter(p => p.Status, new StringEnumConverter <ClientStatus>(new SnakeCaseNamingPolicy())); // Users options.AddDataObjectConverter <ITypingStart, TypingStart>(); options.AddDataObjectConverter <IUserUpdate, UserUpdate>() .WithPropertyName(u => u.IsBot, "bot") .WithPropertyName(u => u.IsSystem, "system") .WithPropertyName(u => u.IsVerified, "verified") .WithPropertyName(u => u.IsMFAEnabled, "mfa_enabled"); // Voice options.AddDataObjectConverter <IVoiceStateUpdate, VoiceStateUpdate>() .WithPropertyName(v => v.IsDeafened, "deaf") .WithPropertyName(v => v.IsMuted, "mute") .WithPropertyName(v => v.IsSelfDeafened, "self_deaf") .WithPropertyName(v => v.IsSelfMuted, "self_mute") .WithPropertyName(v => v.IsStreaming, "self_stream") .WithPropertyName(v => v.IsVideoEnabled, "self_video") .WithPropertyName(v => v.IsSuppressed, "suppress"); options.AddDataObjectConverter <IVoiceServerUpdate, VoiceServerUpdate>(); // Webhooks options.AddDataObjectConverter <IWebhooksUpdate, WebhooksUpdate>(); // Interactions options.AddDataObjectConverter <IInteractionCreate, InteractionCreate>(); options.AddDataObjectConverter <IApplicationCommandCreate, ApplicationCommandCreate>(); options.AddDataObjectConverter <IApplicationCommandUpdate, ApplicationCommandUpdate>(); options.AddDataObjectConverter <IApplicationCommandDelete, ApplicationCommandDelete>(); // Other options.AddDataObjectConverter <IUnknownEvent, UnknownEvent>(); return(options); }