Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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);
        }