Example #1
0
        public override void write(object obj, IProtocolFormatter formatter)
        {
            if (obj is IDictionary)
            {
                ITypeWriter typeWriter = MessageWriter.getWriter(typeof(IDictionary), formatter, false);
                typeWriter.write(obj, formatter);
                return;
            }

            IEnumerator   en        = (IEnumerator)obj;
            List <Object> arrayList = new List <Object>();

            while (en.MoveNext())
            {
                arrayList.Add(en.Current);
            }

            object[]    array  = arrayList.ToArray();
            ITypeWriter writer = MessageWriter.getWriter(array.GetType(), formatter, false);

            writer.write(array, formatter);
        }
Example #2
0
        public void write(object obj, IProtocolFormatter writer)
        {
            ITypeWriter typeWriter;

            if (obj is IDictionary)
            {
                typeWriter = MessageWriter.getWriter(typeof(IDictionary), writer, false);
                typeWriter.write(obj, writer);
                return;
            }

            /*
             * if( obj is Array )
             * {
             * base.write( obj, writer );
             * return;
             * }*/

            object[] array = null;

#if (FULL_BUILD)
            if (VectorUtils.IsVector(obj))
            {
                Type objectType  = obj.GetType();
                Type elementType = objectType.GetGenericArguments()[0];
                Type vectorType  = typeof(V3VectorWriter <>).MakeGenericType(elementType);
                typeWriter = (ITypeWriter)vectorType.GetConstructor(new Type[0]).Invoke(new object[0]);
                typeWriter.write(obj, writer);
                return;
            }

            if (obj is ICollection)
            {
                ICollection coll = (ICollection)obj;
                SerializationConfigHandler serializationConfig = (SerializationConfigHandler)ORBConfig.GetInstance().GetConfig("weborb/serialization");

                if (!serializationConfig.LegacyCollectionSerialization && !(obj is IWebORBArray))
                {
                    typeWriter = MessageWriter.getWriter(typeof(IWebORBArrayCollection), writer, false);
                    typeWriter.write(new WebORBArrayCollection(coll), writer);
                    return;
                }
                else
                {
                    array = new object[coll.Count];
                    coll.CopyTo(array, 0);
                }
            }
            else
            {
#endif
            IEnumerable collection  = (IEnumerable)obj;
            IEnumerator enumerator  = collection.GetEnumerator();
            List <Object> arrayList = new List <Object>();

            while (enumerator.MoveNext())
            {
                arrayList.Add(enumerator.Current);
            }

            array = arrayList.ToArray();
#if (FULL_BUILD)
        }
#endif

            typeWriter = MessageWriter.getWriter(array.GetType(), writer, false);
            typeWriter.write(array, writer);
            //base.write( array, writer );
        }
Example #3
0
        public void WriteObject(string className, IDictionary objectFields, IProtocolFormatter writer)
        {
            IEnumerator      en            = objectFields.Keys.GetEnumerator();
            V3ReferenceCache cache         = (V3ReferenceCache)writer.GetReferenceCache();
            String           traitsClassId = className;

            List <String> toRemove = new List <String>();

            while (en.MoveNext())
            {
                String fieldName = en.Current.ToString();
                object obj       = objectFields[fieldName];

                if (obj != null && obj is ICollection && ((ICollection)obj).Count == 0)
                {
                    toRemove.Add(fieldName);
                    continue;
                }

                if (obj != null && obj.GetType().IsArray&& ((Object[])obj).Length == 0)
                {
                    toRemove.Add(fieldName);
                    continue;
                }
            }

            foreach (Object key in toRemove)
            {
                objectFields.Remove(key);
            }

            en = objectFields.Keys.GetEnumerator();

            if (traitsClassId == null)
            {
                StringBuilder sb = new StringBuilder();

                while (en.MoveNext())
                {
                    sb.Append(en.Current.ToString());
                    sb.Append("-");
                }

                traitsClassId = sb.ToString();
                en.Reset();
            }

            if (cache.HasTraits(traitsClassId))
            {
                writer.DirectWriteBytes(new byte[] { (byte)Datatypes.OBJECT_DATATYPE_V3 });
                int    traitId = (int)cache.GetTraitsId(traitsClassId);
                byte[] bytes   = FlashorbBinaryWriter.GetVarIntBytes(0x1 | traitId << 2);
                writer.DirectWriteBytes(bytes);
            }
            else
            {
                writer.BeginWriteNamedObject(className, objectFields.Count);

                if (className == null)
                {
                    cache.AddToTraitsCache(traitsClassId);
                }

                while (en.MoveNext())
                {
                    string fieldName = en.Current.ToString();

                    if (Log.isLogging(LoggingConstants.DEBUG))
                    {
                        Log.log(LoggingConstants.DEBUG, "serializing property/field : " + fieldName);
                    }

                    writer.WriteFieldName(fieldName);
                }

                en.Reset();
            }

            while (en.MoveNext())
            {
                Object fieldName = en.Current;

                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "serializing property/field : " + fieldName);
                }

                //writer.BeginWriteFieldValue();

                //try
                //{
                //MessageWriter.writeObject( objectFields[ fieldName ], writer );
                //Log.log( "REFCACHE", "WRITING FIELD " + fieldName );
                object obj = objectFields[fieldName];

                if (obj != null && obj is ICollection && ((ICollection)obj).Count == 0)
                {
                    continue;
                }

                if (obj != null && obj.GetType().IsArray&& ((Object[])obj).Length == 0)
                {
                    continue;
                }

                ITypeWriter typeWriter = MessageWriter.getWriter(obj, writer);
                typeWriter.write(obj, writer);
                //Log.log( "REFCACHE", "DONE WRITING FIELD " + fieldName );
                //}
                //catch( Exception exception )
                //{
                //	if( Log.isLogging( LoggingConstants.ERROR ) )
                //		Log.log( LoggingConstants.ERROR, "unable to serialize object's field " + fieldName, exception );
                //}
                //finally
                //{
                //	writer.EndWriteFieldValue();
                //}
            }

            writer.EndWriteNamedObject();
        }
Example #4
0
        public void write(object obj, IProtocolFormatter writer)
        {
            Array arrayObj = null;

            if (obj is IWebORBArray)
            {
                IEnumerator en = ((IWebORBArray)obj).GetEnumerator();
                arrayObj = new object[((IWebORBArray)obj).Count];

                int i = 0;
                while (en.MoveNext())
                {
                    arrayObj.SetValue(en.Current, i++);
                }
            }
            else
            {
                //TODO: test out this cast!
                arrayObj = (Array)obj;
            }

            if (obj.GetType().IsAssignableFrom(typeof(Byte[])))
            {
                Byte[] byteArray = new byte[arrayObj.Length];

                for (int i = 0; i < arrayObj.Length; i++)
                {
                    byteArray[i] = (Byte)arrayObj.GetValue(i);
                }

                writer.WriteByteArray(byteArray);
                return;
            }

            if (arrayObj.Rank > 1)
            {
                multiDimArrayWriter.write(arrayObj, writer);
                return;
            }

            writer.BeginWriteArray(arrayObj.Length);

            bool genericArray = arrayObj.GetType().GetElementType().BaseType == null;

            if (genericArray)
            {
                for (int i = 0; i < arrayObj.Length; i++)
                {
                    //Log.log( "REFCACHE", "WRITING ARRAY ELEMENT " + i );
                    object      value      = arrayObj.GetValue(i);
                    ITypeWriter typeWriter = MessageWriter.getWriter(value, writer);
                    typeWriter.write(value, writer);
                    //Log.log( "REFCACHE", "DONE WRITING ARRAY ELEMENT " + i );
                }
            }
            else
            {
                ITypeWriter typeWriter    = null;
                ITypeWriter contextWriter = null;


                for (int i = 0; i < arrayObj.Length; i++)
                {
                    object value = arrayObj.GetValue(i);

                    if (contextWriter == null)
                    {
                        typeWriter    = MessageWriter.getWriter(value, writer);
                        contextWriter = writer.getContextWriter();
                    }
                    else
                    {
                        writer.setContextWriter(contextWriter);
                    }

                    typeWriter.write(value, writer);
                }
            }

            writer.EndWriteArray();
        }