/// <summary> /// Loads a <see cref="JConstructor" /> from a <see cref="JsonReader" />. /// </summary> /// <param name="reader">A <see cref="JsonReader" /> that will be read for the content of the <see cref="JConstructor" />.</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="JConstructor" /> that contains the JSON that was read from the specified /// <see cref="JsonReader" />. /// </returns> public new static JConstructor Load(JsonReader reader, JsonLoadSettings settings) { if (reader.TokenType == JsonToken.None) { if (!reader.Read()) { throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader."); } } reader.MoveToContent(); if (reader.TokenType != JsonToken.StartConstructor) { throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } var c = new JConstructor((string)reader.Value); c.SetLineInfo(reader as IJsonLineInfo, settings); c.ReadTokenFrom(reader, settings); return(c); }
/// <summary> /// Loads an <see cref="JConstructor"/> from a <see cref="JsonReader"/>. /// </summary> /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JConstructor"/>.</param> /// <returns>A <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns> public new static JConstructor Load(JsonReader reader) { if (reader.TokenType == JsonToken.None) if (!reader.Read()) throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader."); while (reader.TokenType == JsonToken.Comment) reader.Read(); if (reader.TokenType != JsonToken.StartConstructor) throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); var c = new JConstructor((string)reader.Value); c.SetLineInfo(reader as IJsonLineInfo); c.ReadTokenFrom(reader); return c; }
internal void ReadContentFrom(JsonReader r) { ValidationUtils.ArgumentNotNull(r, "r"); var lineInfo = r as IJsonLineInfo; var parent = this; do { if (parent is JProperty && ((JProperty)parent).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); 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); 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); 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); parent.Add(v); break; case JsonToken.Comment: v = JValue.CreateComment(r.Value.ToString()); v.SetLineInfo(lineInfo); parent.Add(v); break; case JsonToken.Null: v = JValue.CreateNull(); v.SetLineInfo(lineInfo); parent.Add(v); break; case JsonToken.Undefined: v = JValue.CreateUndefined(); v.SetLineInfo(lineInfo); parent.Add(v); break; case JsonToken.PropertyName: var propertyName = r.Value.ToString(); var property = new JProperty(propertyName); property.SetLineInfo(lineInfo); 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()); }