public void WriteClass(Type clazz)
        {
            ObjectSerializationHandler handler =
                ObjectSerializerRegistry.GetHandlerByObjectType(clazz);
            ObjectTypeMapper mapper =
                ObjectSerializerRegistry.GetMapperByObjectType(clazz);

            if (handler == null && clazz.IsArray)
            {
                //we may have special handlers for certain types of arrays
                //if handler is null, treat like any other array
                WriteByte(BinaryObjectEncoder.OBJECT_TYPE_ARRAY);
                WriteClass(clazz.GetElementType());
            }
            else if (mapper == null)
            {
                throw new ConnectorException("No serializer for class: " + clazz);
            }
            else
            {
                String typeName = mapper.HandledSerialType;
                WriteByte(BinaryObjectEncoder.OBJECT_TYPE_CLASS);
                WriteString(typeName, true);
            }
        }
예제 #2
0
        private static String EncodeClass(Type clazz)
        {
            ObjectSerializationHandler handler =
                ObjectSerializerRegistry.GetHandlerByObjectType(clazz);
            ObjectTypeMapper mapper =
                ObjectSerializerRegistry.GetMapperByObjectType(clazz);

            if (handler == null && clazz.IsArray)
            {
                //we may have special handlers for certain types of arrays
                //if handler is null, treat like any other array
                return(EncodeClass(clazz.GetElementType()) + "[]");
            }
            else if (mapper == null)
            {
                throw new ConnectorException("No serializer for class: " + clazz);
            }
            else
            {
                String typeName = mapper.HandledSerialType;
                return(typeName);
            }
        }
        public void WriteObject(ObjectEncoder encoder, Object obj)
        {
            if (_firstObject)
            {
                WriteInt(BinaryObjectEncoder.OBJECT_MAGIC);
                WriteInt(BinaryObjectEncoder.ENCODING_VERSION);
                _firstObject = false;
            }

            MemoryStream objectBuffer = new MemoryStream();

            _outputBufferStack.Add(objectBuffer);

            if (obj == null)
            {
                WriteByte(BinaryObjectEncoder.OBJECT_TYPE_NULL);
            }
            else
            {
                Type clazz = obj.GetType();
                WriteClass(clazz);
                ObjectSerializationHandler handler =
                    ObjectSerializerRegistry.GetHandlerByObjectType(clazz);
                if (handler == null)
                {
                    //we may have special handlers for certain types of arrays
                    //if handler is null, treat like any other array
                    if (obj is Array)
                    {
                        Array array  = (Array)obj;
                        int   length = array.Length;
                        for (int i = 0; i < length; i++)
                        {
                            Object val = array.GetValue(i);
                            StartAnonymousField();
                            WriteObject(encoder, val);
                            EndField();
                        }
                    }
                    else
                    {
                        throw new ConnectorException("No serializer for class: " + clazz);
                    }
                }
                else
                {
                    handler.Serialize(obj, encoder);
                }
            }

            //write end-object into the current obj buffer
            WriteByte(BinaryObjectEncoder.FIELD_TYPE_END_OBJECT);

            //pop the stack
            _outputBufferStack.RemoveAt(_outputBufferStack.Count - 1);

            //it's a top-level object, flush the constant pool
            if (_outputBufferStack.Count == 0)
            {
                WriteInt(_constantBuffer.Count);
                foreach (String constant in _constantBuffer)
                {
                    WriteString(constant, false);
                    WriteInt(_constantPool[constant]);
                }
                _constantBuffer.Clear();
            }

            //now write the actual object
            objectBuffer.Close();
            byte[] bytes = objectBuffer.ToArray();
            WriteBytes(bytes);
        }
        public Object ReadObject(ObjectDecoder decoder)
        {
            if (_firstObject)
            {
                int magic = ReadInt();
                if (magic != BinaryObjectEncoder.OBJECT_MAGIC)
                {
                    throw new ConnectorException("Bad magic number: " + magic);
                }
                int version = ReadInt();
                if (version != BinaryObjectEncoder.ENCODING_VERSION)
                {
                    throw new ConnectorException("Unexpected version: " + version);
                }
                _firstObject = false;
            }

            //if it's a top-level object, it's proceeded by a constant pool
            if (_readStateStack.Count == 0)
            {
                int size = ReadInt();
                for (int i = 0; i < size; i++)
                {
                    String constant = ReadString(false);
                    int    code     = ReadInt();
                    _constantPool[code] = constant;
                }
            }

            Type clazz = ReadClass();

            ReadState state = new ReadState();

            while (true)
            {
                byte type = ReadByte();
                if (type == BinaryObjectEncoder.FIELD_TYPE_END_OBJECT)
                {
                    break;
                }
                else if (type == BinaryObjectEncoder.FIELD_TYPE_ANONYMOUS_FIELD)
                {
                    byte[] bytes = ReadByteArray();
                    state.anonymousFields.Add(bytes);
                }
                else if (type == BinaryObjectEncoder.FIELD_TYPE_NAMED_FIELD)
                {
                    String fieldName = ReadString(true);
                    byte[] bytes     = ReadByteArray();
                    state.objectFields[fieldName] = bytes;
                }
                else
                {
                    throw new ConnectorException("Unknown type: " + type);
                }
            }
            _readStateStack.Add(state);

            Object rv;

            if (clazz == null)
            {
                rv = null;
            }
            else
            {
                ObjectSerializationHandler handler =
                    ObjectSerializerRegistry.GetHandlerByObjectType(clazz);
                if (handler == null)
                {
                    //we may have special handlers for certain types of arrays
                    //if handler is null, treat like any other array
                    if (clazz.IsArray)
                    {
                        int   length = GetNumAnonymousFields();
                        Array array  = Array.CreateInstance(clazz.GetElementType(),
                                                            length);
                        for (int i = 0; i < length; i++)
                        {
                            StartAnonymousField(i);
                            Object element = ReadObject(decoder);
                            array.SetValue(element, i);
                        }
                        rv = array;
                    }
                    else
                    {
                        throw new ConnectorException("No deserializer for type: " + clazz);
                    }
                }
                else
                {
                    rv = handler.Deserialize(decoder);
                }
            }
            _readStateStack.RemoveAt(_readStateStack.Count - 1);
            return(rv);
        }
예제 #5
0
 private Object ReadObjectInternal()
 {
     if (_expectedClass != null)
     {
         ObjectSerializationHandler handler =
             ObjectSerializerRegistry.GetHandlerByObjectType(_expectedClass);
         if (handler == null)
         {
             if (_expectedClass.IsArray)
             {
                 IList <Object> temp = new List <Object>();
                 for (XmlElement child = XmlUtil.GetFirstChildElement(_node); child != null;
                      child = XmlUtil.GetNextElement(child))
                 {
                     XmlObjectDecoder sub = new XmlObjectDecoder(child, null);
                     Object           obj = sub.ReadObject();
                     temp.Add(obj);
                 }
                 int   length = temp.Count;
                 Array array  = Array.CreateInstance(_expectedClass.GetElementType(), length);
                 for (int i = 0; i < length; i++)
                 {
                     Object element = temp[i];
                     array.SetValue(element, i);
                 }
                 return(array);
             }
             else
             {
                 throw new ConnectorException("No deserializer for type: " + _expectedClass);
             }
         }
         else
         {
             return(handler.Deserialize(this));
         }
     }
     else if (_node.LocalName.Equals("null"))
     {
         return(null);
     }
     else if (_node.LocalName.Equals("Array"))
     {
         String componentType = XmlUtil.GetAttribute(_node, "componentType");
         if (componentType == null)
         {
             componentType = "Object";
         }
         Type           componentClass = DecodeClass(componentType);
         IList <Object> temp           = new List <Object>();
         for (XmlElement child = XmlUtil.GetFirstChildElement(_node); child != null;
              child = XmlUtil.GetNextElement(child))
         {
             XmlObjectDecoder sub = new XmlObjectDecoder(child, null);
             Object           obj = sub.ReadObject();
             temp.Add(obj);
         }
         int   length = temp.Count;
         Array array  = Array.CreateInstance(componentClass,
                                             length);
         for (int i = 0; i < length; i++)
         {
             Object element = temp[i];
             array.SetValue(element, i);
         }
         return(array);
     }
     else
     {
         Type clazz =
             DecodeClass(_node.LocalName);
         ObjectSerializationHandler handler =
             ObjectSerializerRegistry.GetHandlerByObjectType(clazz);
         if (handler == null)
         {
             throw new ConnectorException("No deserializer for type: " + clazz);
         }
         else
         {
             return(handler.Deserialize(this));
         }
     }
 }
예제 #6
0
 /// <summary>
 /// Writes the object
 /// </summary>
 /// <param name="object"></param>
 /// <param name="inline"></param>
 /// <returns>The type name (regardless of whether it was inlined)</returns>
 String WriteObjectInternal(Object obj, bool inline)
 {
     if (obj == null)
     {
         if (inline)
         {
             throw new ArgumentException("null cannot be inlined");
         }
         BeginElement("null");
         EndElement();
         return("null");
     }
     else
     {
         Type clazz = obj.GetType();
         ObjectSerializationHandler handler =
             ObjectSerializerRegistry.GetHandlerByObjectType(clazz);
         if (handler == null)
         {
             //we may have special handlers for certain types of arrays
             //if handler is null, treat like any other array
             if (clazz.IsArray)
             {
                 if (!inline)
                 {
                     String componentTypeName = EncodeClass(clazz.GetElementType());
                     BeginElement("Array");
                     WriteAttributeInternal("componentType", componentTypeName);
                 }
                 Array array  = (Array)obj;
                 int   length = array.Length;
                 for (int i = 0; i < length; i++)
                 {
                     Object val = array.GetValue(i);
                     WriteObjectInternal(val, false);
                 }
                 if (!inline)
                 {
                     EndElement();
                 }
                 return("Array");
             }
             else
             {
                 throw new ConnectorException("No serializer for class: " + clazz);
             }
         }
         else
         {
             String typeName = EncodeClass(clazz);
             if (!inline)
             {
                 BeginElement(typeName);
             }
             handler.Serialize(obj, this);
             if (!inline)
             {
                 EndElement();
             }
             return(typeName);
         }
     }
 }