System.Runtime.Serialization.Json.TypeMap.Deserialize C# (CSharp) Method

Deserialize() public method

public Deserialize ( System.Runtime.Serialization.Json.JsonSerializationReader jsr ) : object
jsr System.Runtime.Serialization.Json.JsonSerializationReader
return object
		public virtual object Deserialize (JsonSerializationReader jsr)
		{
			XmlReader reader = jsr.Reader;
			bool isNull = reader.GetAttribute ("type") == "null";

			object ret = isNull ? null : FormatterServices.GetUninitializedObject (type);
			if (ret != null && OnDeserializing != null)
				OnDeserializing.Invoke (ret, new object [] {new StreamingContext (StreamingContextStates.All)});
			Dictionary<TypeMapMember,bool> filled = new Dictionary<TypeMapMember,bool> ();

			reader.ReadStartElement ();
			for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
				bool consumed = false;
				for (int i = 0; i < members.Length; i++) {
					TypeMapMember mm = members [i];
					if (mm.Name == reader.LocalName && reader.NamespaceURI == String.Empty) {
						if (filled.ContainsKey (mm))
							throw new SerializationException (String.Format ("Object content '{0}' for '{1}' already appeared in the reader", reader.LocalName, type));
						mm.SetMemberValue (ret, jsr.ReadObject (mm.Type));
						filled [mm] = true;
						consumed = true;
						break;
					}
				}
				if (!consumed)
					reader.Skip ();
			}
			reader.ReadEndElement ();
			if (ret != null && OnDeserialized != null)
				OnDeserialized.Invoke (ret, new object [] {new StreamingContext (StreamingContextStates.All)});
			return ret;
		}
	}

Usage Example

示例#1
0
        public object ReadObject(Type type)
        {
            if (serialized_object_count++ == serializer.MaxItemsInObjectGraph)
            {
                throw SerializationError(String.Format("The object graph exceeded the maximum object count '{0}' specified in the serializer", serializer.MaxItemsInObjectGraph));
            }

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.DBNull:
                string dbn = reader.ReadElementContentAsString();
                if (dbn != String.Empty)
                {
                    throw new SerializationException(String.Format("The only expected DBNull value string is '{{}}'. Tha actual input was '{0}'.", dbn));
                }
                return(DBNull.Value);

            case TypeCode.String:
                return(reader.ReadElementContentAsString());

            case TypeCode.Single:
                return(reader.ReadElementContentAsFloat());

            case TypeCode.Double:
                return(reader.ReadElementContentAsDouble());

            case TypeCode.Decimal:
                return(reader.ReadElementContentAsDecimal());

            case TypeCode.Byte:
            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
                int i = reader.ReadElementContentAsInt();
                if (type.IsEnum)
                {
                    return(Enum.ToObject(type, (object)i));
                }
                else
                {
                    return(Convert.ChangeType(i, type, null));
                }

            case TypeCode.Int64:
            case TypeCode.UInt64:
                long l = reader.ReadElementContentAsLong();
                if (type.IsEnum)
                {
                    return(Enum.ToObject(type, (object)l));
                }
                else
                {
                    return(Convert.ChangeType(l, type, null));
                }

            case TypeCode.Boolean:
                return(reader.ReadElementContentAsBoolean());

            default:
                if (type == typeof(Guid))
                {
                    return(new Guid(reader.ReadElementContentAsString()));
                }
                else if (type == typeof(Uri))
                {
                    return(new Uri(reader.ReadElementContentAsString()));
                }
                else if (type == typeof(XmlQualifiedName))
                {
                    string s   = reader.ReadElementContentAsString();
                    int    idx = s.IndexOf(':');
                    return(idx < 0 ? new XmlQualifiedName(s) : new XmlQualifiedName(s.Substring(0, idx), s.Substring(idx + 1)));
                }
                else if (type != typeof(object))
                {
                    // strongly-typed object
                    if (reader.IsEmptyElement)
                    {
                        // empty -> null array or object
                        reader.Read();
                        return(null);
                    }

                    Type ct = GetCollectionType(type);
                    if (ct != null)
                    {
                        return(DeserializeGenericCollection(type, ct));
                    }
                    else
                    {
                        TypeMap map = GetTypeMap(type);
                        return(map.Deserialize(this));
                    }
                }
                else
                {
                    return(ReadInstanceDrivenObject());
                }
            }
        }
All Usage Examples Of System.Runtime.Serialization.Json.TypeMap::Deserialize