/// <summary> /// Load a <see cref="JObject" /> 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 <c>null</c>, default load settings will be used. /// </param> /// <returns>A <see cref="JObject" /> populated from the string that contains JSON.</returns> /// <exception cref="JsonReaderException"> /// <paramref name="json" /> is not valid JSON. /// </exception> /// <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 JObject Parse(string json, JsonLoadSettings settings) { using (JsonReader reader = new JsonTextReader(new StringReader(json))) { var o = Load(reader, settings); while (reader.Read()) { // Any content encountered here other than a comment will throw in the reader. } return(o); } }
internal void ReadTokenFrom(JsonReader reader, JsonLoadSettings options) { var startDepth = reader.Depth; if (!reader.Read()) { throw JsonReaderException.Create(reader, "Error reading {0} from JsonReader.".FormatWith(CultureInfo.InvariantCulture, GetType().Name)); } ReadContentFrom(reader, options); var endDepth = reader.Depth; if (endDepth > startDepth) { throw JsonReaderException.Create(reader, "Unexpected end of content while loading {0}.".FormatWith(CultureInfo.InvariantCulture, GetType().Name)); } }
internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings) { ValidationUtils.ArgumentNotNull(r, nameof(r)); var lineInfo = r as IJsonLineInfo; var parent = this; do { if ((parent as JProperty)?.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: var 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: var o = new JObject(); o.SetLineInfo(lineInfo, settings); parent.Add(o); parent = o; break; case JsonToken.EndObject: if (parent == this) { return; } parent = parent.Parent; break; case JsonToken.StartConstructor: var 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: var 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: var propertyName = r.Value.ToString(); var property = new JProperty(propertyName); property.SetLineInfo(lineInfo, settings); var parentObject = (JObject)parent; // handle multiple properties with the same name in JSON var 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()); }