public static object DeserializePayloadUsingSpecificSerializer(
            this DescribedSerialization describedSerialization,
            IDeserialize deserializer,
            IDecompress decompressor                    = null,
            TypeMatchStrategy typeMatchStrategy         = TypeMatchStrategy.NamespaceAndName,
            MultipleMatchStrategy multipleMatchStrategy = MultipleMatchStrategy.ThrowOnMultiple)
        {
            new { describedSerialization }.Must().NotBeNull();
            new { deserializer }.Must().NotBeNull();

            var localDecompressor = decompressor ?? new NullCompressor();
            var targetType        = describedSerialization.PayloadTypeRepresentation.ResolveFromLoadedTypes(typeMatchStrategy, multipleMatchStrategy);

            object ret;

            switch (describedSerialization.SerializationDescription.SerializationFormat)
            {
            case SerializationFormat.Binary:
                var rawBytes          = Convert.FromBase64String(describedSerialization.SerializedPayload);
                var decompressedBytes = localDecompressor.DecompressBytes(rawBytes);
                ret = deserializer.Deserialize(decompressedBytes, targetType);
                break;

            case SerializationFormat.String:
                ret = deserializer.Deserialize(describedSerialization.SerializedPayload, targetType);
                break;

            default: throw new NotSupportedException(Invariant($"{nameof(SerializationFormat)} - {describedSerialization.SerializationDescription.SerializationFormat} is not supported."));
            }

            return(ret);
        }
示例#2
0
        public ITransport GetTransport(string json)
        {
            var baseTransport = _serializer.Deserialize <ITransport>(json);
            var type          = _container.Resolve <ITransport>(baseTransport.TransportType.ToString());

            return(_serializer.Deserialize(json, type.GetType()) as ITransport);
        }
示例#3
0
 public void OnEditDeserialize(IDeserialize objDeserialize)
 {
     assemblyMetadata = (AssemblyMetadata)objDeserialize.Deserialize();
     pathVariable     = objDeserialize.ToString();
     HierarchicalAreas.Clear();
     TreeViewLoaded(assemblyMetadata);
     MessageBox.Show(objDeserialize.ToString());
 }
        public IEnumerable <T> GetItems <T>()
        {
            Type   type = typeof(T);
            string path = ConfigurationSettings.AppSettings[type.Name];

            if (String.IsNullOrEmpty(path))
            {
                throw new Exception("There is no appropriate key in App.config");
            }
            return(serializer.Deserialize <T>(path));
        }
示例#5
0
        public static object DeserializePayloadUsingSpecificSerializer(
            this DescribedSerializationBase describedSerializationBase,
            IDeserialize deserializer,
            AssemblyMatchStrategy assemblyMatchStrategy = AssemblyMatchStrategy.AnySingleVersion)
        {
            if (describedSerializationBase == null)
            {
                throw new ArgumentNullException(nameof(describedSerializationBase));
            }

            if (deserializer == null)
            {
                throw new ArgumentNullException(nameof(deserializer));
            }

            var targetType = describedSerializationBase.PayloadTypeRepresentation.ResolveFromLoadedTypes(assemblyMatchStrategy);

            object result;

            var serializationFormat = describedSerializationBase.GetSerializationFormat();

            switch (serializationFormat)
            {
            case SerializationFormat.Binary:
                var describedSerializationBinary = (BinaryDescribedSerialization)describedSerializationBase;
                var serializedBytes = describedSerializationBinary.SerializedPayload;
                result = serializedBytes == null ? null : deserializer.Deserialize(serializedBytes, targetType);
                break;

            case SerializationFormat.String:
                var describedSerializationString = (StringDescribedSerialization)describedSerializationBase;
                var serializedString             = describedSerializationString.SerializedPayload;
                result = serializedString == null ? null : deserializer.Deserialize(serializedString, targetType);
                break;

            default:
                throw new NotSupportedException(Invariant($"{nameof(SerializationFormat)} - {serializationFormat} is not supported."));
            }

            return(result);
        }
        /// <summary>
        /// Deserializes a stream to the specified type.
        /// </summary>
        public static async ValueTask <T> DeserializeAsync <T>(IDeserialize deserializer, Stream source)
        {
            if (deserializer is null)
            {
                throw new ArgumentNullException(nameof(deserializer));
            }
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            string text;

            using (var reader = new StreamReader(source))
                text = await reader.ReadToEndAsync().ConfigureAwait(false);
            return(deserializer.Deserialize <T>(text));
        }
        private async Task LoadConfigForType <T>(ILog log, IDeserialize serializer, IConfiguration configuration, string appDataSubFolderName, Func <T, Task> foundItems)
        {
            if (foundItems == null)
            {
                return;
            }
            var configFolder = System.IO.Path.Combine(configuration.AppDataDirectory.FullName, appDataSubFolderName);

            if (!System.IO.Directory.Exists(configFolder))
            {
                System.IO.Directory.CreateDirectory(configFolder);
            }

            foreach (var file in System.IO.Directory.GetFiles(configFolder))
            {
                try
                {
                    if (System.IO.File.Exists(file))
                    {
                        var json = System.IO.File.ReadAllText(file);
                        if (!string.IsNullOrEmpty(json))
                        {
                            var items = serializer.Deserialize <T>(json);
                            if (items != null)
                            {
                                await foundItems(items);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    log.Error(e, "Could not load config from file:{0}", file);
                }
            }
        }
示例#8
0
 public static bool Load(IDeserialize deserializer, ref ObservableModelData obj)
 {
     return(deserializer.Deserialize(ref obj));
 }