Beispiel #1
0
        private bool TryDeserializeInternal(IDeserializerContext context, ICompositeStorage storage)
        {
            string typeName = storage.Get <string>(Name.TypeName);

            if (!typeNameMapper.TryGet(typeName, out Type outputType))
            {
                return(false);
            }

            if (!storage.TryGet(Name.Payload, out ICompositeStorage childStorage))
            {
                return(false);
            }

            ICompositeModel model = modelFactory.Create(outputType) as ICompositeModel;

            if (model == null)
            {
                return(false);
            }

            model.Load(childStorage);
            context.Output = model;
            return(true);
        }
        public object Deserialize(IDeserializerContext context)
        {
            T instance = FastActivator <T> .Create();

            if (context.IsEmptyElement)
            {
                context.Read();
                return(instance);
            }

            if (!context.Read())
            {
                return(instance);
            }

            while (context.NodeType != XmlNodeType.EndElement && context.NodeType != XmlNodeType.None)
            {
                if (context.NodeType == XmlNodeType.Element)
                {
                    ReadProperty(context, instance);
                    continue;
                }

                if (!context.Read())
                {
                    break;
                }
            }

            context.ExitElement();

            return(instance);
        }
 protected override object GetXmlObject(IDeserializerContext context, XmlReader reader)
 {
     if (HasContent(reader)) {
         return XElement.Load(reader);
     }
     return null;
 }
Beispiel #4
0
        protected override bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage)
        {
            if (base.TryLoad(input, context, type, storage))
            {
                ICompositeStorage payloadStorage = GetOrAddPayloadStorage(storage);
                foreach (ICompositeFormatterExtender extender in extenders)
                {
                    extender.Load(payloadStorage, context.Output);
                }

                IKeyValueCollection metadata;
                ICompositeStorage   metadataStorage;
                if (context.TryGetEnvelopeMetadata(out metadata) && storage.TryGet(MetadataKey, out metadataStorage))
                {
                    foreach (string key in metadataStorage.Keys)
                    {
                        metadata.Add(key, metadataStorage.Get <object>(key));
                    }
                }

                return(true);
            }

            return(false);
        }
 public override object ProcessFromDb(IDeserializerContext context, int column)
 {
     object result = base.ProcessFromDb(context, column);
     if (result != null) {
         switch (Type.GetTypeCode(result.GetType())) {
         case TypeCode.String:
             return Enum.Parse(Type, (string)result);
         case TypeCode.Byte:
             return Enum.ToObject(Type, (byte)result);
         case TypeCode.SByte:
             return Enum.ToObject(Type, (sbyte)result);
         case TypeCode.UInt16:
             return Enum.ToObject(Type, (ushort)result);
         case TypeCode.Int16:
             return Enum.ToObject(Type, (short)result);
         case TypeCode.UInt32:
             return Enum.ToObject(Type, (uint)result);
         case TypeCode.Int32:
             return Enum.ToObject(Type, (int)result);
         case TypeCode.UInt64:
             return Enum.ToObject(Type, (ulong)result);
         case TypeCode.Int64:
             return Enum.ToObject(Type, (long)result);
         default:
             result = Convert.ChangeType(result, underlyingType);
             Debug.Assert(result != null);
             return Enum.ToObject(Type, result);
         }
     }
     return result;
 }
Beispiel #6
0
            protected override bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage)
            {
                if (storage.TryGet("Count", out int count))
                {
                    log.Debug($"Count '{count}'.");

                    Type  listType = typeof(List <>).MakeGenericType(type.Type);
                    IList list     = (IList)Activator.CreateInstance(listType);

                    log.Debug($"List: '{list != null}'.");

                    for (int i = 0; i < count; i++)
                    {
                        if (storage.TryGet(i.ToString(), out ICompositeStorage innerStorage) && base.TryLoad(input, context, type, innerStorage))
                        {
                            log.Debug($"Add item '{context.Output != null}'.");
                            list.Add(context.Output);
                        }
                        else
                        {
                            log.Error($"Item failed at index '{i}'.");
                            return(false);
                        }
                    }

                    context.Output = list;
                    return(true);
                }

                log.Debug($"Count not found.");
                return(base.TryLoad(input, context, type, storage));
            }
Beispiel #7
0
        private void ReadItem(IDeserializerContext context, IDictionary <TKey, TValue> dictionary)
        {
            if (context.NodeType != XmlNodeType.Element || context.LocalName != "item")
            {
                throw new InvalidOperationException("Dictionary is not at an item element");
            }

            context.Read();

            object key = context.Deserialize(context.Namespace);

            object element = context.Deserialize(context.Namespace);

            if (context.NodeType != XmlNodeType.EndElement || context.LocalName != "item")
            {
                throw new InvalidOperationException("Dictionary is not at the end of an item");
            }

            context.Read();

            if (key != null)
            {
                dictionary.Add((TKey)key, (TValue)element);
            }
        }
Beispiel #8
0
        protected override bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage)
        {
            if (base.TryLoad(input, context, type, storage))
            {
                if (storage.TryGet("AggregateRootException", out ICompositeStorage inner))
                {
                    AggregateRootExceptionDecorator decorator = new AggregateRootExceptionDecorator((AggregateRootException)context.Output);

                    IKey key = inner.Get <IKey>(Name.AggregateRootKey);
                    if (key != null)
                    {
                        decorator.SetKey(key);
                    }

                    key = inner.Get <IKey>(Name.CommandKey);
                    if (key != null)
                    {
                        decorator.SetCommandKey(key);
                    }

                    key = inner.Get <IKey>(Name.SourceCommandKey);
                    if (key != null)
                    {
                        decorator.SetSourceCommandKey(key);
                    }
                }

                return(true);
            }

            return(false);
        }
 public override object ProcessFromDb(IDeserializerContext context, int column)
 {
     object result = base.ProcessFromDb(context, column);
     if (result != null) {
         result = DateTime.SpecifyKind(Convert.ToDateTime(result), dateTimeKind);
     }
     return result;
 }
 public override sealed object ProcessFromDb(IDeserializerContext context, int column)
 {
     SqlXml xml = context.GetSqlXml(column);
     if (!xml.IsNull) {
         return ProcessXmlReader(context, xml.CreateReader());
     }
     return null;
 }
 public override object ProcessFromDb(IDeserializerContext context, int column)
 {
     object result = base.ProcessFromDb(context, column);
     if ((result != null) && (!Type.IsAssignableFrom(result.GetType()))) {
         result = Convert.ChangeType(result, Type);
     }
     return result;
 }
Beispiel #12
0
 public Task <bool> TryDeserializeAsync(Stream input, IDeserializerContext context)
 {
     return(Task.Factory.StartNew(() => RunWithCatch(() =>
     {
         XmlSerializer serializer = new XmlSerializer(context.OutputType);
         object output = serializer.Deserialize(input);
         context.Output = output;
     })));
 }
Beispiel #13
0
 public bool TryDeserialize(Stream input, IDeserializerContext context)
 {
     Ensure.NotNull(context, "context");
     return(RunWithCatch(() =>
     {
         XmlSerializer serializer = new XmlSerializer(context.OutputType);
         object output = serializer.Deserialize(input);
         context.Output = output;
     }));
 }
 protected override object GetXmlObject(IDeserializerContext context, XmlReader reader)
 {
     if (HasContent(reader)) {
         context.XmlDocument.Load(reader);
         XmlElement result = context.XmlDocument.DocumentElement;
         context.XmlDocument.RemoveAll();
         return result;
     }
     return null;
 }
Beispiel #15
0
        public async Task <bool> TryDeserializeAsync(Stream input, IDeserializerContext context)
        {
            Ensure.NotNull(input, "input");
            Ensure.NotNull(context, "context");

            ICompositeStorage storage = storageFactory.Create();
            await storage.LoadAsync(input).ConfigureAwait(false);

            return(TryDeserializeInternal(context, storage));
        }
Beispiel #16
0
        private bool ListIsEmpty(IDeserializerContext context)
        {
            if (context.IsEmptyElement)
            {
                context.Read();
                return(true);
            }

            return(false);
        }
        public object Deserialize(IDeserializerContext context)
        {
            var s = context.ReadElementAsString();

            if (string.IsNullOrEmpty(s))
            {
                return(default(char));
            }

            return(XmlConvert.ToChar(s));
        }
Beispiel #18
0
        public bool TryDeserialize(Stream input, IDeserializerContext context)
        {
            Ensure.NotNull(input, "input");
            Ensure.NotNull(context, "context");

            ICompositeStorage storage = storageFactory.Create();

            storage.Load(input);

            return(TryDeserializeInternal(context, storage));
        }
Beispiel #19
0
        public bool TryDeserialize(Stream input, IDeserializerContext context)
        {
            Ensure.NotNull(context, "context");

            //TODO: Catch exceptions.
            using (StreamReader reader = new StreamReader(input))
            {
                string inputValue = reader.ReadToEnd();
                context.Output = JsonConvert.DeserializeObject(inputValue, context.OutputType, settings);
            }

            return(true);
        }
        private Envelope WrapOutputInEnvelope(Type innerType, object output, IDeserializerContext context)
        {
            Envelope envelope = EnvelopeFactory.Create(output, innerType);

            IKeyValueCollection metadata = context.GetEnvelopeMetadata();

            foreach (string key in metadata.Keys)
            {
                envelope.Metadata.Add(key, metadata.Get <object>(key));
            }

            return(envelope);
        }
Beispiel #21
0
        public async Task <bool> TryDeserializeAsync(Stream input, IDeserializerContext context)
        {
            Ensure.NotNull(context, "context");

            //TODO: Catch exceptions.
            using (StreamReader reader = new StreamReader(input))
            {
                string inputValue = await reader.ReadToEndAsync();

                context.Output = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject(inputValue, context.OutputType, settings));
            }

            return(true);
        }
Beispiel #22
0
        public async Task <bool> TryDeserializeAsync(Stream input, IDeserializerContext context)
        {
            CompositeType type;

            if (!provider.TryGet(context.OutputType, out type))
            {
                return(false);
            }

            ICompositeStorage storage = storageFactory.Create();
            await storage.LoadAsync(input).ConfigureAwait(false);

            return(TryLoad(input, context, type, storage));
        }
Beispiel #23
0
        public bool TryDeserialize(Stream input, IDeserializerContext context)
        {
            CompositeType type;

            if (!provider.TryGet(context.OutputType, out type))
            {
                return(false);
            }

            ICompositeStorage storage = storageFactory.Create();

            storage.Load(input);

            return(TryLoad(input, context, type, storage));
        }
Beispiel #24
0
        public bool TryDeserialize(Stream input, IDeserializerContext context)
        {
            if (typeof(IList).IsAssignableFrom(context.OutputType))
            {
                DefaultDeserializerContext innerContext = new DefaultDeserializerContext(context.OutputType.GetGenericArguments()[0]);
                if (inner.TryDeserialize(input, innerContext))
                {
                    context.Output = innerContext.Output;
                    return(true);
                }

                return(false);
            }

            return(inner.TryDeserialize(input, context));
        }
        public async Task <bool> TryDeserializeAsync(Stream input, IDeserializerContext context)
        {
            DefaultDeserializerContext innerContext;

            if (TryDeserializeEnvelope(input, context, out innerContext))
            {
                if (await inner.TryDeserializeAsync(input, innerContext))
                {
                    context.Output = WrapOutputInEnvelope(innerContext.OutputType, innerContext.Output, innerContext);
                    return(true);
                }

                return(false);
            }

            return(await inner.TryDeserializeAsync(input, context));
        }
        public bool TryDeserialize(Stream input, IDeserializerContext context)
        {
            DefaultDeserializerContext innerContext;

            if (TryDeserializeEnvelope(input, context, out innerContext))
            {
                if (inner.TryDeserialize(input, innerContext))
                {
                    context.Output = WrapOutputInEnvelope(innerContext.OutputType, innerContext.Output, innerContext);
                    return(true);
                }

                return(false);
            }

            return(inner.TryDeserialize(input, context));
        }
        public async Task <bool> TryDeserializeAsync(Stream input, IDeserializerContext context)
        {
            Ensure.NotNull(input, "input");
            Ensure.NotNull(context, "context");

            ICompositeModel model = modelFactory.Invoke(context.OutputType) as ICompositeModel;

            if (model == null)
            {
                return(false);
            }

            ICompositeStorage storage = storageFactory.Create();
            await storage.LoadAsync(input).ConfigureAwait(false);

            model.Load(storage);
            context.Output = model;
            return(true);
        }
        public bool TryDeserialize(Stream input, IDeserializerContext context)
        {
            Ensure.NotNull(input, "input");
            Ensure.NotNull(context, "context");

            ICompositeModel model = modelFactory.Invoke(context.OutputType) as ICompositeModel;

            if (model == null)
            {
                return(false);
            }

            ICompositeStorage storage = storageFactory.Create();

            storage.Load(input);

            model.Load(storage);
            context.Output = model;
            return(true);
        }
        public object Deserialize(IDeserializerContext context)
        {
            string uriString = context.ReadElementAsString();

            if (uriString == null)
            {
                return(null);
            }

            try
            {
                Uri uri = new Uri(uriString);

                return(uri);
            }
            catch (UriFormatException ex)
            {
                throw new SerializationException("The Uri is in an invalid format: " + uriString, ex);
            }
        }
Beispiel #30
0
        public object Deserialize(IDeserializerContext context)
        {
            Dictionary <TKey, TValue> dictionary = new Dictionary <TKey, TValue>();

            if (DictionaryIsEmpty(context))
            {
                return(dictionary);
            }

            MoveToFirstElement(context);

            while (context.NodeType != XmlNodeType.EndElement)
            {
                ReadItem(context, dictionary);
            }

            MovePastEndElement(context);

            return(dictionary);
        }
        private void ReadProperty(IDeserializerContext context, T instance)
        {
            DeserializeObjectProperty <T> property;

            if (_propertyCache.TryGetProperty(context.LocalName, out property))
            {
                object value = context.Deserialize(context.Namespace);

                property.SetValue(instance, value);
            }
            else
            {
                if (_log.IsDebugEnabled)
                {
                    _log.Debug("No property " + context.LocalName + " in class " + typeof(T).ToFriendlyName() + " for deserialization");
                }

                context.Deserialize(context.Namespace);
            }
        }
Beispiel #32
0
        protected override bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage)
        {
            if (!base.TryLoad(input, context, type, storage))
            {
                return(false);
            }

            ICompositeStorage payloadStorage;
            GuidedObject      guided = context.Output as GuidedObject;

            if (guided != null && storage.TryGet(Name.Payload, out payloadStorage))
            {
                string guid;
                if (payloadStorage.TryGet("Guid", out guid))
                {
                    guided.Guid = guid;
                }
            }

            return(true);
        }
        public bool TryDeserialize(Stream input, IDeserializerContext context)
        {
            Ensure.NotNull(context, "context");

            string inputValue = null;

            try
            {
                using (StreamReader reader = new StreamReader(input))
                {
                    inputValue     = reader.ReadToEnd();
                    context.Output = JsonConvert.DeserializeObject(inputValue, settings);
                }
            }
            catch (JsonException e)
            {
                throw DeserializationFailed(inputValue, e);
            }

            return(true);
        }
        public async Task <bool> TryDeserializeAsync(Stream input, IDeserializerContext context)
        {
            Ensure.NotNull(context, "context");

            string inputValue = null;

            try
            {
                using (StreamReader reader = new StreamReader(input))
                {
                    inputValue = await reader.ReadToEndAsync();

                    context.Output = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject(inputValue, settings));
                }
            }
            catch (JsonException e)
            {
                throw DeserializationFailed(inputValue, e);
            }

            return(true);
        }
Beispiel #35
0
        /// <summary>
        /// Tries to load a object from the <paramref name="input"/> described by the <paramref name="type"/>.
        /// </summary>
        /// <param name="input">The serialized value to deserialize.</param>
        /// <param name="context">The deserialization context.</param>
        /// <param name="type">The composite type descriptor to load.</param>
        /// <param name="storage">The composite storage with loaded values.</param>
        /// <returns><c>true</c> if object was deserialized; <c>false</c> otherwise.</returns>
        protected virtual bool TryLoad(Stream input, IDeserializerContext context, CompositeType type, ICompositeStorage storage)
        {
            int version;

            if (!storage.TryGet(Name.Version, out version))
            {
                throw new MissingVersionValueException();
            }

            CompositeVersion  typeVersion = GetCompositeVersion(type, version, context.OutputType);
            ICompositeStorage valueStorage;

            if (!storage.TryGet(Name.Payload, out valueStorage))
            {
                throw new MissingPayloadValueException();
            }

            List <object> values = new List <object>();

            foreach (CompositeProperty property in typeVersion.Properties)
            {
                object value;
                if (!TryLoadValue(valueStorage, property.Name, property.Type, out value))
                {
                    throw new NotSupportedValueException(type.Type, property.Name, property.Type);
                }

                values.Add(value);
            }

            context.Output = typeVersion.Constructor.Factory(values.ToArray());

            if (type.VersionProperty.Setter != null)
            {
                type.VersionProperty.Setter(context.Output, version);
            }

            return(true);
        }
        private bool TryDeserializeEnvelope(Stream input, IDeserializerContext context, out DefaultDeserializerContext innerContext)
        {
            if (typeof(Envelope).IsAssignableFrom(context.OutputType))
            {
                Type innerType = typeof(object);
                if (context.OutputType.IsGenericType)
                {
                    innerType = context.OutputType.GetGenericArguments()[0];
                }

                innerContext = new DefaultDeserializerContext(innerType);
                foreach (string key in context.Metadata.Keys)
                {
                    innerContext.Metadata.Add(key, context.Metadata.Get <object>(key));
                }

                innerContext.AddEnvelopeMetadata(new KeyValueCollection());
                return(true);
            }

            innerContext = null;
            return(false);
        }
 protected override sealed object ProcessXmlReader(IDeserializerContext context, XmlReader xmlReader)
 {
     using (xmlReader) {
         return GetXmlObject(context, xmlReader);
     }
 }
 protected virtual object ProcessXmlReader(IDeserializerContext context, XmlReader xmlReader)
 {
     return xmlReader;
 }
 public virtual object ProcessFromDb(IDeserializerContext context, int column)
 {
     object result = context.GetValue(column);
     if (result == DBNull.Value) {
         return null;
     }
     return result;
 }
 protected override object GetXmlObject(IDeserializerContext context, XmlReader reader)
 {
     XmlDocument doc = new XmlDocument(context.NameTable);
     doc.Load(reader);
     return doc;
 }
 protected override object GetXmlObject(IDeserializerContext context, XmlReader reader)
 {
     return new XPathDocument(reader);
 }
 protected abstract object GetXmlObject(IDeserializerContext context, XmlReader reader);
 public override object ProcessFromDb(IDeserializerContext context, int column)
 {
     throw new NotSupportedException("Nested members need to be deserialized directly via SqlDeserializer");
 }