Deserialize() публичный Метод

public Deserialize ( System.Runtime.Serialization.Json.JsonSerializationReader jsr ) : object
jsr System.Runtime.Serialization.Json.JsonSerializationReader
Результат object
Пример #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());
                }
            }
        }
Пример #2
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));
            }

            bool nullable = false;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                nullable = true;
                type     = Nullable.GetUnderlyingType(type);
            }

            bool isNull = reader.GetAttribute("type") == "null";

            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:
                if (isNull)
                {
                    reader.ReadElementContentAsString();
                    return(null);
                }
                else
                {
                    return(reader.ReadElementContentAsString());
                }

            case TypeCode.Char:
                var c = reader.ReadElementContentAsString();
                if (c.Length > 1)
                {
                    throw new XmlException("Invalid JSON char");
                }
                return(Char.Parse(c));

            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:
                if (type.IsEnum)
                {
                    return(Enum.ToObject(type, Convert.ChangeType(reader.ReadElementContentAsLong(), Enum.GetUnderlyingType(type), null)));
                }
                else
                {
                    return(Convert.ChangeType(reader.ReadElementContentAsDecimal(), type, null));
                }

            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
                if (type.IsEnum)
                {
                    return(Enum.ToObject(type, Convert.ChangeType(reader.ReadElementContentAsLong(), Enum.GetUnderlyingType(type), null)));
                }
                else
                {
                    return(Convert.ChangeType(reader.ReadElementContentAsDecimal(), type, null));
                }

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

            case TypeCode.DateTime:
                // it does not use ReadElementContentAsDateTime(). Different string format.
                var s = reader.ReadElementContentAsString();
                if (s.Length < 2 || !s.StartsWith("/Date(", StringComparison.Ordinal) || !s.EndsWith(")/", StringComparison.Ordinal))
                {
                    if (nullable)
                    {
                        return(null);
                    }
                    throw new XmlException("Invalid JSON DateTime format. The value format should be '/Date(UnixTime)/'");
                }

                // The date can contain [SIGN]LONG, [SIGN]LONG+HOURSMINUTES or [SIGN]LONG-HOURSMINUTES
                // the format for HOURSMINUTES is DDDD
                int tidx = s.IndexOf('-', 8);
                if (tidx == -1)
                {
                    tidx = s.IndexOf('+', 8);
                }
                int minutes = 0;
                if (tidx == -1)
                {
                    s = s.Substring(6, s.Length - 8);
                }
                else
                {
                    int offset;
                    int.TryParse(s.Substring(tidx + 1, s.Length - 3 - tidx), out offset);

                    minutes = (offset % 100) + (offset / 100) * 60;
                    if (s [tidx] == '-')
                    {
                        minutes = -minutes;
                    }

                    s = s.Substring(6, tidx - 6);
                }
                var date = new DateTime(1970, 1, 1).AddMilliseconds(long.Parse(s));
                if (minutes != 0)
                {
                    date = date.AddMinutes(minutes);
                }
                return(date);

            default:
                if (type == typeof(Guid))
                {
                    return(new Guid(reader.ReadElementContentAsString()));
                }
                else if (type == typeof(Uri))
                {
                    if (isNull)
                    {
                        reader.ReadElementContentAsString();
                        return(null);
                    }
                    else
                    {
                        return(new Uri(reader.ReadElementContentAsString()));
                    }
                }
                else if (type == typeof(XmlQualifiedName))
                {
                    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 = GetCollectionElementType(type);
                    if (ct != null)
                    {
                        return(DeserializeGenericCollection(type, ct));
                    }
                    else
                    {
                        TypeMap map = GetTypeMap(type);
                        return(map.Deserialize(this));
                    }
                }
                else
                {
                    return(ReadInstanceDrivenObject());
                }
            }
        }
Пример #3
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));
            }

            bool isNull = reader.GetAttribute("type") == "null";

            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:
                if (isNull)
                {
                    reader.ReadElementContentAsString();
                    return(null);
                }
                else
                {
                    return(reader.ReadElementContentAsString());
                }

            case TypeCode.Char:
                var c = reader.ReadElementContentAsString();
                if (c.Length > 1)
                {
                    throw new XmlException("Invalid JSON char");
                }
                return(Char.Parse(c));

            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:
                int i = reader.ReadElementContentAsInt();
                if (type.IsEnum)
                {
                    return(Enum.ToObject(type, (object)i));
                }
                else
                {
                    return(Convert.ChangeType(i, type, null));
                }

            case TypeCode.UInt32:
            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());

            case TypeCode.DateTime:
                // it does not use ReadElementContentAsDateTime(). Different string format.
                var s = reader.ReadElementContentAsString();
                if (s.Length < 2 || !s.StartsWith("/Date(", StringComparison.Ordinal) || !s.EndsWith(")/", StringComparison.Ordinal))
                {
                    throw new XmlException("Invalid JSON DateTime format. The value format should be '/Date(UnixTime)/'");
                }
                return(new DateTime(1970, 1, 1).AddMilliseconds(long.Parse(s.Substring(6, s.Length - 8))));

            default:
                if (type == typeof(Guid))
                {
                    return(new Guid(reader.ReadElementContentAsString()));
                }
                else if (type == typeof(Uri))
                {
                    if (isNull)
                    {
                        reader.ReadElementContentAsString();
                        return(null);
                    }
                    else
                    {
                        return(new Uri(reader.ReadElementContentAsString()));
                    }
                }
                else if (type == typeof(XmlQualifiedName))
                {
                    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());
                }
            }
        }