/// <summary> /// Loads an <see cref="BacktraceJObject"/> from a <see cref="JsonReader"/>. /// </summary> /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="BacktraceJObject"/>.</param> /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON. /// If this is null, default load settings will be used.</param> /// <returns>A <see cref="BacktraceJObject"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns> public new static BacktraceJObject Load(JsonReader reader, JsonLoadSettings settings) { ValidationUtils.ArgumentNotNull(reader, "reader"); if (reader.TokenType == JsonToken.None) { if (!reader.Read()) { throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader."); } } reader.MoveToContent(); if (reader.TokenType != JsonToken.StartObject) { throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } BacktraceJObject o = new BacktraceJObject(); o.SetLineInfo(reader as IJsonLineInfo, settings); o.ReadTokenFrom(reader, settings); return(o); }
internal override bool DeepEquals(JToken node) { BacktraceJObject t = node as BacktraceJObject; if (t == null) { return(false); } return(_properties.Compare(t._properties)); }
/// <summary> /// Load a <see cref="BacktraceJObject"/> from a string that contains JSON. /// </summary> /// <param name="json">A <see cref="String"/> that contains JSON.</param> /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON. /// If this is null, default load settings will be used.</param> /// <returns>A <see cref="BacktraceJObject"/> populated from the string that contains JSON.</returns> /// <example> /// <code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParse" title="Parsing a JSON Object from Text" /> /// </example> public new static BacktraceJObject Parse(string json, JsonLoadSettings settings) { using (JsonReader reader = new JsonTextReader(new StringReader(json))) { BacktraceJObject o = Load(reader, settings); if (reader.Read() && reader.TokenType != JsonToken.Comment) { throw JsonReaderException.Create(reader, "Additional text found in JSON string after parsing content."); } return(o); } }
/// <summary> /// Writes the property name of a name/value pair on a JSON object. /// </summary> /// <param name="name">The name of the property.</param> public override void WritePropertyName(string name) { BacktraceJObject o = _parent as BacktraceJObject; if (o != null) { // avoid duplicate property name exception // last property name wins o.Remove(name); } AddParent(new BacktraceJProperty(name)); // don't set state until after in case of an error // incorrect state will cause issues if writer is disposed when closing open properties base.WritePropertyName(name); }
internal override void MergeItem(object content, JsonMergeSettings settings) { BacktraceJObject o = content as BacktraceJObject; if (o == null) { return; } foreach (KeyValuePair <string, JToken> contentItem in o) { BacktraceJProperty existingProperty = Property(contentItem.Key); if (existingProperty == null) { Add(contentItem.Key, contentItem.Value); } else if (contentItem.Value != null) { BacktraceJContainer existingContainer = existingProperty.Value as BacktraceJContainer; if (existingContainer == null) { if (contentItem.Value.Type != JTokenType.Null || (settings != null && settings.MergeNullValueHandling == MergeNullValueHandling.Merge)) { existingProperty.Value = contentItem.Value; } } else if (existingContainer.Type != contentItem.Value.Type) { existingProperty.Value = contentItem.Value; } else { existingContainer.Merge(contentItem.Value, settings); } } } }
internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings) { ValidationUtils.ArgumentNotNull(r, "r"); IJsonLineInfo lineInfo = r as IJsonLineInfo; BacktraceJContainer parent = this; do { if (parent is BacktraceJProperty && (parent as BacktraceJProperty).Value != null) { if (parent == this) { return; } parent = parent.Parent; } switch (r.TokenType) { case JsonToken.None: // new reader. move to actual content break; case JsonToken.StartArray: JArray a = new JArray(); a.SetLineInfo(lineInfo, settings); parent.Add(a); parent = a; break; case JsonToken.EndArray: if (parent == this) { return; } parent = parent.Parent; break; case JsonToken.StartObject: BacktraceJObject o = new BacktraceJObject(); o.SetLineInfo(lineInfo, settings); parent.Add(o); parent = o; break; case JsonToken.EndObject: if (parent == this) { return; } parent = parent.Parent; break; case JsonToken.StartConstructor: JConstructor constructor = new JConstructor(r.Value.ToString()); constructor.SetLineInfo(lineInfo, settings); parent.Add(constructor); parent = constructor; break; case JsonToken.EndConstructor: if (parent == this) { return; } parent = parent.Parent; break; case JsonToken.String: case JsonToken.Integer: case JsonToken.Float: case JsonToken.Date: case JsonToken.Boolean: case JsonToken.Bytes: JValue v = new JValue(r.Value); v.SetLineInfo(lineInfo, settings); parent.Add(v); break; case JsonToken.Comment: if (settings != null && settings.CommentHandling == CommentHandling.Load) { v = JValue.CreateComment(r.Value.ToString()); v.SetLineInfo(lineInfo, settings); parent.Add(v); } break; case JsonToken.Null: v = JValue.CreateNull(); v.SetLineInfo(lineInfo, settings); parent.Add(v); break; case JsonToken.Undefined: v = JValue.CreateUndefined(); v.SetLineInfo(lineInfo, settings); parent.Add(v); break; case JsonToken.PropertyName: string propertyName = r.Value.ToString(); BacktraceJProperty property = new BacktraceJProperty(propertyName); property.SetLineInfo(lineInfo, settings); BacktraceJObject parentObject = (BacktraceJObject)parent; // handle multiple properties with the same name in JSON BacktraceJProperty existingPropertyWithName = parentObject.Property(propertyName); if (existingPropertyWithName == null) { parent.Add(property); } else { existingPropertyWithName.Replace(property); } parent = property; break; default: throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType)); } } while (r.Read()); }
/// <summary> /// Initializes a new instance of the <see cref="BacktraceJObject"/> class from another <see cref="BacktraceJObject"/> object. /// </summary> /// <param name="other">A <see cref="BacktraceJObject"/> object to copy from.</param> public BacktraceJObject(BacktraceJObject other) : base(other) { }