Esempio n. 1
0
        private void EncodeArray(MemoryStream ms, BArray lst)
        {
            var obj = new BObject();

            for (int i = 0; i < lst.Count; ++i)
            {
                obj.Add(Convert.ToString(i), lst[i]);
            }

            EncodeDocument(ms, obj);
        }
Esempio n. 2
0
        public static byte[] Serialize(BObject obj)
        {
            var bson = new Bson();
            var ms   = new MemoryStream();

            bson.EncodeDocument(ms, obj);

            byte[] buf = new byte[ms.Position];
            ms.Seek(0, SeekOrigin.Begin);
            ms.Read(buf, 0, buf.Length);

            return(buf);
        }
Esempio n. 3
0
        private void EncodeDocument(MemoryStream ms, BObject obj)
        {
            var dms = new MemoryStream();

            foreach (string str in obj.Keys)
            {
                EncodeElement(dms, str, obj[str]);
            }

            var bw = new BinaryWriter(ms);

            bw.Write((Int32)(dms.Position + 4 + 1));
            bw.Write(dms.GetBuffer(), 0, (int)dms.Position);
            bw.Write((byte)0);
        }
Esempio n. 4
0
        private BArray DecodeArray()
        {
            BObject obj = DecodeDocument();

            int    i     = 0;
            BArray array = new BArray();

            while (obj.ContainsKey(Convert.ToString(i)))
            {
                array.Add(obj[Convert.ToString(i)]);

                i += 1;
            }

            return(array);
        }
Esempio n. 5
0
        private BObject DecodeDocument()
        {
            int length = reader.ReadInt32() - 4;

            BObject obj = new BObject();

            int i = (int)reader.BaseStream.Position;

            while (reader.BaseStream.Position < i + length - 1)
            {
                BValue value = DecodeElement(out var name);
                obj.Add(name, value);
            }

            reader.ReadByte(); // zero
            return(obj);
        }