Exemplo n.º 1
0
        /// <summary>
        /// Obsolete - only backward compatibility
        /// </summary>
        internal static void WriteValue(BinaryWriter writer, DataTable dt, Encoding encoding)
        {
            if (dt == null)
            {
                WriteNullValue(writer);
                return;
            }
            // column count
            NumericSerializers.WriteUIntNullableMemberCount(writer, (uint)dt.Columns.Count);

            // table name
            WriteValue(writer, dt.TableName, encoding);

            // columns
            foreach (DataColumn col in dt.Columns)
            {
                WriteValue(writer, col.Caption, encoding);
                WriteValue(writer, col.ColumnName, encoding);

                var dataType = col.DataType?.ToString();
                if (dataType != null && dataType.StartsWith("System."))
                {
                    dataType = "0." + dataType.Remove(0, "System.".Length);
                }
                WriteValue(writer, dataType, encoding);
            }

            NumericSerializers.WriteVarInt(writer, dt.Rows.Count);
            foreach (DataRow row in dt.Rows)
            {
                for (var colIndex = 0; colIndex < row.ItemArray.Length; colIndex++)
                {
                    var item     = row.ItemArray[colIndex];
                    var itemType = dt.Columns[colIndex].DataType;

                    var basicTypeInfo = BoisTypeCache.GetBasicType(itemType);

                    if (basicTypeInfo.KnownType == EnBasicKnownType.Unknown)
                    {
                        throw new Exception($"Serialization of DataTable with item type of '{itemType}' is not supported.");
                    }

                    var itemToWrite = item;
                    if (itemType == typeof(string))
                    {
                        itemToWrite =
                            item == DBNull.Value
                                                                ? null
                                                                : item.ToString();
                    }
                    else if (item == DBNull.Value)
                    {
                        itemToWrite = null;
                    }

                    // write the object
                    WriteRootBasicType(writer, itemToWrite, itemType, basicTypeInfo, encoding);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// String - Format: (Embedded-Nullable-0-0-0-0-0-0) [if not embedded?0-0-0-0-0-0-0-0]
        /// Embeddable range: 0..63
        /// </summary>
        internal static void WriteValue(BinaryWriter writer, string str, Encoding encoding)
        {
            if (str == null)
            {
                WriteNullValue(writer);
            }
            else if (str.Length == 0)
            {
                NumericSerializers.WriteUIntNullableMemberCount(writer, 0u);
            }
            else
            {
                byte[] strBytes;
                if (str.Length > 64)
                {
                    strBytes = GetStringBytes(ref str, encoding);
                }
                else
                {
                    strBytes = GetStringBytes(str, encoding);
                }

                // Int32
                NumericSerializers.WriteUIntNullableMemberCount(writer, (uint)strBytes.Length);
                writer.Write(strBytes);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// byte[] - Format: (Array Length:Embedded-Nullable-0-0-0-0-0-0) [if array length not embedded?0-0-0-0-0-0-0-0] (data:0-0-0-0-0-0-0-0)
        /// Embeddable Array Length range: 0..63
        /// </summary>
        internal static void WriteValue(BinaryWriter writer, byte[] bytes)
        {
            if (bytes == null)
            {
                WriteNullValue(writer);
                return;
            }

            // uint doesn't deal with negative numbers
            NumericSerializers.WriteUIntNullableMemberCount(writer, (uint)bytes.Length);
            writer.Write(bytes);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Obsolete - only backward compatibility
        /// </summary>
        internal static void WriteValue(BinaryWriter writer, DataSet ds, Encoding encoding)
        {
            if (ds == null)
            {
                WriteNullValue(writer);
                return;
            }
            // tables count
            NumericSerializers.WriteUIntNullableMemberCount(writer, (uint)ds.Tables.Count);

            WriteValue(writer, ds.DataSetName, encoding);

            foreach (DataTable dt in ds.Tables)
            {
                WriteValue(writer, dt, encoding);
            }
        }
Exemplo n.º 5
0
        internal static void WriteRootBasicTypedArray(BinaryWriter writer, Array array, BoisBasicTypeInfo typeInfo, Encoding encoding)
        {
            if (array == null)
            {
                PrimitiveWriter.WriteNullValue(writer);
                return;
            }

            var arrayItemType     = typeInfo.BareType;
            var arrayItemTypeType = BoisTypeCache.GetBasicType(typeInfo.BareType);

            // Int32
            NumericSerializers.WriteUIntNullableMemberCount(writer, (uint)array.Length);

            for (int i = 0; i < array.Length; i++)
            {
                WriteRootBasicType(writer, array.GetValue(i), arrayItemType, arrayItemTypeType, encoding);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Guid? - Format: (Embedded-Nullable-0-0-0-0-0-0) [if not embedded?0-0-0-0-0-0-0-0]
        /// Embeddable range: 0..63
        /// </summary>
        internal static void WriteValue(BinaryWriter writer, Guid?g)
        {
            if (g == null)
            {
                WriteNullValue(writer);
                return;
            }

            var guid = g.Value;

            if (guid == Guid.Empty)
            {
                // Int32
                NumericSerializers.WriteUIntNullableMemberCount(writer, 0u);
                return;
            }

            var data = guid.ToByteArray();

            // Int32
            NumericSerializers.WriteUIntNullableMemberCount(writer, (uint)data.Length);
            writer.Write(data);
        }