private void SetObjectMember(JsonReader reader, object target, Type targetType, string memberName) { if (!reader.Read()) throw new JsonSerializationException(string.Format("Unexpected end when setting {0}'s value.", memberName)); MemberMappingCollection memberMappings = GetMemberMappings(targetType); Type memberType; object value; // test if a member with memberName exists on the type // otherwise test if target is a dictionary and assign value with the key if it is if (memberMappings.Contains(memberName)) { MemberMapping memberMapping = memberMappings[memberName]; if (memberMapping.Ignored) return; // ignore member if it is readonly if (!memberMapping.Writable) return; // get the member's underlying type memberType = ReflectionUtils.GetMemberUnderlyingType(memberMapping.Member); value = GetObject(reader, memberType); ReflectionUtils.SetMemberValue(memberMapping.Member, target, value); } else if (typeof(IDictionary).IsAssignableFrom(targetType)) { // attempt to get the IDictionary's type memberType = ReflectionUtils.GetDictionaryValueType(target.GetType()); value = GetObject(reader, memberType); ((IDictionary)target).Add(memberName, value); } else { throw new JsonSerializationException(string.Format("Could not find member '{0}' on object of type '{1}'", memberName, targetType.GetType().Name)); } }
private object PopulateObject(JsonReader reader, Type objectType) { object newObject = Activator.CreateInstance(objectType); while (reader.Read()) { switch (reader.TokenType) { case JsonToken.PropertyName: string memberName = reader.Value.ToString(); SetObjectMember(reader, newObject, objectType, memberName); break; case JsonToken.EndObject: return newObject; default: throw new JsonSerializationException("Unexpected token when deserializing object: " + reader.TokenType); } } throw new JsonSerializationException("Unexpected end when deserializing object."); }
private object PopulateList(JsonReader reader, Type objectType) { Type elementType = ReflectionUtils.GetListItemType(objectType); IList populatedList = CollectionUtils.CreateAndPopulateList(objectType, delegate(IList list) { while (reader.Read()) { switch (reader.TokenType) { case JsonToken.EndArray: return; case JsonToken.Comment: break; default: object value = GetObject(reader, elementType); list.Add(value); break; } } throw new JsonSerializationException("Unexpected end when deserializing array."); }); return populatedList; }
private JavaScriptObject PopulateJavaScriptObject(JsonReader reader) { JavaScriptObject jsObject = new JavaScriptObject(); while (reader.Read()) { switch (reader.TokenType) { case JsonToken.PropertyName: string memberName = reader.Value.ToString(); // move to the value token. skip comments do { if (!reader.Read()) throw new JsonSerializationException("Unexpected end while deserializing object."); } while (reader.TokenType == JsonToken.Comment); object value = GetObject(reader, null); jsObject[memberName] = value; break; case JsonToken.EndObject: return jsObject; case JsonToken.Comment: break; default: throw new JsonSerializationException("Unexpected token while deserializing object: " + reader.TokenType); } } throw new JsonSerializationException("Unexpected end while deserializing object."); }
private JavaScriptArray PopulateJavaScriptArray(JsonReader reader) { JavaScriptArray jsArray = new JavaScriptArray(); while (reader.Read()) { switch (reader.TokenType) { case JsonToken.EndArray: return jsArray; case JsonToken.Comment: break; default: object value = GetObject(reader, null); jsArray.Add(value); break; } } throw new JsonSerializationException("Unexpected end while deserializing array."); }
/// <summary> /// Deserializes the Json structure contained by the specified <see cref="JsonReader"/> /// into an instance of the specified type. /// </summary> /// <param name="reader">The type of object to create.</param> /// <param name="objectType">The <see cref="Type"/> of object being deserialized.</param> /// <returns>The instance of <paramref name="objectType"/> being deserialized.</returns> public object Deserialize(JsonReader reader, Type objectType) { if (reader == null) throw new ArgumentNullException("reader"); if (!reader.Read()) return null; return GetObject(reader, objectType); }