Beispiel #1
0
 public BinaryMessageEncoderFactory(BsonBinaryWriter binaryWriter)
     : this(null, Ensure.IsNotNull(binaryWriter, "binaryWriter"))
 {
 }
Beispiel #2
0
 public BinaryMessageEncoderFactory(BsonBinaryReader binaryReader, BsonBinaryWriter binaryWriter)
 {
     Ensure.That(binaryReader != null || binaryWriter != null, "bsonReader and bsonWriter cannot both be null.");
     _binaryReader = binaryReader;
     _binaryWriter = binaryWriter;
 }
        /***************************************************/
        /**** Test Functions                            ****/
        /***************************************************/

        public static void TestBson()
        {
            List <BHoMObject> nodes = new List <BHoMObject>
            {
                new Node {
                    Point = new Point(1, 2, 3), Name = "A"
                },
                new Node {
                    Point = new Point(4, 5, 6), Name = "B"
                },
                new Node {
                    Point = new Point(7, 8, 9), Name = "C"
                }
            };


            List <object> items = new List <object>
            {
                new A(-6, -7)
                {
                    a = 1, publicField = -4
                },
                new B {
                    a = 2, b = 45
                },
                new C {
                    a = 3, c = 56
                },
                new D {
                    a = 4, b = 67, d = 123
                },
                new E {
                    a = 5, c = 78, e = 456
                },
                new Node {
                    Point = new Point(1, 2, 3), Name = "A"
                },
                nodes,
                new Dictionary <string, A> {
                    { "A", new A {
                          a = 1
                      } },
                    { "C", new C {
                          a = 3, c = 56
                      } },
                    { "E", new E {
                          a = 5, c = 78, e = 456
                      } }
                }
            };

            List <BsonDocument> docs   = items.Select(x => x.ToBsonDocument()).ToList();
            List <object>       items2 = docs.Select(x => BsonSerializer.Deserialize(x, typeof(object))).ToList();

            foreach (BsonDocument doc in docs)
            {
                Console.WriteLine(doc.ToJson());
                Console.WriteLine();
            }

            string outputFileRoot = @"C:\Users\adecler\Documents\"; // initialize to the file to write to.

            File.WriteAllLines(@"C:\Users\adecler\Documents\json_Save.txt", docs.Select(x => x.ToJson()));

            FileStream mongoStream = new FileStream(outputFileRoot + "bsonSave_Mongo.txt", FileMode.Create);
            var        writer      = new BsonBinaryWriter(mongoStream);

            BsonSerializer.Serialize(writer, typeof(object), docs);
            mongoStream.Flush();
            mongoStream.Close();

            FileStream      csharpStream = new FileStream(outputFileRoot + "bsonSave_CSharp.txt", FileMode.Create);
            BinaryFormatter formatter    = new BinaryFormatter();

            formatter.Serialize(csharpStream, docs);
            csharpStream.Close();

            FileStream          mongoReadStream = File.OpenRead(outputFileRoot + "bsonSave_Mongo.txt");
            var                 reader          = new BsonBinaryReader(mongoReadStream);
            List <BsonDocument> readBson        = BsonSerializer.Deserialize(reader, typeof(object)) as List <BsonDocument>;
            List <object>       items3          = readBson.Select(x => BsonSerializer.Deserialize(x, typeof(object))).ToList();

            // Directly writing and reading objects to the stream using Bson serializer seems to have a problem when reading back
            //FileStream objectStream = new FileStream(outputFileRoot + "objectSave_Mongo.txt", FileMode.Create);
            //var objectWriter = new BsonBinaryWriter(objectStream);
            //BsonSerializer.Serialize(objectWriter, typeof(List<object>), items);
            //objectStream.Flush();
            //objectStream.Close();

            //FileStream objectReadStream = File.OpenRead(outputFileRoot + "objectSave_Mongo.txt");
            //var objectReader = new BsonBinaryReader(objectReadStream);
            //var readObject = BsonSerializer.Deserialize(reader, typeof(object));


            Console.WriteLine("Done!");
        }
        private void WriteQuery(BsonBinaryWriter binaryWriter, BsonDocument query)
        {
            var context = BsonSerializationContext.CreateRoot <BsonDocument>(binaryWriter);

            BsonDocumentSerializer.Instance.Serialize(context, query);
        }
            // methods
            private void SerializeFilter(BsonBinaryWriter bsonWriter, BsonDocument filter)
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);

                BsonDocumentSerializer.Instance.Serialize(context, filter);
            }
Beispiel #6
0
 // constructors
 public DeleteMessageBinaryEncoder(BsonBinaryReader binaryReader, BsonBinaryWriter binaryWriter)
 {
     Ensure.That(binaryReader != null || binaryWriter != null, "binaryReader and binaryWriter cannot both be null.");
     _binaryReader = binaryReader;
     _binaryWriter = binaryWriter;
 }
 // protected methods
 protected abstract void SerializeRequest(BsonBinaryWriter bsonBinaryWriter, WriteRequest request);
            // methods
            private void SerializeCriteria(BsonBinaryWriter bsonWriter, BsonDocument criteria)
            {
                var context = BsonSerializationContext.CreateRoot <BsonDocument>(bsonWriter);

                BsonDocumentSerializer.Instance.Serialize(context, criteria);
            }
Beispiel #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BsonWriter"/> class.
 /// </summary>
 /// <param name="writer">The writer.</param>
 public BsonWriter(BinaryWriter writer)
 {
     ValidationUtils.ArgumentNotNull(writer, "writer");
     _writer = new BsonBinaryWriter(writer);
 }
Beispiel #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BsonWriter"/> class.
 /// </summary>
 /// <param name="stream">The stream.</param>
 public BsonWriter(Stream stream)
 {
     ValidationUtils.ArgumentNotNull(stream, "stream");
     _writer = new BsonBinaryWriter(new BinaryWriter(stream));
 }
Beispiel #11
0
        static void Main(string[] args)
        {
            FileInfo assemblyFileInfo = new FileInfo(args[0]);
            string   savePath         = args[1];

            DirectoryInfo fireMLDirInfo  = assemblyFileInfo.Directory;
            DirectoryInfo contentDirInfo = fireMLDirInfo.Parent;

            List <string> plotFileList  = new List <string>();
            List <string> assetFileList = new List <string>();

            foreach (FileInfo fileInfo in fireMLDirInfo.GetFiles("*.*", SearchOption.AllDirectories))
            {
                string ext = fileInfo.Extension;
                if (ext == ".fmlplot")
                {
                    plotFileList.Add(fileInfo.FullName);
                }
                else if (ext == ".fmlasset")
                {
                    assetFileList.Add(fileInfo.FullName);
                }
            }

            string xsdDirPath = fireMLDirInfo.FullName + "\\" + "XSD";

            //FireEngine.XNAContent.ContentManager contentManager = new FireEngine.XNAContent.ContentManager(contentDirInfo.FullName);
            CompilerKernel kernel = new CompilerKernel(plotFileList.ToArray(), assetFileList.ToArray(), xsdDirPath, null /*contentManager*/);
            FireMLRoot     result = kernel.CompileFireML();

            Error[] errors = kernel.CheckPoint();

            foreach (Error e in errors)
            {
                Console.WriteLine("{0}\n{1},{2}\n{3}", e.Location.FileName, e.Location.Line, e.Location.Column, e.Message);
                Console.WriteLine();
            }

            if (errors.Length > 0)
            {
                Environment.Exit(-1);
                return;
            }

            Stream     bsonStream = new FileStream(savePath, FileMode.Create);
            BsonBuffer bsonBuffer = new BsonBuffer();
            BsonBinaryWriterSettings bsonSettings = new BsonBinaryWriterSettings();
            BsonBinaryWriter         bsonWriter   = new BsonBinaryWriter(bsonStream, bsonBuffer, bsonSettings);

            BsonSerializer.Serialize <FireMLRoot>(bsonWriter, result);
            bsonWriter.Close();

            JsonWriterSettings jsonSettings = new JsonWriterSettings();

            jsonSettings.NewLineChars = "\r\n";
            jsonSettings.OutputMode   = JsonOutputMode.JavaScript;
            jsonSettings.Indent       = true;
            jsonSettings.IndentChars  = "  ";
            StreamWriter streamWriter = new StreamWriter(new FileStream(savePath + ".json", FileMode.Create));
            JsonWriter   jsonWriter   = new JsonWriter(streamWriter, jsonSettings);

            BsonSerializer.Serialize <FireMLRoot>(jsonWriter, result);
            jsonWriter.Close();
        }