Exemple #1
0
        internal static void WriteIEnumerable(this IEnumerable values, BinaryWriter writer)
        {
            bool         first    = true;
            var          position = 0L;
            var          count    = 0;
            BSonTypeEnum type     = BSonTypeEnum.BSON_Null;

            foreach (var item in values)
            {
                if (first)
                {
                    first = false;
                    type  = item.GetBSonType();
                    writer.Write((byte)type);                            // scrivo il tipo dell'item nell'array
                    position = writer.BaseStream.Position;               // salvo la posizione per scrivere la dimenzione dell'array
                    writer.Write((int)0);                                // scrivo 0 perchè non so quato è grande l'array
                    BSonSerializer.WriteCorrectType(item, writer, type); // scrivo l'item
                }
                else
                {
                    BSonSerializer.WriteCorrectType(item, writer, type); // scrivo l'item
                }
                count++;
            }

            writer.BaseStream.Position = position;
            writer.Write(count);
            writer.BaseStream.Seek(0, SeekOrigin.End);
        }
Exemple #2
0
        internal static object ReadCorrectType(this BSonTypeEnum type, BinaryReader reader)
        {
            switch (type)
            {
            case BSonTypeEnum.BSON_Null: return(null);

            case BSonTypeEnum.BSON_Bool: return(reader.ReadBoolean());

            case BSonTypeEnum.BSON_Byte: return(reader.ReadByte());

            case BSonTypeEnum.BSON_int16: return(reader.ReadInt16());

            case BSonTypeEnum.BSON_int32: return(reader.ReadInt32());

            case BSonTypeEnum.BSON_int64: return(reader.ReadInt64());

            case BSonTypeEnum.BSON_single: return(reader.ReadSingle());

            case BSonTypeEnum.BSON_double: return(reader.ReadDouble());

#if WINDOWS_PHONE
            case BSonTypeEnum.BSON_decimal: return((decimal)reader.ReadDouble());
#else
            case BSonTypeEnum.BSON_decimal: return(reader.ReadDecimal());
#endif
            case BSonTypeEnum.BSON_GUID: return(new Guid(reader.ReadBytes(16)));

            case BSonTypeEnum.BSON_bynary:
                var alen = reader.ReadInt32(); return(reader.ReadBytes(alen));

            case BSonTypeEnum.BSON_chararray:
                var clen = reader.ReadInt32(); return(reader.ReadChars(clen));

            case BSonTypeEnum.BSON_DateTime: return(new DateTime(reader.ReadInt64()));

            case BSonTypeEnum.BSON_string: return(reader.ReadString());

            case BSonTypeEnum.BSON_ObjectId:
                break;

            case BSonTypeEnum.BSON_timestamp:
                break;

            case BSonTypeEnum.BSON_Document:
                break;

            case BSonTypeEnum.BSON_DocumentArray:
                break;

            case BSonTypeEnum.BSON_Dictionary:
                break;

            case BSonTypeEnum.BSON_objectType: return(Type.GetType(reader.ReadString()));

            default:
                break;
            }
            return(null);
        }
Exemple #3
0
        private static BSonProp DeserializeProperty(BinaryReader reader, BSonTypeEnum type)
        {
            string name = reader.ReadString();

            return(new BSonProp()
            {
                Name = name, Value = ReadCorrectType(type, reader)
            });
        }
Exemple #4
0
        private static object NavigateStream(this BinaryReader reader)
        {
            byte         check = reader.ReadByte();
            BSonTypeEnum type  = (BSonTypeEnum)check;

            switch (type)
            {
            case BSonTypeEnum.BSON_Document:
                return(DeserializeDocument(reader, BSonDocumentType.BSON_Document));

            case BSonTypeEnum.BSON_DocumentArray:
                return(DeserializeDocument(reader, BSonDocumentType.BSON_DocumentArray));

            case BSonTypeEnum.BSON_Dictionary:
                return(DeserializeDocument(reader, BSonDocumentType.BSON_Dictionary));

            case BSonTypeEnum.BSON_Null:
            case BSonTypeEnum.BSON_Bool:
            case BSonTypeEnum.BSON_Byte:
            case BSonTypeEnum.BSON_int16:
            case BSonTypeEnum.BSON_int32:
            case BSonTypeEnum.BSON_int64:
            case BSonTypeEnum.BSON_single:
            case BSonTypeEnum.BSON_double:
            case BSonTypeEnum.BSON_decimal:
            case BSonTypeEnum.BSON_GUID:
            case BSonTypeEnum.BSON_bynary:
            case BSonTypeEnum.BSON_chararray:
            case BSonTypeEnum.BSON_DateTime:
            case BSonTypeEnum.BSON_string:
            case BSonTypeEnum.BSON_ObjectId:
            case BSonTypeEnum.BSON_timestamp:
            case BSonTypeEnum.BSON_objectType:
            default:
                return(DeserializeProperty(reader, type));
            }
        }
Exemple #5
0
        internal static void WriteCorrectType(this object value, BinaryWriter writer, BSonTypeEnum type)
        {
            switch (type)
            {
            case BSonTypeEnum.BSON_Null: return;

            case BSonTypeEnum.BSON_Bool:
                writer.Write((bool)value); break;

            case BSonTypeEnum.BSON_Byte:
                writer.Write((byte)value); break;

            case BSonTypeEnum.BSON_int16:
                writer.Write((short)value); break;

            case BSonTypeEnum.BSON_int32:
                writer.Write((int)value); break;

            case BSonTypeEnum.BSON_int64:
                writer.Write((long)value); break;

            case BSonTypeEnum.BSON_single:
                writer.Write((Single)value); break;

            case BSonTypeEnum.BSON_double:
                writer.Write((double)value); break;

            case BSonTypeEnum.BSON_decimal:
#if WINDOWS_PHONE
                writer.Write((double)value); break;
#else
                writer.Write((decimal)value); break;
#endif
            case BSonTypeEnum.BSON_GUID:
                writer.Write(((Guid)value).ToByteArray()); break;

            case BSonTypeEnum.BSON_bynary:
                var barr = (byte[])value; writer.Write((int)barr.Length); writer.Write(barr); break;

            case BSonTypeEnum.BSON_chararray:
                var carr = (char[])value; writer.Write((int)carr.Length); writer.Write(carr); break;

            case BSonTypeEnum.BSON_DateTime:
                writer.Write(((DateTime)value).Ticks); break; // Writes the Date in GMT/UTC format pattern

            case BSonTypeEnum.BSON_string:
                writer.Write(value.ToString()); break;

            case BSonTypeEnum.BSON_ObjectId:
                break;

            case BSonTypeEnum.BSON_timestamp:
                break;

            case BSonTypeEnum.BSON_Document:
            case BSonTypeEnum.BSON_DocumentArray:
            case BSonTypeEnum.BSON_Dictionary:
                writer.Write(((BSonDoc)value).Serialize());
                break;

            case BSonTypeEnum.BSON_objectType:
                writer.Write(((Type)value).AssemblyQualifiedName);
                break;

            default:
                break;
            }
        }