Exemplo n.º 1
0
        private string EntityListSerializer(List <IEntity> entities, Encoding encoding, Formatting formatting, uint tabulationsCount)
        {
            string outerTabulationBuilder = string.Empty;
            string innerTabulationBuilder = string.Empty;

            if (formatting == Formatting.Indented)
            {
                SerializationTools.InitTabulations(out outerTabulationBuilder, out innerTabulationBuilder, tabulationsCount);
            }
            StringBuilder builder = new StringBuilder();

            builder.AppendLine("[");
            for (int i = 0; i < entities.Count; ++i)
            {
                if (entities[i] is File)
                {
                    builder.AppendLine($"{innerTabulationBuilder}(file:{i})={(formatting == Formatting.Indented ? File.SerializeToString(entities[i] as File, encoding, formatting, tabulationsCount + 1) : File.SerializeToString(entities[i] as File, encoding))}{(i == entities.Count - 1 ? ";" : ",")}");
                }
                else if (entities[i] is Directory)
                {
                    builder.AppendLine($"{innerTabulationBuilder}(directory:{i})={(formatting == Formatting.Indented ? Directory.SerializeToString(entities[i] as Directory, encoding, formatting, tabulationsCount + 1) : Directory.SerializeToString(entities[i] as Directory, encoding))}{(i == entities.Count - 1 ? ";" : ",")}");
                }
                else if (entities[i] is Hypermedia)
                {
                    var serializator = SerializationVersionTools.GetSerializationVersion((entities[i] as Hypermedia).Version);
                    builder.AppendLine($"{innerTabulationBuilder}(hypermedia:{i})={(formatting == Formatting.Indented ? serializator.SerializeToString(entities[i] as Hypermedia, formatting, tabulationsCount + 1) : serializator.SerializeToString(entities[i] as Hypermedia))}{(i == entities.Count - 1 ? ";" : ",")}");
                }
            }
            builder.Append($"{outerTabulationBuilder}]");
            return(builder.ToString());
        }
 /// <summary>
 ///   Serializes given hypermedia to stream asynchronously.
 /// </summary>
 /// <param name="outputStream">
 ///   The output stream in which hypermedia would be serialized.
 /// </param>
 /// <param name="instance">
 ///   The instance of hypermedia that would be serialized.
 /// </param>
 /// <param name="formatting">
 ///   <see cref="Formatting">Formatting</see> options for serialization.
 /// </param>
 /// <returns>
 ///   Task or void.
 /// </returns>
 public static async Task SerializeAsync(Stream outputStream, Hypermedia instance, Formatting formatting)
 {
     await Task.Run(async() =>
     {
         var serializator = SerializationVersionTools.GetSerializationVersion(instance.Version);
         var buffer       = Encoding.UTF8.GetBytes(serializator.SerializeToString(instance, formatting, 0));
         await outputStream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
     }).ConfigureAwait(false);
 }
Exemplo n.º 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);
        }
 /// <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));
 }
Exemplo n.º 5
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);
        }