-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add ValueKind property in System.Text.Json.Nodes.JsonNode class #53406
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsBackground and Motivation
Proposed APInamespace System.Text.Json.Nodes
{
public abstract class JsonNode : IDynamicMetaObjectProvider {
+ public abstract JsonValueKind ValueKind { get; }
}
} Usage Examplesvar json = JsonNode.Parse("{ \"name\": \"Kingcean\" }");
Assert.AreEqual(JsonValueKind.Object, json.ValueKind );
Assert.AreEqual(JsonValueKind.String, json["name"].ValueKind ; Implementation for Reference// namespace System.Text.Json.Nodes
public abstract class JsonNode
{
public abstract JsonValueKind ValueKind { get; }
// ...
}
public sealed class JsonArray : JsonNode, IList<JsonNode?>, ICollection<JsonNode?>, IEnumerable<JsonNode?>, IEnumerable
{
public override JsonValueKind ValueKind => JsonValueKind.Array;
// ...
}
public sealed class JsonObject : JsonNode, IDictionary<string, JsonNode?>, ICollection<KeyValuePair<string, JsonNode?>>, IEnumerable<KeyValuePair<string, JsonNode?>>, IEnumerable
{
public override JsonValueKind ValueKind => JsonValueKind.Object;
// ...
}
internal abstract partial class JsonValue<TValue> : JsonValue
{
public override JsonValueKind ValueKind
{
get
{
if (_value is null) return JsonValueKind.Null;
if (_value is JsonElement element) return element.ValueKind;
if (_value is string || _value is Guid || _value is DateTime || _value is DateTimeOffset) return JsonValueKind.String;
if (_value is bool b) return b ? JsonValueKind.True : JsonValueKind.False;
if (_value is int || _value is long || _value is uint || _value is ulong || _value is short || _value is ushort || _value is double || _value is float || _value is decimal || _value is byte || || _value is sbyte) return JsonValueKind.Number;
throw new NotSupportedException();
}
}
// ...
}
|
Having such a property would be extremely useful! 👏🏻 /cc @steveharter |
As mentioned in #52611, this property would be very useful in OpenIddict. Any chance it could be discussed for 6.0? |
The main reason that this isn't supported is that a However, I did create a Also, note that Closing as it should be addressed by #55827. |
Update Just assume I had not read the doc correctly ... thanks |
Background and Motivation
System.Text.Json.Node.JsonNode
has provided a way toas
JsonObject
,JsonArray
andJsonValue
, but has no way to get its value kind directly.Proposed API
namespace System.Text.Json.Nodes { public abstract class JsonNode : IDynamicMetaObjectProvider { + public abstract JsonValueKind ValueKind { get; } } }
Usage Examples
Implementation for Reference
The text was updated successfully, but these errors were encountered: