Skip to content

Commit

Permalink
Implemented STJ
Browse files Browse the repository at this point in the history
  • Loading branch information
Quahu committed Jul 29, 2024
1 parent 8a34d12 commit 00cf546
Show file tree
Hide file tree
Showing 35 changed files with 821 additions and 316 deletions.
2 changes: 1 addition & 1 deletion src/Disqord.Core/Disqord.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
<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="7.0.0-preview.6.22324.4" />
<PackageReference Include="System.Text.Json" Version="9.0.0-preview.6.24327.7" />
</ItemGroup>
</Project>
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.GetValue<object>()!;

public TransientSlashCommandOptionChoice(IClient client, ApplicationCommandOptionChoiceJsonModel model)
: base(client, model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TransientSlashCommandInteractionOption : TransientClientEntity<Appl
public SlashCommandOptionType Type => Model.Type;

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

/// <inheritdoc/>
public IReadOnlyDictionary<string, ISlashCommandInteractionOption> Options
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.GetValue<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()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public virtual void Serialize(Stream stream, object obj, IJsonSerializerOptions?
public virtual IJsonNode GetJsonNode(object? obj)
{
if (obj == null)
{
return DefaultJsonNode.Create(JValue.CreateNull(), UnderlyingSerializer);
}

return DefaultJsonNode.Create(JToken.FromObject(obj, UnderlyingSerializer), UnderlyingSerializer);
}
Expand Down Expand Up @@ -145,4 +147,4 @@ public static JsonTextWriter Conditional(IJsonSerializerOptions? options, TextWr
return new JsonTextWriter(writer);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected override JsonObjectContract CreateObjectContract(Type objectType)
{
null => null,
JToken jToken => DefaultJsonNode.Create(jToken, _serializer.UnderlyingSerializer),
_ => DefaultJsonNode.Create(JToken.FromObject(value, _serializer.UnderlyingSerializer), _serializer.UnderlyingSerializer)
_ => DefaultJsonNode.Create(value, _serializer.UnderlyingSerializer)
};
model.ExtensionData.Add(key, node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,70 @@ public class DefaultJsonArray : DefaultJsonNode, IJsonArray
public int Count => Token.Count;

/// <inheritdoc/>
public IJsonNode? this[int index] => Create(Token[index], Serializer);
public IJsonNode? this[int index]
{
get => Create(Token[index], Serializer);
set => Token[index] = GetJToken(value)!;
}

bool ICollection<IJsonNode?>.IsReadOnly => false;

public DefaultJsonArray(JArray token, JsonSerializer serializer)
: base(token, serializer)
{ }

/// <inheritdoc/>
public void Add(IJsonNode? item)
{
Token.Add(GetJToken(item)!);
}

/// <inheritdoc/>
public void Clear()
{
Token.Clear();
}

/// <inheritdoc/>
public bool Contains(IJsonNode? item)
{
return Token.Contains(GetJToken(item)!);
}

/// <inheritdoc/>
public void CopyTo(IJsonNode?[] array, int arrayIndex)
{
var count = Count;
for (var i = 0; i < count; i++)
{
array[arrayIndex + i] = this[i];
}
}

/// <inheritdoc/>
public bool Remove(IJsonNode? item)
{
return Token.Remove(GetJToken(item)!);
}

/// <inheritdoc/>
public int IndexOf(IJsonNode? item)
{
return Token.IndexOf(GetJToken(item)!);
}

/// <inheritdoc/>
public void Insert(int index, IJsonNode? item)
{
Token.Insert(index, GetJToken(item)!);
}

/// <inheritdoc/>
public void RemoveAt(int index)
{
Token.RemoveAt(index);
}

/// <inheritdoc/>
public IEnumerator<IJsonNode?> GetEnumerator()
{
Expand Down Expand Up @@ -72,4 +130,4 @@ public void Reset()
public void Dispose()
{ }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Qommon;

namespace Disqord.Serialization.Json.Default;

Expand All @@ -16,7 +17,10 @@ public class DefaultJsonNode : IJsonNode
/// </summary>
public JToken Token { get; }

private protected readonly JsonSerializer Serializer;
/// <summary>
/// Gets the underlying serializer.
/// </summary>
public JsonSerializer Serializer { get; }

public DefaultJsonNode(JToken token, JsonSerializer serializer)
{
Expand All @@ -37,34 +41,27 @@ public DefaultJsonNode(JToken token, JsonSerializer serializer)
/// <returns>
/// The string representing this node.
/// </returns>
public string ToString(Formatting formatting)
{
return Token.ToString(formatting);
}

/// <summary>
/// Formats this node into an indented JSON representation.
/// </summary>
/// <returns>
/// The string representing this node.
/// </returns>
public override string ToString()
public string ToJsonString(JsonFormatting formatting)
{
return Token.ToString(Formatting.Indented);
return Token.ToString(formatting switch
{
JsonFormatting.Indented => Formatting.Indented,
_ => Formatting.None
});
}

/// <summary>
/// Creates a new <see cref="DefaultJsonNode"/> from the specified object.
/// </summary>
/// <param name="obj"> The object to create the node for. </param>
/// <param name="serializer"> The default JSON serializer. </param>
/// <param name="obj"> The object to create the node for. </param>
/// <returns>
/// A JSON node representing the object.
/// </returns>
public static IJsonNode? Create(object? obj, DefaultJsonSerializer serializer)
public static IJsonNode? Create(object? obj, JsonSerializer serializer)
{
var token = obj != null ? JToken.FromObject(obj) : JValue.CreateNull();
return Create(token, serializer.UnderlyingSerializer);
var token = obj != null ? JToken.FromObject(obj, serializer) : JValue.CreateNull();
return Create(token, serializer);
}

[return: NotNullIfNotNull("token")]
Expand All @@ -79,4 +76,12 @@ public override string ToString()
_ => throw new InvalidOperationException("Unknown JSON token type.")
};
}
}

[return: NotNullIfNotNull("node")]
internal static JToken? GetJToken(IJsonNode? node)
{
return node != null
? Guard.IsAssignableToType<DefaultJsonNode>(node).Token
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,64 @@ public class DefaultJsonObject : DefaultJsonNode, IJsonObject
public int Count => Token.Count;

/// <inheritdoc/>
public IEnumerable<string> Keys => (Token as IDictionary<string, JToken>).Keys;
public ICollection<string> Keys => (Token as IDictionary<string, JToken>).Keys;

/// <inheritdoc/>
public IEnumerable<IJsonNode?> Values => (Token as IDictionary<string, JToken>).Values.Select(x => Create(x, Serializer));
public ICollection<IJsonNode?> Values => (Token as IDictionary<string, JToken>).Values.Select(value => Create(value, Serializer)).ToArray();

/// <inheritdoc/>
public IJsonNode? this[string key] => Create(Token[key], Serializer);
public IJsonNode? this[string key]
{
get => Create(Token[key], Serializer);
set => Token[key] = GetJToken(value);
}

bool ICollection<KeyValuePair<string, IJsonNode?>>.IsReadOnly => false;

public DefaultJsonObject(JObject token, JsonSerializer serializer)
: base(token, serializer)
{ }

/// <inheritdoc/>
public void Add(KeyValuePair<string, IJsonNode?> item)
{
Add(item.Key, item.Value);
}

/// <inheritdoc/>
public void Clear()
{
Token.RemoveAll();
}

/// <inheritdoc/>
public bool Contains(KeyValuePair<string, IJsonNode?> item)
{
return TryGetValue(item.Key, out var value) && ReferenceEquals(value, item.Value);
}

/// <inheritdoc/>
public void CopyTo(KeyValuePair<string, IJsonNode?>[] array, int arrayIndex)
{
var index = 0;
foreach (var (key, value) in this)
{
array[arrayIndex + index++] = KeyValuePair.Create(key, value);
}
}

/// <inheritdoc/>
public bool Remove(KeyValuePair<string, IJsonNode?> item)
{
return Remove(item.Key);
}

/// <inheritdoc/>
public void Add(string key, IJsonNode? value)
{
Token.Add(key, GetJToken(value));
}

/// <inheritdoc/>
public bool ContainsKey(string key)
{
Expand All @@ -50,6 +96,12 @@ public bool TryGetValue(string key, out IJsonNode? value)
return false;
}

/// <inheritdoc/>
public bool Remove(string key)
{
return Token.Remove(key);
}

private sealed class Enumerator : IEnumerator<KeyValuePair<string, IJsonNode?>>
{
public KeyValuePair<string, IJsonNode?> Current => KeyValuePair.Create(_enumerator.Current.Key, Create(_enumerator.Current.Value, _serializer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ public class DefaultJsonValue : DefaultJsonNode, IJsonValue
/// <inheritdoc cref="DefaultJsonNode.Token"/>
public new JValue Token => (base.Token as JValue)!;

/// <inheritdoc/>
public object? Value
{
get => Token.Value;
set => Token.Value = value;
}

public DefaultJsonValue(JValue token, JsonSerializer serializer)
: base(token, serializer)
{ }

/// <inheritdoc/>
public T? GetValue<T>()
{
return Token.Value<T>();
}

/// <inheritdoc/>
public override string ToString()
{
return Token.ToString(CultureInfo.InvariantCulture);
}
}
}
Loading

0 comments on commit 00cf546

Please sign in to comment.