Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
e285e2e
WIP STJ Support
Hawxy Jul 29, 2022
059bd4b
Handle null flag
Hawxy Jul 29, 2022
be3c10e
Fix build
Hawxy Jul 29, 2022
bfe1215
Support optional
Hawxy Jul 29, 2022
9d64b79
Fixed and expanded upon existing code.
Quahu Aug 1, 2022
8a34d12
Added some TODOs.
Quahu Aug 1, 2022
00cf546
Implemented STJ
Quahu Jul 29, 2024
9c053b9
Fix STJ polymorphism issue
Quahu Jul 29, 2024
69859ca
Add extension data support
Quahu Jul 29, 2024
6c3b560
Fix ToJsonString() in dispatch error log
Quahu Jul 29, 2024
a90ed07
API adjustments
Quahu Jul 29, 2024
e23c621
Fix extension data not being deserialized
Quahu Jul 30, 2024
cc60082
More adjustments
Quahu Jul 30, 2024
4b6c07b
Make nodes internal
Quahu Jul 30, 2024
c20c720
Refactor converters
Quahu Jul 30, 2024
82389a4
Disable STJ impl for .NET 6
Quahu Jul 30, 2024
5d291be
Add conditional serializer replacement to TestBot
Quahu Jul 30, 2024
246e56b
Made STJ nodes fully internal
Quahu Jul 30, 2024
12c283f
Fix TestBot build
Quahu Jul 30, 2024
2f9d927
Fix BindOptions for STJ. API adjustments
Quahu Aug 14, 2024
46e5bd4
Add Type to IJsonNode
Quahu Aug 24, 2024
4a9fb46
Fix VOICE_STATE_UPDATE lurker check
Quahu Aug 24, 2024
e0a0933
Ensure GUILD_CREATE and GUILD_DELETE pop pending guild even on failure
Quahu Aug 24, 2024
809485c
Add Path to IJsonNode
Quahu Aug 24, 2024
bd81efe
Fix loss of JSON information in converter exceptions
Quahu Aug 24, 2024
63d9c6f
Add common extension for deserializing JSON arrays
Quahu Aug 24, 2024
28cd56b
Safely deserialize gateway guild's members
Quahu Aug 24, 2024
a60ebfa
Update STJ
Quahu Aug 24, 2024
49d47e7
Merge branch 'master' into stj
Quahu Apr 22, 2025
931deb1
Adjust Program
Quahu Apr 22, 2025
66483d5
Fix string enum conversion of OverwriteTargetType
Quahu Apr 22, 2025
7b1731f
Remove NET 8 checks
Quahu Apr 22, 2025
81c34e2
Check HasValueSequence in StringEnumConverterImpl
Quahu Apr 22, 2025
cb3ea2f
Adjust exception rethrowing
Quahu Apr 23, 2025
a2666ce
Move MaxSafeInteger to JsonUtilities
Quahu Apr 23, 2025
0ee095e
Make STJ default, put Newtonsoft in legacy package
Quahu Apr 24, 2025
01bb6e8
Remove 'Entities' folders for JSON code
Quahu Apr 26, 2025
f506849
Merge branch 'master' into stj
Quahu May 27, 2025
b316699
Adjust converter namespaces after merge
Quahu May 27, 2025
ecc5fa9
Rename JsonSerializeOptions to JsonSerializationOptions
Quahu May 27, 2025
e19ad52
Remove Newtonsoft.Json from Core
Quahu May 27, 2025
0298d62
Clean up explicit STJ namespaces
Quahu May 27, 2025
4e2ec12
Add missing Newtonsoft.Json package reference
Quahu May 27, 2025
3c34cf7
Add STJ support for components V2
Quahu May 27, 2025
dee6791
Fix voice serialization with STJ
Quahu Jun 13, 2025
0b76245
Add support for deserialization with ReferenceHandler.Preserve option…
Junko-Ganan Aug 2, 2025
a721115
Merge branch 'master' into stj
Quahu Aug 30, 2025
bdaa8b8
Implement interaction and modal component converters for STJ
Quahu Aug 31, 2025
5f5f62a
Merge branch 'master' into stj
Quahu Feb 25, 2026
23fdfff
Merge branch 'master' into stj
Quahu Jun 4, 2026
cd0eef5
Update STJ for new components and Newtonsoft.Json parity
Quahu Jun 4, 2026
26bdb5c
Add missing ComponentType handling in legacy ComponentConverter
Quahu Jun 6, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Disqord.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<Project Path="src/Disqord.Rest.Api/Disqord.Rest.Api.csproj" />
<Project Path="src/Disqord.Rest/Disqord.Rest.csproj" />
</Folder>
<Folder Name="/Source/Core/Serialization/" />
<Folder Name="/Source/Core/Serialization/Json/">
<Project Path="src/Disqord.Serialization.Json.Newtonsoft/Disqord.Serialization.Json.Newtonsoft.csproj" />
</Folder>
<Folder Name="/Source/Core/Voice/">
<Project Path="src/Disqord.Voice.Api/Disqord.Voice.Api.csproj" />
<Project Path="src/Disqord.Voice/Disqord.Voice.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Disqord.Serialization.Json;
using Qmmands;
using Qommon;

Expand Down Expand Up @@ -84,15 +85,17 @@ static IReadOnlyDictionary<string, ISlashCommandInteractionOption> GetArguments(
continue;
}

if (option.Value is not string stringValue)
if (option.Value.Type != JsonValueType.String)
{
var value = GetOptionValue(option);
arguments[parameter] = actualType.IsEnum
? Enum.ToObject(actualType, option.Value)
: Convert.ChangeType(option.Value, actualType, context.Locale);
? Enum.ToObject(actualType, value)
: Convert.ChangeType(value, actualType, context.Locale);

continue;
}

var stringValue = option.Value.ToType<string>()!;
if (interaction is IAutoCompleteInteraction)
{
// Treat string values as arguments for auto-complete.
Expand All @@ -105,7 +108,7 @@ static IReadOnlyDictionary<string, ISlashCommandInteractionOption> GetArguments(
{
// If the option is just a string, pass it through to type parsing.
var rawArguments = context.RawArguments ??= new Dictionary<IParameter, MultiString>();
rawArguments[parameter] = option.Value as string;
rawArguments[parameter] = stringValue;
continue;
}

Expand Down Expand Up @@ -134,5 +137,30 @@ static IReadOnlyDictionary<string, ISlashCommandInteractionOption> GetArguments(

return Next.ExecuteAsync(context);
}

private static object GetOptionValue(ISlashCommandInteractionOption option)
{
var value = option.Value!;
switch (option.Type)
{
case SlashCommandOptionType.Integer:
{
return value.ToType<long>();
}
case SlashCommandOptionType.Boolean:
{
return value.ToType<bool>();
}
case SlashCommandOptionType.Number:
{
return value.ToType<double>();
}
default:
{
Throw.ArgumentOutOfRangeException(nameof(option));
return default;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public async ValueTask LocalizeAsync(IEnumerable<LocalApplicationCommand> global
var temporaryFileName = string.Format(CultureInfo.InvariantCulture, @this.TemporaryFileNameFormat, storeInformation.Locale.Name);
var temporaryFilePath = Path.Join(@this.DirectoryPath, temporaryFileName);
var memoryStream = storeInformation.MemoryStream;
@this.Serializer.Serialize(memoryStream, storeInformation.Model, new DefaultJsonSerializerOptions
@this.Serializer.Serialize(memoryStream, storeInformation.Model, new JsonSerializationOptions
{
Formatting = JsonFormatting.Indented
});
Expand Down
1 change: 1 addition & 0 deletions src/Disqord.Core/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[assembly: InternalsVisibleTo("Disqord.Rest.Api")]
[assembly: InternalsVisibleTo("Disqord.Gateway")]
[assembly: InternalsVisibleTo("Disqord.Gateway.Api")]
[assembly: InternalsVisibleTo("Disqord.Tests")]
[assembly: InternalsVisibleTo("Disqord.TestBot")]
[assembly: InternalsVisibleTo("Disqord.Voice")]
[assembly: InternalsVisibleTo("Disqord.Voice.Api")]
Expand Down
2 changes: 1 addition & 1 deletion src/Disqord.Core/Disqord.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1"/>
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
<PackageReference Include="Qommon" Version="4.0.1"/>
<PackageReference Include="System.Text.Json" Version="9.0.0"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Disqord.Serialization.Json;

namespace Disqord;

Expand All @@ -23,7 +24,7 @@ public interface ISlashCommandInteractionOption
/// <remarks>
/// <see cref="Value"/> and <see cref="Options"/> are mutually exclusive.
/// </remarks>
object? Value { get; }
IJsonValue? Value { get; }

/// <summary>
/// Gets the nested options of this option.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public IReadOnlyDictionary<CultureInfo, string> NameLocalizations
}

/// <inheritdoc/>
public object Value => Model.Value.Value!;
public object Value => Model.Value.ToType<object>()!;

public TransientSlashCommandOptionChoice(IClient client, ApplicationCommandOptionChoiceJsonModel model)
: base(client, model)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Disqord.Models;
using Disqord.Serialization.Json;
using Qommon;
using Qommon.Collections.ReadOnly;

Expand All @@ -15,7 +16,7 @@ public class TransientSlashCommandInteractionOption : TransientClientEntity<Appl
public SlashCommandOptionType Type => Model.Type;

/// <inheritdoc/>
public object? Value => Model.Value.GetValueOrDefault()?.Value;
public IJsonValue? Value => Model.Value.GetValueOrDefault();

/// <inheritdoc/>
public IReadOnlyDictionary<string, ISlashCommandInteractionOption> Options
Expand Down
6 changes: 3 additions & 3 deletions src/Disqord.Core/Enums/OverwriteTargetType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public enum OverwriteTargetType : byte
/// The overwrite targets a role.
/// </summary>
[EnumMember(Value = "role")]
Role,
Role = 0,

/// <summary>
/// The overwrite targets a member.
/// </summary>
[EnumMember(Value = "member")]
Member
}
Member = 1
}
2 changes: 2 additions & 0 deletions src/Disqord.Core/Library/Library.Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public static partial class Library
[EditorBrowsable(EditorBrowsableState.Never)]
public static class Debug
{
public static bool LogSafeDeserializationExceptions;

public static bool DumpJson;

public static TextWriter DumpWriter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ protected override void OnValidate()
Guard.HasSizeBetweenOrEqualTo(Name, Discord.Limits.ApplicationCommand.Option.Choice.MinNameLength, Discord.Limits.ApplicationCommand.Option.Choice.MaxNameLength);

Guard.IsNotNull(Value);
Guard.IsNotNull(Value.Value);

var value = Guard.IsAssignableToType<IConvertible>(Value.Value, nameof(Value));
var objectValue = Value.ToType<object>();
Guard.IsNotNull(objectValue);

var value = Guard.IsAssignableToType<IConvertible>(objectValue, nameof(Value));
switch (value.GetTypeCode())
{
case TypeCode.SByte:
Expand Down Expand Up @@ -62,4 +64,4 @@ protected override void OnValidate()
}
}
}
}
}
38 changes: 38 additions & 0 deletions src/Disqord.Core/Models/Extensions/JsonArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Disqord.Serialization.Json;
using Microsoft.Extensions.Logging;

namespace Disqord.Models;

internal static class JsonArrayExtensions
{
public static IEnumerable<TJsonModel> SafelyDeserializeItems<TJsonModel>(this IJsonArray jsonArray, ILogger logger, [CallerArgumentExpression(nameof(jsonArray))] string? caller = null)
where TJsonModel : JsonModel
{
var count = jsonArray.Count;
for (var i = 0; i < count; i++)
{
TJsonModel? model = null;
try
{
model = jsonArray[i]?.ToType<TJsonModel>();
}
catch (Exception ex)
{
if (Library.Debug.LogSafeDeserializationExceptions)
{
logger.LogWarning(ex, "Failed to deserialize {Caller}.", caller);
}
}

if (model == null)
{
continue;
}

yield return model;
}
}
}
Loading
Loading