private object CoerceType(Type targetType, IDictionary value, out Dictionary <string, MemberInfo> memberMap) { object result = this.InstantiateObject(targetType, out memberMap); if (memberMap != null) { IEnumerator enumerator = value.Keys.GetEnumerator(); try { while (enumerator.MoveNext()) { object obj = enumerator.Current; MemberInfo memberInfo2; Type memberInfo = TypeCoercionUtility.GetMemberInfo(memberMap, obj as string, out memberInfo2); this.SetMemberValue(result, memberInfo, memberInfo2, value[obj]); } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } } return(result); }
private object CoerceType(Type targetType, IDictionary value, out Dictionary<string, MemberInfo> memberMap) { object newValue = this.InstantiateObject(targetType, out memberMap); if (memberMap != null) { // copy any values into new object foreach (object key in value.Keys) { MemberInfo memberInfo; Type memberType = TypeCoercionUtility.GetMemberInfo(memberMap, key as String, out memberInfo); this.SetMemberValue(newValue, memberType, memberInfo, value[key]); } } return newValue; }
private void PopulateObject(ref object result, Type objectType, Dictionary <string, MemberInfo> memberMap, Type genericDictionaryType) { if (this.Source[this.index] != '{') { throw new JsonDeserializationException("Expected JSON object.", this.index); } IDictionary dictionary = result as IDictionary; if (dictionary == null && objectType.GetInterface(JsonReader.TypeGenericIDictionary) != null) { throw new JsonDeserializationException(string.Format("Types which implement Generic IDictionary<TKey, TValue> also need to implement IDictionary to be deserialized. ({0})", objectType), this.index); } JsonToken jsonToken; while (true) { this.index++; if (this.index >= this.SourceLength) { break; } jsonToken = this.Tokenize(this.Settings.AllowUnquotedObjectKeys); if (jsonToken == JsonToken.ObjectEnd) { goto Block_5; } if (jsonToken != JsonToken.String && jsonToken != JsonToken.UnquotedName) { goto Block_7; } string text = (jsonToken != JsonToken.String) ? this.ReadUnquotedKey() : ((string)this.ReadString(null)); MemberInfo memberInfo; Type type; if (genericDictionaryType == null && memberMap != null) { type = TypeCoercionUtility.GetMemberInfo(memberMap, text, out memberInfo); } else { type = genericDictionaryType; memberInfo = null; } jsonToken = this.Tokenize(); if (jsonToken != JsonToken.NameDelim) { goto Block_11; } this.index++; if (this.index >= this.SourceLength) { goto Block_12; } if (this.Settings.HandleCyclicReferences && text == "@ref") { int num = (int)this.Read(typeof(int), false); result = this.previouslyDeserialized[num]; jsonToken = this.Tokenize(); } else { object obj = this.Read(type, false); if (dictionary != null) { if (objectType == null && this.Settings.IsTypeHintName(text)) { result = this.Settings.Coercion.ProcessTypeHint(dictionary, obj as string, out objectType, out memberMap); } else { dictionary[text] = obj; } } else { this.Settings.Coercion.SetMemberValue(result, type, memberInfo, obj); } jsonToken = this.Tokenize(); } if (jsonToken != JsonToken.ValueDelim) { goto IL_234; } } throw new JsonDeserializationException("Unterminated JSON object.", this.index); Block_5: goto IL_234; Block_7: throw new JsonDeserializationException("Expected JSON object property name.", this.index); Block_11: throw new JsonDeserializationException("Expected JSON object property name delimiter.", this.index); Block_12: throw new JsonDeserializationException("Unterminated JSON object.", this.index); IL_234: if (jsonToken != JsonToken.ObjectEnd) { throw new JsonDeserializationException("Unterminated JSON object.", this.index); } this.index++; }
private void PopulateObject(ref object result, Type objectType, Dictionary <string, MemberInfo> memberMap, Type genericDictionaryType) { if (this.Source[this.index] != JsonReader.OperatorObjectStart) { throw new JsonDeserializationException(JsonReader.ErrorExpectedObject, this.index); } #if WINDOWS_PHONE IDictionary idict = result as IDictionary; #else IDictionary idict = result as IDictionary; if (idict == null && objectType.GetInterface(JsonReader.TypeGenericIDictionary) != null) { throw new JsonDeserializationException( String.Format(JsonReader.ErrorGenericIDictionary, objectType), this.index); } #endif JsonToken token; do { Type memberType; MemberInfo memberInfo; // consume opening brace or delim this.index++; if (this.index >= this.SourceLength) { throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index); } // get next token token = this.Tokenize(this.Settings.AllowUnquotedObjectKeys); if (token == JsonToken.ObjectEnd) { break; } if (token != JsonToken.String && token != JsonToken.UnquotedName) { throw new JsonDeserializationException(JsonReader.ErrorExpectedPropertyName, this.index); } // parse object member value string memberName = (token == JsonToken.String) ? (String)this.ReadString(null) : this.ReadUnquotedKey(); // if (genericDictionaryType == null && memberMap != null) { // determine the type of the property/field memberType = TypeCoercionUtility.GetMemberInfo(memberMap, memberName, out memberInfo); } else { memberType = genericDictionaryType; memberInfo = null; } // get next token token = this.Tokenize(); if (token != JsonToken.NameDelim) { throw new JsonDeserializationException(JsonReader.ErrorExpectedPropertyNameDelim, this.index); } // consume delim this.index++; if (this.index >= this.SourceLength) { throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index); } object value; // Reference to previously deserialized value if (Settings.HandleCyclicReferences && memberName == "@ref") { // parse object member value int refId = (int)this.Read(typeof(int), false); // Change result object to the one previously deserialized result = previouslyDeserialized[refId]; // get next token // this will probably be the end of the object token = this.Tokenize(); continue; } else { // Normal serialized value // parse object member value value = this.Read(memberType, false); } if (idict != null) { if (objectType == null && this.Settings.IsTypeHintName(memberName)) { result = this.Settings.Coercion.ProcessTypeHint(idict, value as string, out objectType, out memberMap); } else { idict[memberName] = value; } } else { this.Settings.Coercion.SetMemberValue(result, memberType, memberInfo, value); } // get next token token = this.Tokenize(); } while (token == JsonToken.ValueDelim); if (token != JsonToken.ObjectEnd) { throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index); } // consume closing brace this.index++; //return result; }
private void PopulateObject(object result, Type objectType, Dictionary <string, MemberInfo> memberMap, Type genericDictionaryType) { if (this.Source[this.index] != JsonReader.OperatorObjectStart) { throw new JsonDeserializationException(JsonReader.ErrorExpectedObject, this.index); } JsonToken token; do { Type memberType; MemberInfo memberInfo; // consume opening brace or delim this.index++; if (this.index >= this.SourceLength) { throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index); } // get next token token = this.Tokenize(this.Settings.AllowUnquotedObjectKeys); if (token == JsonToken.ObjectEnd) { break; } if (token != JsonToken.String && token != JsonToken.UnquotedName) { throw new JsonDeserializationException(JsonReader.ErrorExpectedPropertyName, this.index); } // parse object member value string memberName = (token == JsonToken.String) ? (String)this.ReadString(null) : this.ReadUnquotedKey(); if (genericDictionaryType == null && memberMap != null) { // determine the type of the property/field memberType = TypeCoercionUtility.GetMemberInfo(memberMap, memberName, out memberInfo); } else { memberType = genericDictionaryType; memberInfo = null; } // get next token token = this.Tokenize(); if (token != JsonToken.NameDelim) { throw new JsonDeserializationException(JsonReader.ErrorExpectedPropertyNameDelim, this.index); } // consume delim this.index++; if (this.index >= this.SourceLength) { throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index); } // parse object member value object value = this.Read(memberType, false); if (result is IDictionary) { if (objectType == null && this.Settings.IsTypeHintName(memberName)) { result = this.Settings.Coercion.ProcessTypeHint((IDictionary)result, value as string, out objectType, out memberMap); } else { ((IDictionary)result)[memberName] = value; } } else if (objectType.GetInterface(JsonReader.TypeGenericIDictionary) != null) { throw new JsonDeserializationException( String.Format(JsonReader.ErrorGenericIDictionary, objectType), this.index); } else { this.Settings.Coercion.SetMemberValue(result, memberType, memberInfo, value); } // get next token token = this.Tokenize(); } while (token == JsonToken.ValueDelim); if (token != JsonToken.ObjectEnd) { throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index); } // consume closing brace this.index++; //return result; }