Exemplo n.º 1
0
        private ICompositeStorage TrySerializeInternal(object input, ISerializerContext context)
        {
            Ensure.NotNull(input, "input");
            Ensure.NotNull(context, "context");

            ICompositeModel model = input as ICompositeModel;

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

            ICompositeStorage storage = storageFactory.Create();

            if (!typeNameMapper.TryGet(model.GetType(), out string typeName))
            {
                return(null);
            }

            storage.Add(Name.TypeName, typeName);
            ICompositeStorage childStorage = storage.Add(Name.Payload);

            model.Save(childStorage);
            return(storage);
        }
Exemplo n.º 2
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 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 TrySerialize(object input, ISerializerContext context)
        {
            Ensure.NotNull(input, "input");
            Ensure.NotNull(context, "context");

            ICompositeModel model = input as ICompositeModel;

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

            ICompositeStorage storage = storageFactory.Create();

            model.Save(storage);

            storage.Store(context.Output);
            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 async Task <bool> TrySerializeAsync(object input, ISerializerContext context)
        {
            Ensure.NotNull(input, "input");
            Ensure.NotNull(context, "context");

            ICompositeModel model = input as ICompositeModel;

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

            ICompositeStorage storage = storageFactory.Create();

            model.Save(storage);

            await storage.StoreAsync(context.Output).ConfigureAwait(false);

            return(true);
        }