Skip to content

Commit

Permalink
Optimize JSON string node.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcean committed Oct 27, 2024
1 parent 4729d6a commit 62da449
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Core/Text/Json/BaseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ public static implicit operator BaseJsonValueNode(JsonNode value)
if (value is JsonArray a) return (JsonArrayNode)a;
if (value is not JsonValue token)
throw new InvalidCastException($"Only supports JsonValue, JsonObject and JsonArray but its type is {value.GetType().Name}.");
#if !NET461
#if !NET461 && !NET6_0
switch (token.GetValueKind())
{
case JsonValueKind.Null:
Expand Down
24 changes: 23 additions & 1 deletion Core/Text/Json/StringNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Trivial.Text;
/// Represents a specific JSON string value.
/// </summary>
[System.Text.Json.Serialization.JsonConverter(typeof(JsonObjectNodeConverter))]
public class JsonStringNode : BaseJsonValueNode<string>, IComparable<IJsonValueNode<string>>, IComparable<string>, IEquatable<string>, IEquatable<StringBuilder>, IReadOnlyList<char>
public class JsonStringNode : BaseJsonValueNode<string>, IComparable<IJsonValueNode<string>>, IComparable<string>, IEquatable<string>, IEquatable<StringBuilder>, IEquatable<char>, IReadOnlyList<char>
{
/// <summary>
/// Initializes a new instance of the JsonString class.
Expand Down Expand Up @@ -810,6 +810,28 @@ public bool Equals(StringBuilder other, StringComparison comparisonType)
return Value.Equals(other.ToString(), comparisonType);
}

/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// </summary>
/// <param name="other">The object to compare with the current instance.</param>
/// <returns>true if obj and this instance represent the same value; otherwise, false.</returns>
public bool Equals(ReadOnlySpan<char> other)
{
if (Value is null) return false;
return Value.Equals(other.ToString());
}

/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// </summary>
/// <param name="other">The object to compare with the current instance.</param>
/// <returns>true if obj and this instance represent the same value; otherwise, false.</returns>
public bool Equals(char other)
{
if (Value is null || Value.Length != 1) return false;
return Value[0].Equals(other);
}

/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// </summary>
Expand Down

0 comments on commit 62da449

Please sign in to comment.