Exemple #1
0
        public static byte[] SerializeToBytes(object obj, int serializationType)
        {
            IProtocolFormatter formatter;

            switch (serializationType)
            {
            case AMF0:
                formatter = new AmfFormatter();
                break;

#if (!UNIVERSALW8 && !SILVERLIGHT && !PURE_CLIENT_LIB && !WINDOWS_PHONE8)
            case WOLF:
                formatter = new WolfFormatter();
                break;
#endif
            case AMF3:
            default:
                formatter = new AmfV3Formatter();
                break;
            }

            MessageWriter.writeObject(obj, formatter);
            ProtocolBytes protocolBytes = formatter.GetBytes();
            formatter.Cleanup();

            return(protocolBytes.bytes);
        }
Exemple #2
0
        public void write(object obj, IProtocolFormatter formatter)
        {
            ReferenceCache referenceCache = formatter.GetReferenceCache();
            int            refId          = referenceCache.GetObjectId(obj);

            if (refId != -1)
            {
#if FULL_BUILD
                if (VectorUtils.IsVector(obj) && formatter is AmfV3Formatter)
                {
                    Type           collectionType = obj.GetType();
                    AmfV3Formatter amfV3Formatter = (AmfV3Formatter)formatter;

                    if (VectorUtils.isIntType(collectionType))
                    {
                        amfV3Formatter.WriteVarIntWithoutMarker(Datatypes.INT_VECTOR_V3);
                    }
                    else if (VectorUtils.isUIntType(collectionType))
                    {
                        amfV3Formatter.WriteVarIntWithoutMarker(Datatypes.UINT_VECTOR_V3);
                    }
                    else if (VectorUtils.isNumberType(collectionType))
                    {
                        amfV3Formatter.WriteVarIntWithoutMarker(Datatypes.DOUBLE_VECTOR_V3);
                    }
                    else
                    {
                        amfV3Formatter.WriteVarIntWithoutMarker(Datatypes.OBJECT_VECTOR_V3);
                    }

                    amfV3Formatter.WriteVarIntWithoutMarker(refId << 1);
                }
                else
#endif
                if (obj is IWebORBArrayCollection || obj is IDictionary)
                {
                    formatter.WriteObjectReference(refId);
                }
                else if (obj is ICollection || obj is Array || obj is IWebORBArray)
                {
                    formatter.WriteArrayReference(refId);
                }
            }
            else
            {
                referenceCache.AddObject(obj);
                formatter.getContextWriter().write(obj, formatter);
            }
        }
Exemple #3
0
        public static byte[] ToBytes(Object obj, int type)
        {
            IProtocolFormatter formatter = null;

            switch (type)
            {
            case AMF0:
                formatter = new AmfFormatter();
                break;

            case AMF3:
                formatter = new AmfV3Formatter();
                break;

            case JSON:
                formatter = new JsonRPCFormatter();
                break;

#if (!UNIVERSALW8 && !SILVERLIGHT && !PURE_CLIENT_LIB && !WINDOWS_PHONE8)
            case WOLF:
                formatter = new WolfFormatter();
                break;
#endif
            default:
                throw new Exception("Unknown protocol type");
            }

            MessageWriter.writeObject(obj, formatter);
            ProtocolBytes bytes = formatter.GetBytes();
            formatter.Cleanup();

            if (bytes.bytes.Length != bytes.length)
            {
                byte[] result = new byte[bytes.length];
                Array.Copy(bytes.bytes, result, bytes.length);
                return(result);
            }
            else
            {
                return(bytes.bytes);
            }
        }