Exemplo n.º 1
0
        /// <summary>
        /// Deserializes an object of the specified type from <paramref name="input"/>.
        /// </summary>
        /// <typeparam name="T">The type of object to deserialize.</typeparam>
        /// <param name="input">The stream to read from.</param>
        /// <returns>The deserialized object. <see langword="null"/> values are possible
        /// if <see langword="null"/> was originally serialized into the stream.</returns>
        /// <exception cref="ArgumentNullException">
        ///		<para><paramref name="input"/> is <see langword="null"/>.</para>
        /// </exception>
        /// <exception cref="ArgumentException">
        ///		<para><paramref name="input.CanSeek"/> is <see langword="false"/>.</para>
        /// </exception>
        public static T Deserialize <T>(Stream input)
        {
            ArgumentAssert.IsNotNull(input, "input");
            if (!input.CanSeek)
            {
                throw new ArgumentException("input.CanSeek must be true", "input");
            }

            var serializer = BarfSerializers.Get <T>(PartFlags.None);
            var args       = new BarfDeserializationArgs(SerializerFactory.GetReader(input));

            return(serializer.Deserialize(args));
        }
Exemplo n.º 2
0
 public static void FillRandom <T>(ref T instance, FillArgs args, PartFlags flags, string partName)
 {
     if (flags.IsSet(PartFlags.Dynamic))
     {
         args.RaiseSkippedWarning(partName, "dynamic or polymorphic fields can't be filled");
         instance = default(T);
         return;
     }
     if (args.BeginDeferredFill <T>())
     {
         var tester = BarfSerializers.GetTester <T>(flags);
         tester.Fill(ref instance, args);
         args.EndDeferredFill <T>();
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Serializes <see cref="value"/> into <see cref="output"/>.
        /// </summary>
        /// <typeparam name="T">The type of object to serialize.</typeparam>
        /// <param name="value">The object to serialize.</param>
        /// <param name="output">The stream to write to.</param>
        /// <exception cref="ArgumentNullException">
        ///		<para><paramref name="output"/> is <see langword="null"/>.</para>
        /// </exception>
        public static void Serialize <T>(T value, Stream output)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }

            var serializer = BarfSerializers.Get <T>(PartFlags.None);
            var stream     = output.CanSeek ? output : new MemoryStream();
            var args       = new BarfSerializationArgs(SerializerFactory.GetWriter(stream));

            serializer.Serialize(value, args);

            if (!output.CanSeek)
            {
                output.Write(((MemoryStream)stream).GetBuffer(), 0, (int)stream.Length);
            }
        }
Exemplo n.º 4
0
        public static void AssertAreEqual <T>(T expected, T actual, PartFlags flags, AssertArgs args, string partName)
        {
            if (flags.IsSet(PartFlags.Dynamic))
            {
                if (expected == null)
                {
                    Assert.AreEqual(null, actual);
                    return;
                }
                Assert.AreEqual(expected.GetType(), actual.GetType(), partName);

                var tester = BarfSerializers.GetTester(expected.GetType(), flags & ~PartFlags.Dynamic);
                tester.AssertAreEqual(expected, actual, args);
            }
            else
            {
                var tester = BarfSerializers.GetTester <T>(flags);
                tester.AssertAreEqual(expected, actual, args);
            }
        }
Exemplo n.º 5
0
 public static void WritePart <T>(T part, BarfSerializationArgs args, PartFlags flags)
 {
     if (flags.IsSet(PartFlags.Dynamic))
     {
         if (!typeof(T).IsValueType && part == null)
         {
             args.Writer.WriteVarInt32(-1);
         }
         else
         {
             var type = part.GetType();
             args.Writer.WriteVarInt32(args.TypeTable.Include(type));
             var serializer = BarfSerializers.Get(type, flags & ~PartFlags.Dynamic);
             serializer.SerializeObject(part, args);
         }
     }
     else
     {
         BarfSerializers.Get <T>(flags).Serialize(part, args);
     }
 }
Exemplo n.º 6
0
 public static void ReadPart <T>(ref T instance, BarfDeserializationArgs args, PartFlags flags)
 {
     if (flags.IsSet(PartFlags.Dynamic))
     {
         var index = args.Reader.ReadVarInt32();
         if (index < 0)
         {
             instance = default(T);
         }
         else
         {
             var    type       = args.TypeTable.GetType(index);
             var    serializer = BarfSerializers.Get(type, flags & ~PartFlags.Dynamic);
             object obj        = instance;
             serializer.InnerDeserializeObject(ref obj, args);
             instance = (T)obj;
         }
     }
     else
     {
         BarfSerializers.Get <T>(flags).InnerDeserialize(ref instance, args);
     }
 }