Example #1
0
 public BSONValue(BSONType type, string key)
     : this(type, key, null)
 {
 }
Example #2
0
        private JsonContainer ParseBinary(BinaryReader jsonBinary, ParseState parseMode = ParseState.Object)
        {
            try
            {
                Queue <object> tokenStack      = new Queue <object> ();
                bool           isParsing       = true;
                int            dataSize        = jsonBinary.ReadInt32();
                int            currentPosition = ( int )jsonBinary.BaseStream.Position;
                while (isParsing && (jsonBinary.BaseStream.Position - currentPosition) != dataSize)
                {
                    BSONType rb = ( BSONType )jsonBinary.ReadByte();
                    if (rb == BSONType.EndDoc)
                    {
                        break;
                    }

                    if (parseMode == ParseState.Object)
                    {
                        tokenStack.Enqueue(GetKeyFromBinary(jsonBinary));
                    }
                    else
                    {
                        jsonBinary.ReadByte();
                    }

                    switch (rb)
                    {
                    case BSONType.EndDoc: isParsing = false; break;

                    case BSONType.Double: tokenStack.Enqueue(jsonBinary.ReadDouble()); break;

                    case BSONType.String: tokenStack.Enqueue(GetStringFromBinary(jsonBinary)); break;

                    case BSONType.Document: tokenStack.Enqueue(ParseBinary(jsonBinary, ParseState.Object)); break;

                    case BSONType.Array: tokenStack.Enqueue(ParseBinary(jsonBinary, ParseState.Array)); break;

                    case BSONType.BinaryData: tokenStack.Enqueue(GetBinaryFromBinary(jsonBinary)); break;

                    case BSONType.Boolean: tokenStack.Enqueue(jsonBinary.ReadByte() == 0 ? false : true); break;

                    case BSONType.UTCTime: tokenStack.Enqueue(DateTime.FromFileTimeUtc(jsonBinary.ReadInt64())); break;

                    case BSONType.Null: tokenStack.Enqueue(null); break;

                    case BSONType.Regexp: tokenStack.Enqueue(new Regex(GetStringFromBinary(jsonBinary))); break;

                    case BSONType.JavascriptCode: tokenStack.Enqueue(GetStringFromBinary(jsonBinary)); break;

                    case BSONType.Integer: tokenStack.Enqueue(jsonBinary.ReadInt32()); break;

                    case BSONType.Integer64: tokenStack.Enqueue(jsonBinary.ReadInt64()); break;

                    default: throw new Exception("There is unsupport Data type.");
                    }
                }

                return(Build(parseMode, tokenStack));
            }
            catch { throw new ArgumentException("Invalid JSON document."); }
        }
Example #3
0
 public BSONValue(BSONType type, string key, object value)
 {
     this.BSONType = type;
     this.Key = key;
     this.Value = value;
 }
Example #4
0
 public BSONValue(BSONType type, string key) : this(type, key, null)
 {
 }
Example #5
0
 public BSONValue(BSONType type, string key, object value)
 {
     this.BSONType = type;
     this.Key      = key;
     this.Value    = value;
 }
Example #6
0
 public override int GetHashCode()
 {
     unchecked {
         return(BSONType.GetHashCode() ^ (Value != null ? Value.GetHashCode() : 0));
     }
 }
        protected void WriteBSONValue(BSONValue bv, ExtBinaryWriter bw)
        {
            BSONType bt = bv.BSONType;

            switch (bt)
            {
            case BSONType.EOO:
                break;

            case BSONType.NULL:
            case BSONType.UNDEFINED:
            case BSONType.MAXKEY:
            case BSONType.MINKEY:
                WriteTypeAndKey(bv, bw);
                break;

            case BSONType.OID:
            {
                WriteTypeAndKey(bv, bw);
                BSONOid oid = (BSONOid)bv.Value;
                Debug.Assert(oid._bytes.Length == 12);
                bw.Write(oid._bytes);
                break;
            }

            case BSONType.STRING:
            case BSONType.CODE:
            case BSONType.SYMBOL:
                WriteTypeAndKey(bv, bw);
                bw.WriteBSONString((string)bv.Value);
                break;

            case BSONType.BOOL:
                WriteTypeAndKey(bv, bw);
                bw.Write((bool)bv.Value);
                break;

            case BSONType.INT:
                WriteTypeAndKey(bv, bw);
                bw.Write((int)bv.Value);
                break;

            case BSONType.LONG:
                WriteTypeAndKey(bv, bw);
                bw.Write((long)bv.Value);
                break;

            case BSONType.ARRAY:
            case BSONType.OBJECT:
            {
                BSONDocument doc = (BSONDocument)bv.Value;
                WriteTypeAndKey(bv, bw);
                doc.Serialize(bw.BaseStream);
                break;
            }

            case BSONType.DATE:
            {
                DateTime dt   = (DateTime)bv.Value;
                var      diff = dt.ToLocalTime() - BSONConstants.Epoch;
                long     time = (long)Math.Floor(diff.TotalMilliseconds);
                WriteTypeAndKey(bv, bw);
                bw.Write(time);
                break;
            }

            case BSONType.DOUBLE:
                WriteTypeAndKey(bv, bw);
                bw.Write((double)bv.Value);
                break;

            case BSONType.REGEX:
            {
                BSONRegexp rv = (BSONRegexp)bv.Value;
                WriteTypeAndKey(bv, bw);
                bw.WriteCString(rv.Re ?? "");
                bw.WriteCString(rv.Opts ?? "");
                break;
            }

            case BSONType.BINDATA:
            {
                BSONBinData bdata = (BSONBinData)bv.Value;
                WriteTypeAndKey(bv, bw);
                bw.Write(bdata.Data.Length);
                bw.Write(bdata.Subtype);
                bw.Write(bdata.Data);
                break;
            }

            case BSONType.DBREF:
                //Unsupported DBREF!
                break;

            case BSONType.TIMESTAMP:
            {
                BSONTimestamp ts = (BSONTimestamp)bv.Value;
                WriteTypeAndKey(bv, bw);
                bw.Write(ts.Inc);
                bw.Write(ts.Ts);
                break;
            }

            case BSONType.CODEWSCOPE:
            {
                BSONCodeWScope cw = (BSONCodeWScope)bv.Value;
                WriteTypeAndKey(bv, bw);
                using (var cwwr = new ExtBinaryWriter(new MemoryStream())) {
                    cwwr.WriteBSONString(cw.Code);
                    cw.Scope.Serialize(cwwr.BaseStream);
                    byte[] cwdata = ((MemoryStream)cwwr.BaseStream).ToArray();
                    bw.Write(cwdata.Length);
                    bw.Write(cwdata);
                }
                break;
            }

            default:
                throw new InvalidBSONDataException("Unknown entry type: " + bt);
            }
        }
        static void __BsonParseBsonArray(List <object> arr, BinaryReader reader)
        {
            try
            {
                bool isParsing       = true;
                int  dataSize        = reader.ReadInt32();
                int  currentPosition = (int)reader.BaseStream.Position;
                while (isParsing && (reader.BaseStream.Position - currentPosition) != dataSize)
                {
                    BSONType rb = (BSONType)reader.ReadByte();
                    if (rb == BSONType.EndDoc)
                    {
                        break;
                    }

                    reader.ReadByte();

                    switch (rb)
                    {
                    case BSONType.EndDoc: isParsing = false; break;

                    case BSONType.Double: arr.Add(reader.ReadDouble()); break;

                    case BSONType.String: arr.Add(__BsonGetStringFromBinary(reader)); break;

                    case BSONType.Document:
                    {
                        BlockarObject inner = new BlockarObject();
                        __BsonParseBsonObject(inner, reader);
                        arr.Add(inner);
                    }
                    break;

                    case BSONType.Array:
                    {
                        List <object> innerArr = new List <object> ();
                        __BsonParseBsonArray(innerArr, reader);
                        arr.Add(innerArr.ToArray());
                    }
                    break;

                    case BSONType.BinaryData: arr.Add(__BsonGetBinaryFromBinary(reader)); break;

                    case BSONType.Boolean: arr.Add(reader.ReadByte() != 0); break;

                    case BSONType.UTCTime: arr.Add(DateTime.FromFileTimeUtc(reader.ReadInt64())); break;

                    case BSONType.Null: arr.Add(null); break;

                    case BSONType.Regexp: arr.Add(new Regex(__BsonGetStringFromBinary(reader))); break;

                    case BSONType.JavascriptCode: arr.Add(__BsonGetStringFromBinary(reader)); break;

                    case BSONType.Integer: arr.Add(reader.ReadInt32()); break;

                    case BSONType.Integer64: arr.Add(reader.ReadInt64()); break;

                    default: throw new Exception("There is unsupport Data type.");
                    }
                }
            }
            catch { throw new ArgumentException("Invalid JSON document."); }
        }