コード例 #1
0
        internal static JObject FromBson(byte[] buffer, ref int offset, InstanceFactory factory = null)
        {
            JObject result = new JObject();

            int startingOffset = offset;
            int len            = (Int32)SerializationUtilities.Unmarshall(buffer, ref offset, TypeCode.Int32);

            while (offset < startingOffset + len - 1)
            {
                // get the element type
                var bsonType = (BsonTypes)buffer[offset++];
                // get the element name
                var idxNul = JToken.FindNul(buffer, offset);
                if (idxNul == -1)
                {
                    throw new Exception("Missing ename terminator");
                }
                var ename = JToken.ConvertToString(buffer, offset, idxNul - offset);
                offset = idxNul + 1;

                JToken item = null;
                switch (bsonType)
                {
                case BsonTypes.BsonArray:
                    item = JArray.FromBson(buffer, ref offset, factory);
                    break;

                case BsonTypes.BsonDocument:
                    item = JObject.FromBson(buffer, ref offset, factory);
                    break;

                case BsonTypes.BsonNull:
                    item = new JValue();
                    break;

                case BsonTypes.BsonBoolean:
                case BsonTypes.BsonDateTime:
                case BsonTypes.BsonDouble:
                case BsonTypes.BsonInt32:
                case BsonTypes.BsonInt64:
                case BsonTypes.BsonString:
                    item = JValue.FromBson(bsonType, buffer, ref offset);
                    break;
                }
                result.Add(ename, item);
            }

            if (buffer[offset++] != 0)
            {
                throw new Exception("bad format - missing trailing null on bson document");
            }

            return(result);
        }
コード例 #2
0
 public override void ToBson(byte[] buffer, ref int offset)
 {
     if (buffer != null)
     {
         if (this.Value != null)
         {
             SerializationUtilities.Marshall(buffer, ref offset, this.Value);
         }
     }
     else
     {
         offset += this.GetBsonSize();
     }
 }
コード例 #3
0
        public byte[] ToBson()
        {
            var size   = this.GetBsonSize("") + 5;
            var buffer = new byte[size];
            int offset = 4;

            this.ToBson("", buffer, ref offset);

            // Write the trailing nul
            buffer[offset++] = (byte)0;

            // Write the completed size
            int zero = 0;

            SerializationUtilities.Marshall(buffer, ref zero, offset);
            return(buffer);
        }
コード例 #4
0
        public static JToken FromBson(byte[] buffer, InstanceFactory factory = null)
        {
            var offset = 0;
            var len    = (Int32)SerializationUtilities.Unmarshall(buffer, ref offset, TypeCode.Int32);

            JToken dserResult = null;

            while (offset < buffer.Length - 1)
            {
                var bsonType = (BsonTypes)buffer[offset++];

                // eat the empty ename
                var idxNul = JToken.FindNul(buffer, offset);
                if (idxNul == -1)
                {
                    throw new Exception("Missing ename terminator");
                }
                var ename = JToken.ConvertToString(buffer, offset, idxNul - offset);
                offset = idxNul + 1;

                switch (bsonType)
                {
                case BsonTypes.BsonDocument:
                    dserResult = JObject.FromBson(buffer, ref offset, factory);
                    break;

                case BsonTypes.BsonArray:
                    dserResult = JArray.FromBson(buffer, ref offset, factory);
                    break;

                default:
                    throw new Exception("unexpected top-level object type in bson");
                }
            }
            if (buffer[offset++] != 0)
            {
                throw new Exception("bad format - missing trailing null on bson document");
            }
            return(dserResult);
        }
コード例 #5
0
        internal static JArray FromBson(byte[] buffer, ref int offset, InstanceFactory factory = null)
        {
            BsonTypes elementType = (BsonTypes)0;

            int startingOffset = offset;
            int len            = (Int32)SerializationUtilities.Unmarshall(buffer, ref offset, TypeCode.Int32);

            var list = new ArrayList();
            int idx  = 0;

            while (offset < startingOffset + len - 1)
            {
                // get the element type
                var bsonType = (BsonTypes)buffer[offset++];
                if (elementType == (BsonTypes)0)
                {
                    elementType = bsonType;
                }
                if (bsonType != elementType)
                {
                    throw new Exception("all array elements must be of the same type");
                }

                // get the element name
                var idxNul = JToken.FindNul(buffer, offset);
                if (idxNul == -1)
                {
                    throw new Exception("Missing ename terminator");
                }
                var ename   = JToken.ConvertToString(buffer, offset, idxNul - offset);
                var elemIdx = int.Parse(ename);
                if (elemIdx != idx)
                {
                    throw new Exception("sparse arrays are not supported");
                }
                ++idx;

                offset = idxNul + 1;

                JToken item = null;
                switch (bsonType)
                {
                case BsonTypes.BsonArray:
                    item = JArray.FromBson(buffer, ref offset, factory);
                    break;

                case BsonTypes.BsonDocument:
                    item = JObject.FromBson(buffer, ref offset, factory);
                    break;

                case BsonTypes.BsonNull:
                    item = new JValue();
                    break;

                case BsonTypes.BsonBoolean:
                case BsonTypes.BsonDateTime:
                case BsonTypes.BsonDouble:
                case BsonTypes.BsonInt32:
                case BsonTypes.BsonInt64:
                case BsonTypes.BsonString:
                    item = JValue.FromBson(bsonType, buffer, ref offset);
                    break;
                }
                list.Add(item);
            }
            if (buffer[offset++] != 0)
            {
                throw new Exception("bad format - missing trailing null on bson document");
            }
            return(new JArray((JToken[])list.ToArray(typeof(JToken))));
        }