Esempio n. 1
0
 public static void Write(XmlWriter writer, T proto)
 {
     IProtoColumn <T>[] columns = instance.Value.columns;
     for (int index = 0; index < columns.Length; index++)
     {
         IProtoColumn <T> column = columns[index];
         if (column != null)
         {
             column.Write(writer, proto);
         }
     }
 }
Esempio n. 2
0
        public static void Write(JsonWriter writer, T proto)
        {
            IProtoColumn <T>[] columns = instance.Value.columns;

            writer.WriteStartObject();
            for (int index = 0; index < columns.Length; index++)
            {
                IProtoColumn <T> column = columns[index];
                if (column != null)
                {
                    column.Write(writer, proto);
                }
            }
            writer.WriteEndObject();
        }
Esempio n. 3
0
        /// <summary>
        /// Serialize a tuple into stream.
        ///
        /// The following data are writen into the stream
        /// 1. bitmask bytes length
        /// 2. bitmarks bytes
        /// 3. serialized value for each column in columns masks.
        ///
        /// </summary>
        /// <typeparam name="T">tuple type</typeparam>
        /// <param name="tuple">tuple object</param>
        /// <param name="stream">the stread to write the tuple into</param>
        /// <param name="columnMasks">column masks for serialized columns</param>
        public static void Serialize <T>(this Stream stream, T proto, ProtoSchema schema)
        {
            using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8, true))
            {
                int    bitPos   = (int)stream.Position;
                byte[] bitmarks = new byte[schema.ColumnMasks.Length];
                writer.Write((byte)bitmarks.Length);
                writer.Write(bitmarks, 0, bitmarks.Length);

                for (int index = 0; index < schema.Columns.Length; index++)
                {
                    IProtoColumn column = schema.Columns[index];
                    if (column.ID >= schema.ColumnMasks.Length * 8)
                    {
                        break;
                    }

                    if (!schema.ColumnMasks.GetBitmask(column.ID))
                    {
                        bitmarks.SetBitmask(column.ID, false);
                        continue;
                    }

                    object value = proto.GetValue(column);
                    if (CommonUtils.Equals(value, column.DefaultValue))
                    {
                        bitmarks.SetBitmask(column.ID, false);
                    }
                    else
                    {
                        bitmarks.SetBitmask(column.ID, true);
                        column.Write(writer, value);
                    }
                }

                int endPos = (int)stream.Position;
                stream.Position = bitPos;

                writer.Write((byte)bitmarks.Length);
                writer.Write(bitmarks, 0, bitmarks.Length);

                stream.Position = endPos;
            }
        }
Esempio n. 4
0
 public static void Write2(TProtocol oprot, T value)
 {
     IProtoColumn <T>[] columns = instance.Value.columns;
     oprot.IncrementRecursionDepth();
     try
     {
         TStruct struc = new TStruct(typeof(T).Name);
         oprot.WriteStructBegin(struc);
         for (int index = 0; index < columns.Length; index++)
         {
             IProtoColumn <T> column = columns[index];
             if (column != null)
             {
                 column.Write(oprot, value);
             }
         }
         oprot.WriteFieldStop();
         oprot.WriteStructEnd();
     }
     finally
     {
         oprot.DecrementRecursionDepth();
     }
 }