public void TestIMessagePackSerializerUnpackFrom_Invalid() { IMessagePackSerializer target = CreateTarget <int>(); using (var buffer = new MemoryStream(new byte[] { 0xC2 })) using (var unpacker = Unpacker.Create(buffer)) { unpacker.Read(); Assert.Throws <SerializationException>(() => target.UnpackFrom(unpacker)); } }
private object UnpackFromCore(Unpacker unpacker, IMessagePackSerializer underlyingTypeSerializer) { if (unpacker.LastReadData.IsNil) { return(Activator.CreateInstance(TargetType)); } else { return(_nullableTImplicitOperator.Invoke(null, new object[] { underlyingTypeSerializer.UnpackFrom(unpacker) })); } }
public void TestIMessagePackSerializerUnpackFrom_Valid_Success() { IMessagePackSerializer target = CreateTarget <int>(); using (var buffer = new MemoryStream(new byte[] { 0x1 })) using (var unpacker = Unpacker.Create(buffer)) { unpacker.Read(); var result = target.UnpackFrom(unpacker); Assert.That(result, Is.EqualTo(1)); } }
public static object BytesToObj(Type type, byte[] data) { if (data == null || data.Length == 0) { return(null); } IMessagePackSerializer ser = serDic.GetOrAdd(type, MessagePackSerializer.Create(type)); MemoryStream stream = new MemoryStream(data); Unpacker up = Unpacker.Create(stream); up.Read(); return(ser.UnpackFrom(up)); }
/// <summary> /// Deserialize object from the <see cref="Stream"/>. /// </summary> /// <param name="source"><see cref="IMessagePackSerializer"/> object.</param> /// <param name="stream">Source <see cref="Stream"/>.</param> /// <returns>Deserialized object.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/> is <c>null</c>. /// Or <paramref name="stream"/> is <c>null</c>. /// </exception> /// <exception cref="System.Runtime.Serialization.SerializationException"> /// Failed to deserialize from <paramref name="stream"/>. /// </exception> public static object Unpack(this IMessagePackSerializer source, Stream stream) { if (source == null) { throw new ArgumentNullException("source"); } if (stream == null) { throw new ArgumentNullException("stream"); } // Unpacker does not have finalizer, so just avoiding unpacker disposing prevents stream closing. var unpacker = Unpacker.Create(stream); if (!unpacker.Read()) { throw SerializationExceptions.NewUnexpectedEndOfStream(); } return(source.UnpackFrom(unpacker)); }
public void TestIMessagePackSerializerUnpackFrom_UnpackerIsNull() { IMessagePackSerializer target = CreateTarget <int>(); Assert.Throws <ArgumentNullException>(() => target.UnpackFrom(null)); }