/// <summary> /// Converts the ActionScript value to a native value of the specified type. /// </summary> /// <remarks> /// The default implementation visits the <paramref name="asValue"/> and forwards the /// data to one of the specialized MapToXXX methods accordingly. /// </remarks> /// <param name="serializer">The serializer to use</param> /// <param name="asValue">The ActionScript value to convert</param> /// <param name="nativeType">The native type to produce</param> /// <returns>The native value</returns> /// <exception cref="ActionScriptException">Thrown if the mapping is not supported</exception> public virtual object ToNative(IActionScriptSerializer serializer, IASValue asValue, Type nativeType) { Visitor visitor = new Visitor(this, nativeType); asValue.AcceptVisitor(serializer, visitor); return(visitor.Result); }
public void WriteObject_ByteArrays_WrittenInSegments(AMFObjectEncoding objectEncoding, byte[] expected, byte[] bytes) { IASValue byteArray = Mocks.CreateMock <IASValue>(); byteArray.AcceptVisitor(serializer, null); LastCall.IgnoreArguments().Do((AcceptVisitorDelegate) delegate(IActionScriptSerializer theSerializer, IASValueVisitor visitor) { ArraySegment <byte>[] segments = new ArraySegment <byte> [bytes.Length]; for (int i = 0; i < bytes.Length; i++) { segments[i] = new ArraySegment <byte>(bytes, i, 1); } visitor.VisitByteArray(serializer, bytes.Length, segments); }); Mocks.ReplayAll(); output.ObjectEncoding = objectEncoding; output.BeginObjectStream(); output.WriteObject(byteArray); output.EndObjectStream(); CollectionAssert.AreElementsEqual(expected, stream.ToArray()); }
public void WriteMessageWrapsErrorsWithAMFException() { // Deliberately inject an exception. // This should be caught and wrapped by the AMFMessageWriter code. IASValue mockValue = Mocks.CreateMock <IASValue>(); mockValue.AcceptVisitor(serializer, null); LastCall.IgnoreArguments().Throw(new Exception("Something bad happened.")); Mocks.ReplayAll(); AMFMessage message = new AMFMessage(); message.Version = 0x1234; message.Headers.Add(new AMFHeader("abc", true, mockValue)); AMFMessageWriter.WriteAMFMessage(output, message); }
public void WriteObject(IASValue value) { if (value == null) { WriteNull(); return; } // Check if we have seen the object reference before. int referenceId; if (referenceCache.TryGetValue(value, out referenceId)) { WriteReference(referenceId); return; } // Otherwise write it out fresh. currentValue = value; value.AcceptVisitor(output.Serializer, this); currentValue = null; }
/// <summary> /// Converts the ActionScript value to a native value of the specified type. /// </summary> /// <remarks> /// The default implementation visits the <paramref name="asValue"/> and forwards the /// data to one of the specialized MapToXXX methods accordingly. /// </remarks> /// <param name="serializer">The serializer to use</param> /// <param name="asValue">The ActionScript value to convert</param> /// <param name="nativeType">The native type to produce</param> /// <returns>The native value</returns> /// <exception cref="ActionScriptException">Thrown if the mapping is not supported</exception> public virtual object ToNative(IActionScriptSerializer serializer, IASValue asValue, Type nativeType) { Visitor visitor = new Visitor(this, nativeType); asValue.AcceptVisitor(serializer, visitor); return visitor.Result; }