Ejemplo n.º 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 args       = new BsonSerializationArgs();

            args.NominalType = typeof(BsonDocument);

            if (format == FileFormat.Json)
            {
                using (var streamWriter = new StreamWriter(tmp))
                    using (var jsonWriter = new JsonWriter(streamWriter, Actor.DefaultJsonWriterSettings))
                    {
                        var context = BsonSerializationContext.CreateRoot(jsonWriter);
                        foreach (var document in Documents)
                        {
                            serializer.Serialize(context, args, document);
                            streamWriter.WriteLine();
                        }
                    }
            }
            else
            {
                using (var fileStream = File.Open(tmp, FileMode.Create, FileAccess.Write, FileShare.None))
                    using (var bsonWriter = new BsonBinaryWriter(fileStream))
                    {
                        var context = BsonSerializationContext.CreateRoot(bsonWriter);
                        foreach (var document in Documents)
                        {
                            serializer.Serialize(context, args, document);
                        }
                    }
            }

            if (!object.ReferenceEquals(tmp, saveAs))
            {
                File.Replace(tmp, saveAs, null);
            }
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
            public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
            {
                FrameDLRObject         v  = (FrameDLRObject)value;
                BsonDocumentSerializer bs = new BsonDocumentSerializer();
                BsonDocument           bd = new BsonDocument(v.ToDictionary());

                bs.Serialize(context, args, bd);
            }
Ejemplo n.º 4
0
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, NameValueCollection value)
        {
            BsonDocument doc = new BsonDocument();

            if (value != null)
            {
                foreach (var key in value.AllKeys)
                {
                    foreach (var val in value.GetValues(key))
                    {
                        doc.Add(key.Replace('.', '|'), val);
                    }
                }
            }

            documentserializer.Serialize(context, doc);
        }