<PackageReference Include="Newtonsoft.Json" Version="5.0.8" />

JProperty

public class JProperty : JContainer
Represents a JSON property.
using Newtonsoft.Json.Utilities; using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; namespace Newtonsoft.Json.Linq { public class JProperty : JContainer { private readonly List<JToken> _content = new List<JToken>(); private readonly string _name; protected override IList<JToken> ChildrenTokens => _content; public string Name { [DebuggerStepThrough] get { return _name; } } public new JToken Value { [DebuggerStepThrough] get { if (_content.Count <= 0) return null; return _content[0]; } set { CheckReentrancy(); JToken item = value ?? new JValue((object)null); if (_content.Count == 0) InsertItem(0, item, false); else SetItem(0, item); } } public override JTokenType Type { [DebuggerStepThrough] get { return JTokenType.Property; } } public JProperty(JProperty other) : base(other) { _name = other.Name; } internal override JToken GetItem(int index) { if (index != 0) throw new ArgumentOutOfRangeException(); return Value; } internal override void SetItem(int index, JToken item) { if (index != 0) throw new ArgumentOutOfRangeException(); if (!JContainer.IsTokenUnchanged(Value, item)) { if (base.Parent != null) ((JObject)base.Parent).InternalPropertyChanging(this); base.SetItem(0, item); if (base.Parent != null) ((JObject)base.Parent).InternalPropertyChanged(this); } } internal override bool RemoveItem(JToken item) { throw new JsonException(StringUtils.FormatWith("Cannot add or remove items from {0}.", CultureInfo.InvariantCulture, typeof(JProperty))); } internal override void RemoveItemAt(int index) { throw new JsonException(StringUtils.FormatWith("Cannot add or remove items from {0}.", CultureInfo.InvariantCulture, typeof(JProperty))); } internal override void InsertItem(int index, JToken item, bool skipParentCheck) { if (Value != null) throw new JsonException(StringUtils.FormatWith("{0} cannot have multiple values.", CultureInfo.InvariantCulture, typeof(JProperty))); base.InsertItem(0, item, false); } internal override bool ContainsItem(JToken item) { return Value == item; } internal override void ClearItems() { throw new JsonException(StringUtils.FormatWith("Cannot add or remove items from {0}.", CultureInfo.InvariantCulture, typeof(JProperty))); } internal override bool DeepEquals(JToken node) { JProperty jProperty = node as JProperty; if (jProperty != null && _name == jProperty.Name) return ContentsEqual(jProperty); return false; } internal override JToken CloneToken() { return new JProperty(this); } internal JProperty(string name) { ValidationUtils.ArgumentNotNull(name, "name"); _name = name; } public JProperty(string name, params object[] content) : this(name, (object)content) { } public JProperty(string name, object content) { ValidationUtils.ArgumentNotNull(name, "name"); _name = name; Value = (IsMultiContent(content) ? new JArray(content) : CreateFromContent(content)); } public override void WriteTo(JsonWriter writer, params JsonConverter[] converters) { writer.WritePropertyName(_name); JToken value = Value; if (value != null) value.WriteTo(writer, converters); else writer.WriteNull(); } internal override int GetDeepHashCode() { return _name.GetHashCode() ^ ((Value != null) ? Value.GetDeepHashCode() : 0); } public new static JProperty Load(JsonReader reader) { if (reader.TokenType == JsonToken.None && !reader.Read()) throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader."); while (reader.TokenType == JsonToken.Comment) { reader.Read(); } if (reader.TokenType != JsonToken.PropertyName) throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader. Current JsonReader item is not a property: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); JProperty jProperty = new JProperty((string)reader.Value); jProperty.SetLineInfo(reader as IJsonLineInfo); jProperty.ReadTokenFrom(reader); return jProperty; } } }