public void WriteExternal(IPofWriter writer)
        {
            writer.WriteInt32(0, INTEGER);

            IPofWriter nested1 = writer.CreateNestedPofWriter(1);

            IPofWriter nested2 = nested1.CreateNestedPofWriter(0);

            nested2.WriteString(0, STRING);
            nested2.WriteObject(1, PERSON);
            nested2.WriteDoubleArray(2, DOUBLE_ARRAY);

            IPofWriter nested3 = nested2.CreateNestedPofWriter(3);

            nested3.WriteArray(0, STRING_ARRAY, typeof(String));

            nested2.WriteBoolean(4, false);
            nested2.WriteRemainder(null);

            nested1.WriteCollection(1, (ICollection <String>)set);
            nested1.WriteDouble(2, 2.0);
            nested1.WriteInt32(3, 5);
            nested1.WriteCollection(4, set, typeof(String));
            nested1.WriteObject(5, PERSON);
            nested1.WriteDouble(10, 2.222);

            writer.WriteDouble(2, 4.444);
            writer.WriteInt32(3, 15);
            writer.WriteObject(4, PERSON);
        }
Beispiel #2
0
            public void Serialize(IPofWriter pofWriter, object o)
            {
                var p = (Product)o;

                pofWriter.WriteObject(0, p.getBalance());
                pofWriter.WriteRemainder(null);
            }
        /// <summary>
        /// Serialize a user type instance to a POF stream by writing its
        /// state using the specified <see cref="IPofWriter"/> object.
        /// </summary>
        /// <param name="writer">
        /// The <b>IPofWriter</b> with which to write the object's state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public void Serialize(IPofWriter writer, object o)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                using (TextWriter txtWriter = new StringWriter(sb))
                {
                    new System.Xml.Serialization.XmlSerializer(o.GetType()).Serialize(txtWriter, o);
                }
                writer.WriteString(0, sb.ToString());
                writer.WriteRemainder(null);
            }
            catch (InvalidOperationException e)
            {
                string typeName = null;
                try
                {
                    typeName = writer.PofContext.GetTypeName(m_typeId);
                }
                catch (Exception)
                {}

                string actual = o.GetType().FullName;
                throw new IOException(
                          "An exception occurred writing an object"
                          + " user type to a POF stream: type-id=" + m_typeId
                          + (typeName == null ? "" : ", class-name=" + typeName)
                          + (actual == null ? "" : ", actual class-name=" + actual)
                          + ", exception=\n" + e);
            }
        }
        /// <summary>
        /// Serialize a user type instance to a POF stream by writing its
        /// state using the specified <see cref="IPofWriter"/> object.
        /// </summary>
        /// <remarks>
        /// An implementation of <b>IPofSerializer</b> is required to follow
        /// the following steps in sequence for writing out an object of a
        /// user type:
        /// <list type="number">
        /// <item>
        /// <description>
        /// The implementation may write any combination of the properties of
        /// the user type by using the "write" methods of the
        /// <b>IPofWriter</b>, but it must do so in the order of the property
        /// indexes.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// After all desired properties of the user type have been written,
        /// the implementation must terminate the writing of the user type by
        /// calling <see cref="IPofWriter.WriteRemainder"/>.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <param name="writer">
        /// The <b>IPofWriter</b> with which to write the object's state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public void Serialize(IPofWriter writer, object o)
        {
            MemoryStream stream = new MemoryStream();

            try
            {
                new BinaryFormatter().Serialize(stream, o);

                stream.Position = 0;
                writer.WriteObject(0, stream.GetBuffer());
                writer.WriteRemainder(null);
            }
            catch (SerializationException e)
            {
                string typeName = null;
                try
                {
                    typeName = writer.PofContext.GetTypeName(m_typeId);
                }
                catch (Exception)
                { }

                string actual = o.GetType().FullName;
                throw new IOException(
                          "An exception occurred writing an object"
                          + " user type to a POF stream: type-id=" + m_typeId
                          + (typeName == null ? "" : ", class-name=" + typeName)
                          + (actual == null ? "" : ", actual class-name=" + actual)
                          + ", exception=\n" + e);
            }
            finally
            {
                stream.Close();
            }
        }
Beispiel #5
0
        /// <summary>
        /// Serialize a user type instance to a POF stream by writing its
        /// state using the specified <see cref="IPofWriter"/> object.
        /// </summary>
        /// <remarks>
        /// An implementation of <b>IPofSerializer</b> is required to follow
        /// the following steps in sequence for writing out an object of a
        /// user type:
        /// <list type="number">
        /// <item>
        /// <description>
        /// If the object is evolvable, the implementation must set the
        /// version by calling <see cref="IPofWriter.VersionId"/>.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// The implementation may write any combination of the properties of
        /// the user type by using the "write" methods of the
        /// <b>IPofWriter</b>, but it must do so in the order of the property
        /// indexes.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// After all desired properties of the user type have been written,
        /// the implementation must terminate the writing of the user type by
        /// calling <see cref="IPofWriter.WriteRemainder"/>.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <param name="writer">
        /// The <b>IPofWriter</b> with which to write the object's state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public void Serialize(IPofWriter writer, object o)
        {
            IIdentity identity = (IIdentity)o;

            writer.WriteString(0, identity.Name);
            writer.WriteRemainder(null);
        }
        /// <summary>
        /// Serialize a user type instance to a POF stream by writing its
        /// state using the specified <see cref="IPofWriter"/> object.
        /// </summary>
        /// <remarks>
        /// An implementation of <b>IPofSerializer</b> is required to follow
        /// the following steps in sequence for writing out an object of a
        /// user type:
        /// <list type="number">
        /// <item>
        /// <description>
        /// If the object is evolvable, the implementation must set the
        /// version by calling <see cref="IPofWriter.VersionId"/>.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// The implementation may write any combination of the properties of
        /// the user type by using the "write" methods of the
        /// <b>IPofWriter</b>, but it must do so in the order of the property
        /// indexes.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// After all desired properties of the user type have been written,
        /// the implementation must terminate the writing of the user type by
        /// calling <see cref="IPofWriter.WriteRemainder"/>.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <param name="writer">
        /// The <b>IPofWriter</b> with which to write the object's state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public virtual void Serialize(IPofWriter writer, object o)
        {
            // set the version identifier
            bool       isEvolvable = o is IEvolvable;
            IEvolvable evolvable   = null;

            if (isEvolvable)
            {
                evolvable        = (IEvolvable)o;
                writer.VersionId =
                    Math.Max(evolvable.DataVersion, evolvable.ImplVersion);
            }

            // POF Annotation processing
            for (IEnumerator enmr = m_tmd.GetAttributes(); enmr.MoveNext();)
            {
                IAttributeMetadata <object> attr = (IAttributeMetadata <object>)enmr.Current;
                attr.Codec.Encode(writer, attr.Index, attr.Get(o));
            }

            // write out any future properties
            Binary remainder = null;

            if (isEvolvable)
            {
                remainder = evolvable.FutureData;
            }
            writer.WriteRemainder(remainder);
        }
Beispiel #7
0
        public void WriteExternal(IPofWriter writer)
        {
            writer.WriteInt32(0, INTEGER);

            IPofWriter nested1 = writer.CreateNestedPofWriter(1);

            IPofWriter nested2 = nested1.CreateNestedPofWriter(0);

            nested2.WriteString(0, STRING);
            nested2.WriteSingleArray(2, FLOAT_ARRAY);

            IPofWriter nested3 = nested2.CreateNestedPofWriter(3);

            nested3.WriteArray(0, STRING_ARRAY, typeof(String));

            nested2.WriteBoolean(4, false);
            nested2.WriteRemainder(null);

            IList list = (IList)set;

            nested1.WriteCollection(1, list);
            nested1.WriteDouble(2, 2.0);
            nested1.WriteInt32(3, 5);
            nested1.WriteCollection(4, set);
            nested1.WriteDouble(10, 2.222);

            writer.WriteDouble(2, 4.444);
            writer.WriteInt32(3, 15);
        }
Beispiel #8
0
            public void Serialize(IPofWriter pofWriter, object o)
            {
                var bal = (Balance)o;

                pofWriter.WriteDouble(0, bal.getBalance());
                pofWriter.WriteObject(1, bal.getCustomer());
                pofWriter.WriteRemainder(null);
            }
Beispiel #9
0
            public void Serialize(IPofWriter pofWriter, object o)
            {
                var c = (Customer)o;

                pofWriter.WriteString(0, c.getName());
                pofWriter.WriteObject(1, c.getProduct());
                pofWriter.WriteObject(2, c.getBalance());
                pofWriter.WriteRemainder(null);
            }
Beispiel #10
0
        /// <summary>
        /// Serialize a user type instance to a POF stream by writing its
        /// state using the specified <see cref="IPofWriter"/> object.
        /// </summary>
        /// <remarks>
        /// An implementation of <b>IPofSerializer</b> is required to follow
        /// the following steps in sequence for writing out an object of a
        /// user type:
        /// <list type="number">
        /// <item>
        /// <description>
        /// If the object is evolvable, the implementation must set the
        /// version by calling <see cref="IPofWriter.VersionId"/>.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// The implementation may write any combination of the properties of
        /// the user type by using the "write" methods of the
        /// <b>IPofWriter</b>, but it must do so in the order of the property
        /// indexes.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// After all desired properties of the user type have been written,
        /// the implementation must terminate the writing of the user type by
        /// calling <see cref="IPofWriter.WriteRemainder"/>.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <param name="writer">
        /// The <b>IPofWriter</b> with which to write the object's state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public void Serialize(IPofWriter writer, object o)
        {
            IPrincipal principal  = (IPrincipal)o;
            IList      identities = new ArrayList(1);

            identities.Add(principal.Identity);

            writer.WriteCollection(0, identities);
            writer.WriteRemainder(null);
        }
        /// <summary>
        /// Serialize an enum instance to a POF stream by writing its
        /// value using the specified <see cref="IPofWriter"/> object.
        /// </summary>
        /// <param name="writer">
        /// The <b>IPofWriter</b> with which to write the object's state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public void Serialize(IPofWriter writer, object o)
        {
            if (o == null || !o.GetType().IsEnum)
            {
                throw new ArgumentException(
                          "EnumPofSerializer can only be used to serialize enum types.");
            }

            writer.WriteString(0, o.ToString());
            writer.WriteRemainder(null);
        }
        /// <summary>
        /// Serialize a user type instance to a POF stream by writing its
        /// state using the specified <see cref="IPofWriter"/> object.
        /// </summary>
        /// <remarks>
        /// An implementation of <b>IPofSerializer</b> is required to follow
        /// the following steps in sequence for writing out an object of a
        /// user type:
        /// <list type="number">
        /// <item>
        /// <description>
        /// If the object is evolvable, the implementation must set the
        /// version by calling <see cref="IPofWriter.VersionId"/>.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// The implementation may write any combination of the properties of
        /// the user type by using the "write" methods of the
        /// <b>IPofWriter</b>, but it must do so in the order of the property
        /// indexes.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// After all desired properties of the user type have been written,
        /// the implementation must terminate the writing of the user type by
        /// calling <see cref="IPofWriter.WriteRemainder"/>.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <param name="writer">
        /// The <see cref="IPofWriter"/> with which to write the object's
        /// state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public virtual void Serialize(IPofWriter writer, object o)
        {
            IPortableObject portable;

            try
            {
                portable = (IPortableObject)o;
            }
            catch (InvalidCastException e)
            {
                string typeName = null;
                try
                {
                    typeName = writer.PofContext.GetTypeName(m_typeId);
                }
                catch (Exception)
                {}

                string actual = o.GetType().FullName;

                throw new IOException(
                          "An exception occurred writing an IPortableObject"
                          + " user type to a POF stream: type-id=" + m_typeId
                          + (typeName == null ? "" : ", class-name=" + typeName)
                          + (actual == null ? "" : ", actual class-name=" + actual)
                          + ", exception=\n" + e);
            }

            // set the version identifier
            bool       isEvolvable = portable is IEvolvable;
            IEvolvable evolvable   = null;

            if (isEvolvable)
            {
                evolvable        = (IEvolvable)portable;
                writer.VersionId =
                    Math.Max(evolvable.DataVersion, evolvable.ImplVersion);
            }

            // write out the object's properties
            portable.WriteExternal(writer);

            // write out any future properties
            Binary remainder = null;

            if (isEvolvable)
            {
                remainder = evolvable.FutureData;
            }
            writer.WriteRemainder(remainder);
        }
Beispiel #13
0
            /// <summary>
            /// Serialize a user type instance to a POF stream by writing its
            /// state using the specified <see cref="IPofWriter"/> object.
            /// </summary>
            /// <remarks>
            /// An implementation of <b>IPofSerializer</b> is required to follow
            /// the following steps in sequence for writing out an object of a
            /// user type:
            /// <list type="number">
            /// <item>
            /// <description>
            /// If the object is evolvable, the implementation must set the
            /// version by calling <see cref="IPofWriter.VersionId"/>.
            /// </description>
            /// </item>
            /// <item>
            /// <description>
            /// The implementation may write any combination of the properties of
            /// the user type by using the "write" methods of the
            /// <b>IPofWriter</b>, but it must do so in the order of the property
            /// indexes.
            /// </description>
            /// </item>
            /// <item>
            /// <description>
            /// After all desired properties of the user type have been written,
            /// the implementation must terminate the writing of the user type by
            /// calling <see cref="IPofWriter.WriteRemainder"/>.
            /// </description>
            /// </item>
            /// </list>
            /// </remarks>
            /// <param name="writer">
            /// The <b>IPofWriter</b> with which to write the object's state.
            /// </param>
            /// <param name="o">
            /// The object to serialize.
            /// </param>
            /// <exception cref="IOException">
            /// If an I/O error occurs.
            /// </exception>
            public void Serialize(IPofWriter writer, Object o)
            {
                var             buffer         = new BinaryMemoryStream(1024 * 8);
                PofStreamWriter userTypeWriter = new PofStreamWriter.UserTypeWriter(
                    new DataWriter(buffer), m_pofContext, TYPE_PORTABLE, -1);

                // COH-5065: due to the complexity of maintaining references
                // in future data, we won't support them for IEvolvable objects
                if (m_pofContext.IsReferenceEnabled && !(o is IEvolvable))
                {
                    userTypeWriter.EnableReference();
                }

                m_serializer.Serialize(userTypeWriter, o);

                String typeName = o.GetType().AssemblyQualifiedName;

                writer.WriteString(0, typeName);
                writer.WriteBinary(1, buffer.ToBinary());
                writer.WriteRemainder(null);

                Register(typeName);
            }
Beispiel #14
0
 /// <summary>
 /// Serialize a user type instance to a POF stream by writing its
 /// state using the specified <see cref="IPofWriter"/> object.
 /// </summary>
 /// <remarks>
 /// An implementation of <b>IPofSerializer</b> is required to follow
 /// the following steps in sequence for writing out an object of a
 /// user type:
 /// <list type="number">
 /// <item>
 /// <description>
 /// If the object is evolvable, the implementation must set the
 /// version by calling <see cref="IPofWriter.VersionId"/>.
 /// </description>
 /// </item>
 /// <item>
 /// <description>
 /// The implementation may write any combination of the properties of
 /// the user type by using the "write" methods of the
 /// <b>IPofWriter</b>, but it must do so in the order of the property
 /// indexes.
 /// </description>
 /// </item>
 /// <item>
 /// <description>
 /// After all desired properties of the user type have been written,
 /// the implementation must terminate the writing of the user type by
 /// calling <see cref="IPofWriter.WriteRemainder"/>.
 /// </description>
 /// </item>
 /// </list>
 /// </remarks>
 /// <param name="writer">
 /// The <b>IPofWriter</b> with which to write the object's state.
 /// </param>
 /// <param name="o">
 /// The object to serialize.
 /// </param>
 /// <exception cref="IOException">
 /// If an I/O error occurs.
 /// </exception>
 public void Serialize(IPofWriter writer, Object o)
 {
     writer.WriteBinary(0, ToBinary(o, m_serializer));
     writer.WriteRemainder(null);
     Register(o);
 }
 /// <see cref="IPofSerializer"/>
 void IPofSerializer.Serialize(IPofWriter writer, object o)
 {
     writer.WriteCollection(0, (HashSet)o);
     writer.WriteRemainder(null);
 }
        /// <summary>
        /// Serialize a user type instance to a POF stream by writing its
        /// state using the specified <see cref="IPofWriter"/> object.
        /// </summary>
        /// <remarks>
        /// An implementation of <b>IPofSerializer</b> is required to follow
        /// the following steps in sequence for writing out an object of a
        /// user type:
        /// <list type="number">
        /// <item>
        /// <description>
        /// If the object is evolvable, the implementation must set the
        /// version by calling <see cref="IPofWriter.VersionId"/>.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// The implementation may write any combination of the properties of
        /// the user type by using the "write" methods of the
        /// <b>IPofWriter</b>, but it must do so in the order of the property
        /// indexes.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// After all desired properties of the user type have been written,
        /// the implementation must terminate the writing of the user type by
        /// calling <see cref="IPofWriter.WriteRemainder"/>.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <param name="pofWriter">
        /// The <b>IPofWriter</b> with which to write the object's state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public void Serialize(IPofWriter pofWriter, Object o)
        {
            if (!(o is IPortableObject))
            {
                throw new IOException("Class [" + o.GetType() +
                                      "] does not implement a IPortableObject interface");
            }

            IPortableObject  po         = (IPortableObject)o;
            bool             fEvolvable = o is IEvolvableObject;
            IEvolvableObject et         = fEvolvable ? (IEvolvableObject)o : null;

            try
            {
                CacheFactory.Log("Serializing " + o.GetType(), CacheFactory.LogLevel.Max);

                IPofContext       ctx     = pofWriter.PofContext;
                IEnumerable <int> typeIds = GetTypeIds(o, ctx);

                foreach (int typeId in typeIds)
                {
                    IEvolvable e = null;
                    if (fEvolvable)
                    {
                        e = et.GetEvolvable(typeId);
                    }

                    IPofWriter writer = pofWriter.CreateNestedPofWriter(typeId, typeId);
                    if (fEvolvable)
                    {
                        writer.VersionId = Math.Max(e.DataVersion, e.ImplVersion);
                    }

                    Type type = GetTypeForTypeId(ctx, typeId);
                    if (type != null)
                    {
                        po.WriteExternal(writer);
                    }

                    writer.WriteRemainder(fEvolvable ? e.FutureData : null);
                }

                pofWriter.WriteRemainder(null);
            }
            catch (Exception e)
            {
                String sClass = null;
                try
                {
                    sClass = pofWriter.PofContext.GetTypeName(m_nTypeId);
                }
                catch (Exception)
                {
                }

                String sActual = null;
                try
                {
                    sActual = o.GetType().FullName;
                }
                catch (Exception)
                {
                }

                throw new IOException(
                          "An exception occurred writing a IPortableObject"
                          + " user type to a POF stream: type-id=" + m_nTypeId
                          + (sClass == null ? "" : ", class-name=" + sClass)
                          + (sActual == null ? "" : ", actual class-name=" + sActual)
                          + ", exception=" + e, e);
            }
        }
 /// <summary>
 /// Serialize a user type instance to a POF stream by writing its
 /// state using the specified <see cref="IPofWriter"/> object.
 /// </summary>
 /// <remarks>
 /// An implementation of <b>IPofSerializer</b> is required to follow
 /// the following steps in sequence for writing out an object of a
 /// user type:
 /// <list type="number">
 /// <item>
 /// <description>
 /// If the object is evolvable, the implementation must set the
 /// version by calling <see cref="IPofWriter.VersionId"/>.
 /// </description>
 /// </item>
 /// <item>
 /// <description>
 /// The implementation may write any combination of the properties of
 /// the user type by using the "write" methods of the
 /// <b>IPofWriter</b>, but it must do so in the order of the property
 /// indexes.
 /// </description>
 /// </item>
 /// <item>
 /// <description>
 /// After all desired properties of the user type have been written,
 /// the implementation must terminate the writing of the user type by
 /// calling <see cref="IPofWriter.WriteRemainder"/>.
 /// </description>
 /// </item>
 /// </list>
 /// </remarks>
 /// <param name="writer">
 /// The <b>IPofWriter</b> with which to write the object's state.
 /// </param>
 /// <param name="o">
 /// The object to serialize.
 /// </param>
 /// <exception cref="IOException">
 /// If an I/O error occurs.
 /// </exception>
 public void Serialize(IPofWriter writer, object o)
 {
     WriteException(writer, (Exception)o);
     writer.WriteRemainder(null);
 }