/// <summary>
 ///   Deserializes given stream to hypermedia asynchronously.
 /// </summary>
 /// <param name="inputStream">
 ///   The input stream from which hypermedia would be deserialized.
 /// </param>
 /// <returns>
 ///   Task with Hypermedia.
 /// </returns>
 public static async Task <Hypermedia> DeserializeAsync(Stream inputStream)
 {
     return(await Task <Hypermedia> .Run(() =>
     {
         List <byte> buffer = new List <byte>();
         bool isEndOfStream = false;
         while (!isEndOfStream)
         {
             int b = inputStream.ReadByte();
             if (b != -1)
             {
                 buffer.Add((byte)b);
             }
             else
             {
                 isEndOfStream = true;
             }
         }
         string input = System.Text.Encoding.UTF8.GetString(buffer.ToArray());
         var deserializer = SerializationVersionTools.GetSerializationVersion(SerializationVersionTools.GetVersion(input));
         return deserializer.DeserializeFromString(input);
     }).ConfigureAwait(false));
 }
Beispiel #2
0
        private bool TryEntitiesListDeserializer(string input, Hypermedia parent, int count)
        {
            List <IEntity> entities = new List <IEntity>();

            if (!DeserializationTools.CheckStringFormat(input, true))
            {
                return(false);
            }

            input = input.TrimStart('[').TrimEnd(']').TrimStart('\r').TrimEnd('\n').TrimStart('\n').TrimEnd('\r');
            List <string> stringList      = new List <string>();
            bool          isEntitiesValid = TrySplitEntitiesList(input, parent, out stringList);

            if (!isEntitiesValid)
            {
                return(false);
            }
            if (stringList.Count != count)
            {
                return(false);
            }

            bool result = true;

            for (int i = 0; i < count; ++i)
            {
                string type  = new string(stringList[i].Skip(1).TakeWhile(s => s != ':').ToArray());
                int    index = int.Parse(new string(stringList[i].Skip(type.Length + 2).TakeWhile(s => s != ')').ToArray()));
                if (index != i)
                {
                    return(false);
                }
                switch (type)
                {
                case "file":
                    if (!File.IsSerializedStringValid(new string(stringList[i].SkipWhile(s => s != '[').ToArray()), parent))
                    {
                        result = false;
                    }
                    break;

                case "directory":
                    if (!Directory.IsSerializedStringValid(new string(stringList[i].SkipWhile(s => s != '[').ToArray()), parent))
                    {
                        result = false;
                    }
                    break;

                case "hypermedia":
                    var deserializer = SerializationVersionTools.GetSerializationVersion(SerializationVersionTools.GetVersion(new string(stringList[i].SkipWhile(s => s != '[').ToArray())));
                    if (!deserializer.IsSerializedStringValid(new string(stringList[i].SkipWhile(s => s != '[').ToArray()), parent))
                    {
                        result = false;
                    }
                    break;

                default:
                    return(false);
                }
            }

            return(result);
        }
Beispiel #3
0
        private List <IEntity> EntitiesListDeserializer(string input, Hypermedia parent, int count)
        {
            List <IEntity> entities = new List <IEntity>();

            DeserializationTools.CheckStringFormat(input, false);

            input = input.TrimStart('[').TrimEnd(']').TrimStart('\r').TrimEnd('\n').TrimStart('\n').TrimEnd('\r');
            var stringList = SplitEntitiesList(input, parent);

            if (stringList.Count != count)
            {
                throw new ArgumentException("Parsed string list does not match expected length", "count");
            }

            for (int i = 0; i < count; ++i)
            {
                string type  = new string(stringList[i].Skip(1).TakeWhile(s => s != ':').ToArray());
                int    index = int.Parse(new string(stringList[i].Skip(type.Length + 2).TakeWhile(s => s != ')').ToArray()));
                if (index != i)
                {
                    throw new ArgumentException("Possible serialization error encountered. Unexpected sequence", "input");
                }
                switch (type)
                {
                case "file":
                    entities.Add(File.DeserializeFromString(new string(stringList[i].SkipWhile(s => s != '[').ToArray()), parent, parent.Encoding));
                    break;

                case "directory":
                    entities.Add(Directory.DeserializeFromString(new string(stringList[i].SkipWhile(s => s != '[').ToArray()), parent, parent.Encoding));
                    break;

                case "hypermedia":
                    var deserializer = SerializationVersionTools.GetSerializationVersion(SerializationVersionTools.GetVersion(new string(stringList[i].SkipWhile(s => s != '[').ToArray())));
                    entities.Add(deserializer.DeserializeFromString(new string(stringList[i].SkipWhile(s => s != '[').ToArray()), parent));
                    break;

                default:
                    throw new ArgumentException("Possible serialization error encountered. Unexpected type", "input");
                }
            }

            if (count != entities.Count)
            {
                throw new ArgumentException("Serialized and deserialized collection length does not match", "count");
            }

            return(entities);
        }