Esempio n. 1
0
        protected override IEnumerator <DeserializeRequest> Deserialize(EasyReader input)
        {
            input.ReadInt32(out _count);
            input.ReadBoolean(out _weighted);
            if (_weighted)
            {
                // Read constant weights
                if (input.ReadBoolean())
                {
                    input.ReadDouble(out _constantWeightSum);
                    _weights = new double[_count];
                    for (int i = 0; i < _count; i++)
                    {
                        input.ReadDouble(out _weights[i]);
                    }
                }

                // Read dynamic weights
                int numDW = input.ReadInt32();
                if (numDW > 0)
                {
                    _dynamicWeights = new List <_ <int, RST> >(numDW);
                    for (int i = 0; i < numDW; i++)
                    {
                        int index   = input.ReadInt32();
                        var request = new DeserializeRequest();
                        yield return(request);

                        _dynamicWeights.Add(new _ <int, RST>(index, request.Result));
                    }
                }
            }

            // Read elements
            _elements = new List <RST>(_count);
            for (int i = 0; i < _count; i++)
            {
                var request = new DeserializeRequest();
                yield return(request);

                _elements.Add(request.Result);
            }
        }
Esempio n. 2
0
 private static BsonItem ReadItem(byte code, BsonDocument document, EasyReader reader)
 {
     object val = null;
     switch (code)
     {
         case 0x01: // double
             val = reader.ReadDouble();
             break;
         case 0x02: // string
             val = reader.ReadString(Encoding.UTF8).TrimEnd('\x00');
             if (document.StringTableMode == BsonStringTableMode.KeysAndValues)
                 val = document.ReverseStringTable[int.Parse((string)val)];
             break;
         case 0x03: // document
             val = Read(reader, document).Top;
             break;
         case 0x04: // array
             val = Read(reader, document, true).Top;
             break;
         case 0x05: // binary
             var length = reader.ReadInt32();
             var subtype = reader.ReadByte();
             if (subtype != 0x00)
                 throw new NotSupportedException("BSON subtypes other than 'generic binary data' are not supported.");
             val = reader.ReadBytes(length);
             break;
         case 0x06: // undefined
             break;
         case 0x07: // ObjectId
             // why does this parser support ObjectIds and not other binary data?
             // shhhhh
             val = Encoding.ASCII.GetString(reader.ReadBytes(12));
             break;
         case 0x08: // boolean
             val = reader.ReadBoolean();
             break;
         case 0x09: // UTC datetime
             val = reader.ReadInt64();
             break;
         case 0x0A: // null
             break;
         case 0x0B: // regex
             // why are you using regex in a Rant package?
             throw new NotSupportedException("Regular expressions are not supported.");
         case 0x0C: // db pointer
             throw new NotSupportedException("DB pointers are not supported.");
         case 0x0D: // Javascript code
         case 0x0F: // JS code with scope
             throw new NotSupportedException("Javascript in BSON is not supported.");
         case 0x0E: // depreceated
             val = reader.ReadString(Encoding.UTF8);
             break;
         case 0x10: // 32 bit integer
             val = reader.ReadInt32();
             break;
         case 0x11: // timestamp
         case 0x12: // 64 bit integer
             val = reader.ReadInt64();
             break;
         case 0xFF: // min key
         case 0x7F: // max key
             // we don't care about these so let's just skip em
             break;
     }
     if (!(val is BsonItem))
         return new BsonItem(val) { Type = code };
     var i = (BsonItem)val;
     i.Type = code;
     return i;
 }
Esempio n. 3
0
        private static BsonItem ReadItem(byte code, BsonDocument document, EasyReader reader)
        {
            object val = null;

            switch (code)
            {
            case 0x01:     // double
                val = reader.ReadDouble();
                break;

            case 0x02:     // string
                val = reader.ReadString(Encoding.UTF8).TrimEnd('\x00');
                if (document.StringTableMode == BsonStringTableMode.KeysAndValues)
                {
                    val = document.ReverseStringTable[int.Parse((string)val)];
                }
                break;

            case 0x03:     // document
                val = Read(reader, document).Top;
                break;

            case 0x04:     // array
                val = Read(reader, document, true).Top;
                break;

            case 0x05:     // binary
                var length  = reader.ReadInt32();
                var subtype = reader.ReadByte();
                if (subtype != 0x00)
                {
                    throw new NotSupportedException("BSON subtypes other than 'generic binary data' are not supported.");
                }
                val = reader.ReadBytes(length);
                break;

            case 0x06:     // undefined
                break;

            case 0x07:     // ObjectId
                // why does this parser support ObjectIds and not other binary data?
                // shhhhh
                val = Encoding.ASCII.GetString(reader.ReadBytes(12));
                break;

            case 0x08:     // boolean
                val = reader.ReadBoolean();
                break;

            case 0x09:     // UTC datetime
                val = reader.ReadInt64();
                break;

            case 0x0A:     // null
                break;

            case 0x0B:     // regex
                // why are you using regex in a Rant package?
                throw new NotSupportedException("Regular expressions are not supported.");

            case 0x0C:     // db pointer
                throw new NotSupportedException("DB pointers are not supported.");

            case 0x0D:     // Javascript code
            case 0x0F:     // JS code with scope
                throw new NotSupportedException("Javascript in BSON is not supported.");

            case 0x0E:     // depreceated
                val = reader.ReadString(Encoding.UTF8);
                break;

            case 0x10:     // 32 bit integer
                val = reader.ReadInt32();
                break;

            case 0x11:     // timestamp
            case 0x12:     // 64 bit integer
                val = reader.ReadInt64();
                break;

            case 0xFF:     // min key
            case 0x7F:     // max key
                // we don't care about these so let's just skip em
                break;
            }
            if (!(val is BsonItem))
            {
                return new BsonItem(val)
                       {
                           Type = code
                       }
            }
            ;
            var i = (BsonItem)val;

            i.Type = code;
            return(i);
        }
    }