MongoDB.Bson.Serialization.Serializers.BsonDocumentSerializer.Serialize C# (CSharp) Метод

Serialize() публичный метод

Serializes an object to a BsonWriter.
public Serialize ( BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options ) : void
bsonWriter BsonWriter The BsonWriter.
nominalType System.Type The nominal type.
value object The object.
options IBsonSerializationOptions The serialization options.
Результат void
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // could get here with a BsonDocumentWrapper from BsonValueSerializer switch statement
            var wrapper = value as BsonDocumentWrapper;
            if (wrapper != null)
            {
                BsonDocumentWrapperSerializer.Instance.Serialize(bsonWriter, nominalType, value, null);
                return;
            }

            var rawBsonDocument = value as RawBsonDocument;
            if (rawBsonDocument != null)
            {
                RawBsonDocumentSerializer.Instance.Serialize(bsonWriter, nominalType, value, options);
                return;
            }

            var bsonDocument = (BsonDocument)value;
            var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
            if (documentSerializationOptions == null)
            {
                var message = string.Format(
                    "Serialize method of BsonDocument expected serialization options of type {0}, not {1}.",
                    BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
                    BsonUtils.GetFriendlyTypeName(options.GetType()));
                throw new BsonSerializationException(message);
            }

            bsonWriter.WriteStartDocument();
            BsonElement idElement = null;
            if (documentSerializationOptions.SerializeIdFirst && bsonDocument.TryGetElement("_id", out idElement))
            {
                bsonWriter.WriteName(idElement.Name);
                BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), idElement.Value, null);
            }

            foreach (var element in bsonDocument)
            {
                // if serializeIdFirst is false then idElement will be null and no elements will be skipped
                if (!object.ReferenceEquals(element, idElement))
                {
                    bsonWriter.WriteName(element.Name);
                    BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), element.Value, null);
                }
            }

            bsonWriter.WriteEndDocument();
        }

Usage Example

Пример #1
0
        internal void Save(string saveAs, FileFormat format)
        {
            if (string.IsNullOrEmpty(saveAs))
            {
                saveAs = FilePath;
                format = FileFormat;
            }

            if (saveAs == null)
                throw new InvalidOperationException("File path should be provided either on opening or saving.");

            if (format == FileFormat.Auto)
                format = saveAs.EndsWith(".json", StringComparison.OrdinalIgnoreCase) ? FileFormat.Json : FileFormat.Bson;

            var tmp = File.Exists(saveAs) ? saveAs + ".tmp" : saveAs;

            var serializer = new BsonDocumentSerializer();
            var options = DocumentSerializationOptions.Defaults;

            if (format == FileFormat.Json)
            {
                using (var streamWriter = new StreamWriter(tmp))
                {
                    foreach (var document in Documents)
                    {
                        using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture))
                        using (var bsonWriter = BsonWriter.Create(stringWriter, Actor.DefaultJsonWriterSettings))
                        {
                            serializer.Serialize(bsonWriter, typeof(BsonDocument), document, options);
                            streamWriter.WriteLine(stringWriter.ToString());
                        }
                    }
                }
            }
            else
            {
                using (var fileStream = File.Open(tmp, FileMode.Create, FileAccess.Write, FileShare.None))
                using (var bsonWriter = BsonWriter.Create(fileStream))
                {
                    foreach (var document in Documents)
                        serializer.Serialize(bsonWriter, typeof(BsonDocument), document, options);
                }
            }

            if (!object.ReferenceEquals(tmp, saveAs))
                File.Replace(tmp, saveAs, null);
        }