Esempio n. 1
0
        object ReadValueContents(Type knownType)
        {
            Type type;
            var  typeAttrib = CurrentGroup.Attribute("type");

            if (typeAttrib == null)
            {
                type = knownType;
            }
            else if (typeAttrib.Value == "null")
            {
                return(null);
            }
            else
            {
                type = SerializerRegistry.ParseSerializedType(typeAttrib.Value);
            }

            var serializer = SerializerRegistry.GetSerializer(type);

            object value;

            serializer.DeserializeConstructor(out value, _version, this);
            serializer.DeserializeContents(ref value, _version, this);
            serializer.DeserializeBacklinks(ref value, _version, this);
            return(value);
        }
Esempio n. 2
0
        public object ReadXml()
        {
            var  root   = _doc.Root;
            long rootId = (long)root.GetAttribute("id");

            _version = (int)root.GetAttribute("version");

            Assert.That(_rootReferences.IsEmpty());

            foreach (var element in root.Elements())
            {
                Assert.That(element.Name.LocalName == "Reference");

                var id       = (long)element.GetAttribute("id");
                var typeName = (string)element.GetAttribute("type");

                var info = new ReadInfo();

                info.Element        = element;
                info.SerializedType = SerializerRegistry.ParseSerializedType(typeName);
                info.Serializer     = SerializerRegistry.GetSerializer(info.SerializedType);

                _rootReferences.Add(id, info);
            }

            foreach (var info in _rootReferences.Values)
            {
                DeserializeConstructor(info);
                DeserializeContents(info, false);
            }

            foreach (var info in _rootReferences.Values)
            {
                _groups.Push(info.Element);
                info.Serializer.DeserializeBacklinks(ref info.Value, _version, this);
                _groups.Pop();
            }

            foreach (var info in _rootReferences.Values)
            {
                info.Serializer.DeserializationComplete(ref info.Value, _version);
            }

            return(_rootReferences[rootId].Value);
        }
Esempio n. 3
0
        public object ReadPrimitive(Type knownType)
        {
            Assert.IsNotNull(knownType);

            if (knownType == typeof(bool))
            {
                return(bool.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(byte))
            {
                return(byte.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(short))
            {
                return(short.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(ushort))
            {
                return(ushort.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(int))
            {
                return(int.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(uint))
            {
                return(uint.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(long))
            {
                return(long.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(ulong))
            {
                return(ulong.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(float))
            {
                return(float.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(double))
            {
                return(double.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(decimal))
            {
                return(decimal.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(char))
            {
                return(char.Parse(CurrentGroup.Value));
            }
            else if (knownType == typeof(string))
            {
                return(CurrentGroup.Value);
            }
            else if (knownType == typeof(byte[]))
            {
                return(Convert.FromBase64String(CurrentGroup.Value));
            }
            else if (knownType == typeof(Type))
            {
                return(SerializerRegistry.ParseSerializedType(CurrentGroup.Value));
            }
            else if (knownType.IsEnum())
            {
                return(Enum.Parse(knownType, CurrentGroup.Value));
            }

            throw Assert.CreateException(
                      "unhandled primative type '{0}'", knownType.Name());
        }