Пример #1
0
        static bool TryGetNetObjectFromAmqpObject(object amqpObject, MappingType mappingType, out object netObject)
        {
            netObject = null;
            if (amqpObject == null)
            {
                return(true);
            }

            switch (SerializationUtilities.GetTypeId(amqpObject))
            {
            case PropertyValueType.Byte:
            case PropertyValueType.SByte:
            case PropertyValueType.Int16:
            case PropertyValueType.Int32:
            case PropertyValueType.Int64:
            case PropertyValueType.UInt16:
            case PropertyValueType.UInt32:
            case PropertyValueType.UInt64:
            case PropertyValueType.Single:
            case PropertyValueType.Double:
            case PropertyValueType.Boolean:
            case PropertyValueType.Decimal:
            case PropertyValueType.Char:
            case PropertyValueType.Guid:
            case PropertyValueType.DateTime:
            case PropertyValueType.String:
                netObject = amqpObject;
                break;

            case PropertyValueType.Unknown:
                if (amqpObject is AmqpSymbol amqpObjectAsAmqpSymbol)
                {
                    netObject = (amqpObjectAsAmqpSymbol).Value;
                }
                else if (amqpObject is ArraySegment <byte> amqpObjectAsArraySegment)
                {
                    ArraySegment <byte> binValue = amqpObjectAsArraySegment;
                    if (binValue.Count == binValue.Array.Length)
                    {
                        netObject = binValue.Array;
                    }
                    else
                    {
                        var buffer = new byte[binValue.Count];
                        Buffer.BlockCopy(binValue.Array, binValue.Offset, buffer, 0, binValue.Count);
                        netObject = buffer;
                    }
                }
                else if (amqpObject is DescribedType amqpObjectAsDescribedType)
                {
                    if (amqpObjectAsDescribedType.Descriptor is AmqpSymbol)
                    {
                        var amqpSymbol = (AmqpSymbol)amqpObjectAsDescribedType.Descriptor;
                        if (amqpSymbol.Equals((AmqpSymbol)UriName))
                        {
                            netObject = new Uri((string)amqpObjectAsDescribedType.Value);
                        }
                        else if (amqpSymbol.Equals((AmqpSymbol)TimeSpanName))
                        {
                            netObject = new TimeSpan((long)amqpObjectAsDescribedType.Value);
                        }
                        else if (amqpSymbol.Equals((AmqpSymbol)DateTimeOffsetName))
                        {
                            netObject = new DateTimeOffset(new DateTime((long)amqpObjectAsDescribedType.Value, DateTimeKind.Utc));
                        }
                    }
                }
                else if (mappingType == MappingType.ApplicationProperty)
                {
                    throw Fx.Exception.AsError(new SerializationException(Resources.FailedToSerializeUnsupportedType.FormatForUser(amqpObject.GetType().FullName)));
                }
                else if (amqpObject is AmqpMap map)
                {
                    var dictionary = new Dictionary <string, object>();
                    foreach (var pair in map)
                    {
                        dictionary.Add(pair.Key.ToString(), pair.Value);
                    }

                    netObject = dictionary;
                }
                else
                {
                    netObject = amqpObject;
                }
                break;
            }

            return(netObject != null);
        }
Пример #2
0
        internal static bool TryGetAmqpObjectFromNetObject(object netObject, MappingType mappingType, out object amqpObject)
        {
            amqpObject = null;
            if (netObject == null)
            {
                return(true);
            }

            switch (SerializationUtilities.GetTypeId(netObject))
            {
            case PropertyValueType.Byte:
            case PropertyValueType.SByte:
            case PropertyValueType.Int16:
            case PropertyValueType.Int32:
            case PropertyValueType.Int64:
            case PropertyValueType.UInt16:
            case PropertyValueType.UInt32:
            case PropertyValueType.UInt64:
            case PropertyValueType.Single:
            case PropertyValueType.Double:
            case PropertyValueType.Boolean:
            case PropertyValueType.Decimal:
            case PropertyValueType.Char:
            case PropertyValueType.Guid:
            case PropertyValueType.DateTime:
            case PropertyValueType.String:
                amqpObject = netObject;
                break;

            case PropertyValueType.Stream:
                if (mappingType == MappingType.ApplicationProperty)
                {
                    amqpObject = StreamToBytes((Stream)netObject);
                }
                break;

            case PropertyValueType.Uri:
                amqpObject = new DescribedType((AmqpSymbol)UriName, ((Uri)netObject).AbsoluteUri);
                break;

            case PropertyValueType.DateTimeOffset:
                amqpObject = new DescribedType((AmqpSymbol)DateTimeOffsetName, ((DateTimeOffset)netObject).UtcTicks);
                break;

            case PropertyValueType.TimeSpan:
                amqpObject = new DescribedType((AmqpSymbol)TimeSpanName, ((TimeSpan)netObject).Ticks);
                break;

            case PropertyValueType.Unknown:
                if (netObject is Stream netObjectAsStream)
                {
                    if (mappingType == MappingType.ApplicationProperty)
                    {
                        amqpObject = StreamToBytes(netObjectAsStream);
                    }
                }
                else if (mappingType == MappingType.ApplicationProperty)
                {
                    throw Fx.Exception.AsError(new SerializationException(Resources.FailedToSerializeUnsupportedType.FormatForUser(netObject.GetType().FullName)));
                }
                else if (netObject is byte[] netObjectAsByteArray)
                {
                    amqpObject = new ArraySegment <byte>(netObjectAsByteArray);
                }
                else if (netObject is IList)
                {
                    // Array is also an IList
                    amqpObject = netObject;
                }
                else if (netObject is IDictionary netObjectAsDictionary)
                {
                    amqpObject = new AmqpMap(netObjectAsDictionary);
                }
                break;
            }

            return(amqpObject != null);
        }