コード例 #1
0
    private void WriteBson(BsonWriter writer, Regex regex)
    {
      // Regular expression - The first cstring is the regex pattern, the second
      // is the regex options string. Options are identified by characters, which 
      // must be stored in alphabetical order. Valid options are 'i' for case 
      // insensitive matching, 'm' for multiline matching, 'x' for verbose mode, 
      // 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode 
      // ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.

      string options = null;

      if (HasFlag(regex.Options, RegexOptions.IgnoreCase))
        options += "i";

      if (HasFlag(regex.Options, RegexOptions.Multiline))
        options += "m";

      if (HasFlag(regex.Options, RegexOptions.Singleline))
        options += "s";

      options += "u";

      if (HasFlag(regex.Options, RegexOptions.ExplicitCapture))
        options += "x";

      writer.WriteRegex(regex.ToString(), options);
    }
コード例 #2
0
    public void WriteValues()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartArray();
      writer.WriteValue(long.MaxValue);
      writer.WriteValue((ulong)long.MaxValue);
      writer.WriteValue(int.MaxValue);
      writer.WriteValue((uint)int.MaxValue);
      writer.WriteValue(byte.MaxValue);
      writer.WriteValue(sbyte.MaxValue);
      writer.WriteValue('a');
      writer.WriteValue(decimal.MaxValue);
      writer.WriteValue(double.MaxValue);
      writer.WriteValue(float.MaxValue);
      writer.WriteValue(true);
      writer.WriteValue(new byte[] { 0, 1, 2, 3, 4 });
      writer.WriteValue(new DateTimeOffset(2000, 12, 29, 12, 30, 0, TimeSpan.Zero));
      writer.WriteValue(new DateTime(2000, 12, 29, 12, 30, 0, DateTimeKind.Utc));
      writer.WriteEnd();

      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
      Assert.AreEqual("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-00-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00", bson);
    }
コード例 #3
0
    public void WriteSingleObject()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartObject();
      writer.WritePropertyName("Blah");
      writer.WriteValue(1);
      writer.WriteEndObject();

      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
      Assert.AreEqual("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00", bson);
    }
コード例 #4
0
    public void SerializeToBson()
    {
      Regex regex = new Regex("abc", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);
      JsonSerializer serializer = new JsonSerializer();
      serializer.Converters.Add(new RegexConverter());

      serializer.Serialize(writer, new RegexTestClass { Regex = regex });

      string expected = "13-00-00-00-0B-52-65-67-65-78-00-61-62-63-00-69-75-00-00";
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());

      Assert.AreEqual(expected, bson);
    }
コード例 #5
0
    public void CloseOutput()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      Assert.IsTrue(ms.CanRead);
      writer.Close();
      Assert.IsFalse(ms.CanRead);

      ms = new MemoryStream();
      writer = new BsonWriter(ms) { CloseOutput = false };

      Assert.IsTrue(ms.CanRead);
      writer.Close();
      Assert.IsTrue(ms.CanRead);
    }
コード例 #6
0
    public void Serialize()
    {
      ObjectIdTestClass c = new ObjectIdTestClass
                              {
                                Id = new BsonObjectId(MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001")),
                                Test = "1234£56"
                              };

      MemoryStream ms = new MemoryStream();
      JsonSerializer serializer = new JsonSerializer();

      // serialize product to BSON
      BsonWriter writer = new BsonWriter(ms);
      serializer.Serialize(writer, c);

      byte[] expected = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000");

      Assert.AreEqual(expected, ms.ToArray());
    }
コード例 #7
0
    public void ConvertEmptyRegexBson()
    {
      Regex regex = new Regex(string.Empty);

      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);
      JsonSerializer serializer = new JsonSerializer();
      serializer.Converters.Add(new RegexConverter());

      serializer.Serialize(writer, new RegexTestClass { Regex = regex });

      ms.Seek(0, SeekOrigin.Begin);
      BsonReader reader = new BsonReader(ms);
      serializer.Converters.Add(new RegexConverter());

      RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);

      Assert.AreEqual("", c.Regex.ToString());
      Assert.AreEqual(RegexOptions.None, c.Regex.Options);
    }
コード例 #8
0
    public void WriteComment()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartArray();
      writer.WriteComment("fail");
    }
コード例 #9
0
    public void LocalDateTimeKindHandling()
    {
      DateTime value = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local);

      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartObject();
      writer.WritePropertyName("DateTime");
      writer.WriteValue(value);
      writer.WriteEndObject();

      byte[] bson = ms.ToArray();

      JObject o;
      BsonReader reader;

      reader = new BsonReader(new MemoryStream(bson), false, DateTimeKind.Local);
      o = (JObject)JToken.ReadFrom(reader);
      Assert.AreEqual(value, (DateTime)o["DateTime"]);
    }
コード例 #10
0
    public void UriGuidTimeSpanTestClassEmptyTest()
    {
      UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass();

      var memoryStream = new MemoryStream();
      var bsonWriter = new BsonWriter(memoryStream);
      JsonSerializer serializer = new JsonSerializer();
      serializer.Serialize(bsonWriter, c1);
      bsonWriter.Flush();
      memoryStream.Position = 0;

      var bsonReader = new BsonReader(memoryStream);

      UriGuidTimeSpanTestClass c2 = serializer.Deserialize<UriGuidTimeSpanTestClass>(bsonReader);
      Assert.AreEqual(c1.Guid, c2.Guid);
      Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
      Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
      Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
      Assert.AreEqual(c1.Uri, c2.Uri);
    }
コード例 #11
0
    public void CanRoundTripStackOverflowData()
    {
      var doc =
          @"{
""AboutMe"": ""<p>I'm the Director for Research and Development for <a href=\""http://www.prophoenix.com\"" rel=\""nofollow\"">ProPhoenix</a>, a public safety software company.  This position allows me to investigate new and existing technologies and incorporate them into our product line, with the end goal being to help public safety agencies to do their jobs more effeciently and safely.</p>\r\n\r\n<p>I'm an advocate for PowerShell, as I believe it encourages administrative best practices and allows developers to provide additional access to their applications, without needing to explicity write code for each administrative feature.  Part of my advocacy for PowerShell includes <a href=\""http://blog.usepowershell.com\"" rel=\""nofollow\"">my blog</a>, appearances on various podcasts, and acting as a Community Director for <a href=\""http://powershellcommunity.org\"" rel=\""nofollow\"">PowerShellCommunity.Org</a></p>\r\n\r\n<p>I’m also a co-host of Mind of Root (a weekly audio podcast about systems administration, tech news, and topics).</p>\r\n"",
""WebsiteUrl"": ""http://blog.usepowershell.com""
}";
      JObject parsed = JObject.Parse(doc);
      var memoryStream = new MemoryStream();
      var bsonWriter = new BsonWriter(memoryStream);
      parsed.WriteTo(bsonWriter);
      bsonWriter.Flush();
      memoryStream.Position = 0;

      BsonReader reader = new BsonReader(memoryStream);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("AboutMe", reader.Value);
      Assert.AreEqual(typeof(string), reader.ValueType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual(@"<p>I'm the Director for Research and Development for <a href=""http://www.prophoenix.com"" rel=""nofollow"">ProPhoenix</a>, a public safety software company.  This position allows me to investigate new and existing technologies and incorporate them into our product line, with the end goal being to help public safety agencies to do their jobs more effeciently and safely.</p>

<p>I'm an advocate for PowerShell, as I believe it encourages administrative best practices and allows developers to provide additional access to their applications, without needing to explicity write code for each administrative feature.  Part of my advocacy for PowerShell includes <a href=""http://blog.usepowershell.com"" rel=""nofollow"">my blog</a>, appearances on various podcasts, and acting as a Community Director for <a href=""http://powershellcommunity.org"" rel=""nofollow"">PowerShellCommunity.Org</a></p>

<p>I’m also a co-host of Mind of Root (a weekly audio podcast about systems administration, tech news, and topics).</p>
", reader.Value);
      Assert.AreEqual(typeof(string), reader.ValueType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("WebsiteUrl", reader.Value);
      Assert.AreEqual(typeof(string), reader.ValueType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("http://blog.usepowershell.com", reader.Value);
      Assert.AreEqual(typeof(string), reader.ValueType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

      Assert.IsFalse(reader.Read());
    }
コード例 #12
0
    public void WriteReadEmptyAndNullStrings()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartArray();
      writer.WriteValue("Content!");
      writer.WriteValue("");
      writer.WriteValue((string)null);
      writer.WriteEndArray();

      ms.Seek(0, SeekOrigin.Begin);

      BsonReader reader = new BsonReader(ms);
      reader.ReadRootValueAsArray = true;

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("Content!", reader.Value);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("", reader.Value);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Null, reader.TokenType);
      Assert.AreEqual(null, reader.Value);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndArray, reader.TokenType);

      Assert.IsFalse(reader.Read());
    }
コード例 #13
0
    public void SerializeEmptyAndNullStrings()
    {
      Product p = new Product();
      p.ExpiryDate = DateTime.Parse("2009-04-05T14:45:00Z");
      p.Name = null;
      p.Price = 9.95m;
      p.Sizes = new[] { "Small", "", null };

      MemoryStream ms = new MemoryStream();
      JsonSerializer serializer = new JsonSerializer();

      BsonWriter writer = new BsonWriter(ms);
      serializer.Serialize(writer, p);

      ms.Seek(0, SeekOrigin.Begin);

      BsonReader reader = new BsonReader(ms);
      Product deserializedProduct = serializer.Deserialize<Product>(reader);

      Console.WriteLine(deserializedProduct.Name);

      Assert.AreEqual(null, deserializedProduct.Name);
      Assert.AreEqual(9.95m, deserializedProduct.Price);
      Assert.AreEqual(3, deserializedProduct.Sizes.Length);
      Assert.AreEqual("Small", deserializedProduct.Sizes[0]);
      Assert.AreEqual("", deserializedProduct.Sizes[1]);
      Assert.AreEqual(null, deserializedProduct.Sizes[2]);
    }
コード例 #14
0
    public void WriteRegexPlusContent()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartObject();
      writer.WritePropertyName("regex");
      writer.WriteRegex("abc", "i");
      writer.WritePropertyName("test");
      writer.WriteRegex(string.Empty, null);
      writer.WriteEndObject();

      byte[] expected = MiscellaneousUtils.HexToBytes("1A-00-00-00-0B-72-65-67-65-78-00-61-62-63-00-69-00-0B-74-65-73-74-00-00-00-00");

      Assert.AreEqual(expected, ms.ToArray());
    }
コード例 #15
0
    public void WriteOidPlusContent()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartObject();
      writer.WritePropertyName("_id");
      writer.WriteObjectId(MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001"));
      writer.WritePropertyName("test");
      writer.WriteValue("1234£56");
      writer.WriteEndObject();

      byte[] expected = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000");

      Assert.AreEqual(expected, ms.ToArray());
    }
コード例 #16
0
    public void WriteOid()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      byte[] oid = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

      writer.WriteStartObject();
      writer.WritePropertyName("_oid");
      writer.WriteObjectId(oid);
      writer.WriteEndObject();

      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
      Assert.AreEqual("17-00-00-00-07-5F-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00", bson);

      ms.Seek(0, SeekOrigin.Begin);
      BsonReader reader = new BsonReader(ms);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
      Assert.AreEqual(oid, reader.Value);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
    }
コード例 #17
0
    public void Example()
    {
      Product p = new Product();
      p.ExpiryDate = DateTime.Parse("2009-04-05T14:45:00Z");
      p.Name = "Carlos' Spicy Wieners";
      p.Price = 9.95m;
      p.Sizes = new[] { "Small", "Medium", "Large" };

      MemoryStream ms = new MemoryStream();
      JsonSerializer serializer = new JsonSerializer();

      // serialize product to BSON
      BsonWriter writer = new BsonWriter(ms);
      serializer.Serialize(writer, p);

      Console.WriteLine(BitConverter.ToString(ms.ToArray()));
      // 7C-00-00-00-02-4E-61-6D-65-00-16-00-00-00-43-61-72-6C-
      // 6F-73-27-20-53-70-69-63-79-20-57-69-65-6E-65-72-73-00-
      // 09-45-78-70-69-72-79-44-61-74-65-00-E0-51-BD-76-20-01-
      // 00-00-01-50-72-69-63-65-00-66-66-66-66-66-E6-23-40-04-
      // 53-69-7A-65-73-00-2D-00-00-00-02-30-00-06-00-00-00-53-
      // 6D-61-6C-6C-00-02-31-00-07-00-00-00-4D-65-64-69-75-6D-
      // 00-02-32-00-06-00-00-00-4C-61-72-67-65-00-00-00


      ms.Seek(0, SeekOrigin.Begin);

      // deserialize product from BSON
      BsonReader reader = new BsonReader(ms);
      Product deserializedProduct = serializer.Deserialize<Product>(reader);

      Console.WriteLine(deserializedProduct.Name);
      // Carlos' Spicy Wieners


      Assert.AreEqual("Carlos' Spicy Wieners", deserializedProduct.Name);
      Assert.AreEqual(9.95m, deserializedProduct.Price);
      Assert.AreEqual(3, deserializedProduct.Sizes.Length);
    }
コード例 #18
0
    public void WriteConstructor()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartArray();
      writer.WriteStartConstructor("fail");
    }
コード例 #19
0
    public void WriteDateTimes()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);
      writer.DateTimeKindHandling = DateTimeKind.Unspecified;

      writer.WriteStartArray();
      writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc));
      writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Local));
      writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Unspecified));
      writer.WriteEndArray();

      ms.Seek(0, SeekOrigin.Begin);

      BsonReader reader = new BsonReader(ms);
      reader.ReadRootValueAsArray = true;
      reader.DateTimeKindHandling = DateTimeKind.Utc;

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Date, reader.TokenType);
      Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Date, reader.TokenType);
      Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Date, reader.TokenType);
      Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndArray, reader.TokenType);

      Assert.IsFalse(reader.Read());
    }
コード例 #20
0
    public void DeserializeByteArrayWithTypeNameHandling()
    {
      TestObject test = new TestObject("Test", new byte[] { 72, 63, 62, 71, 92, 55 });

      JsonSerializer serializer = new JsonSerializer();
      serializer.TypeNameHandling = TypeNameHandling.All;

      byte[] objectBytes;
      using (MemoryStream bsonStream = new MemoryStream())
      using (JsonWriter bsonWriter = new BsonWriter(bsonStream))
      {
        serializer.Serialize(bsonWriter, test);
        bsonWriter.Flush();

        objectBytes = bsonStream.ToArray();
      }

      using (MemoryStream bsonStream = new MemoryStream(objectBytes))
      using (JsonReader bsonReader = new BsonReader(bsonStream))
      {
        // Get exception here
        TestObject newObject = (TestObject)serializer.Deserialize(bsonReader);

        Assert.AreEqual("Test", newObject.Name);
        Assert.AreEqual(new byte[] { 72, 63, 62, 71, 92, 55 }, newObject.Data);
      }
    }
コード例 #21
0
    public void MultibyteCharacterPropertyNamesAndStrings()
    {
      string json = @"{
  ""ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ"": ""ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ""
}";
      JObject parsed = JObject.Parse(json);
      var memoryStream = new MemoryStream();
      var bsonWriter = new BsonWriter(memoryStream);
      parsed.WriteTo(bsonWriter);
      bsonWriter.Flush();
      memoryStream.Position = 0;

      BsonReader reader = new BsonReader(memoryStream);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ", reader.Value);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ", reader.Value);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
    }
コード例 #22
0
    public void ReadNestedArrayIntoLinq()
    {
      string hexdoc = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00";

      byte[] data = MiscellaneousUtils.HexToBytes(hexdoc);

      BsonReader reader = new BsonReader(new MemoryStream(data));
      reader.JsonNet35BinaryCompatibility = true;

      JObject o = (JObject)JToken.ReadFrom(reader);
      Assert.AreEqual(3, o.Count);

      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);
      o.WriteTo(writer);
      writer.Flush();

      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
      Assert.AreEqual(hexdoc, bson);
    }
コード例 #23
0
    public void UriGuidTimeSpanTestClassValuesTest()
    {
      UriGuidTimeSpanTestClass c1 = new UriGuidTimeSpanTestClass
      {
        Guid = new Guid("1924129C-F7E0-40F3-9607-9939C531395A"),
        NullableGuid = new Guid("9E9F3ADF-E017-4F72-91E0-617EBE85967D"),
        TimeSpan = TimeSpan.FromDays(1),
        NullableTimeSpan = TimeSpan.FromHours(1),
        Uri = new Uri("http://testuri.com")
      };

      var memoryStream = new MemoryStream();
      var bsonWriter = new BsonWriter(memoryStream);
      JsonSerializer serializer = new JsonSerializer();
      serializer.Serialize(bsonWriter, c1);
      bsonWriter.Flush();
      memoryStream.Position = 0;

      var bsonReader = new BsonReader(memoryStream);

      UriGuidTimeSpanTestClass c2 = serializer.Deserialize<UriGuidTimeSpanTestClass>(bsonReader);
      Assert.AreEqual(c1.Guid, c2.Guid);
      Assert.AreEqual(c1.NullableGuid, c2.NullableGuid);
      Assert.AreEqual(c1.TimeSpan, c2.TimeSpan);
      Assert.AreEqual(c1.NullableTimeSpan, c2.NullableTimeSpan);
      Assert.AreEqual(c1.Uri, c2.Uri);
    }
コード例 #24
0
    public void WriteRawValue()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartArray();
      writer.WriteRawValue("fail");
    }
コード例 #25
0
    public void Utf8Text()
    {
      string badText =System.IO.File.ReadAllText(@"PoisonText.txt");
      var j = new JObject();
      j["test"] = badText;

      var memoryStream = new MemoryStream();
      var bsonWriter = new BsonWriter(memoryStream);
      j.WriteTo(bsonWriter);
      bsonWriter.Flush();

      memoryStream.Position = 0;
      JObject o = JObject.Load(new BsonReader(memoryStream));

      Assert.AreEqual(badText, (string)o["test"]);
    }
コード例 #26
0
    public void WriteValueOutsideOfObjectOrArray()
    {
      MemoryStream stream = new MemoryStream();

      using (BsonWriter writer = new BsonWriter(stream))
      {
        writer.WriteValue("test");
        writer.Flush();
      }
    }
コード例 #27
0
    public void WriteAndReadEmptyListsAndDictionaries()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartObject();
      writer.WritePropertyName("Arguments");
      writer.WriteStartObject();
      writer.WriteEndObject();
      writer.WritePropertyName("List");
      writer.WriteStartArray();
      writer.WriteEndArray();
      writer.WriteEndObject();

      string bson = BitConverter.ToString(ms.ToArray());

      Assert.AreEqual("20-00-00-00-03-41-72-67-75-6D-65-6E-74-73-00-05-00-00-00-00-04-4C-69-73-74-00-05-00-00-00-00-00", bson);

      BsonReader reader = new BsonReader(new MemoryStream(MiscellaneousUtils.HexToBytes(bson)));

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("Arguments", reader.Value.ToString());

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("List", reader.Value.ToString());

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndArray, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

      Assert.IsFalse(reader.Read());
    }
コード例 #28
0
    public void ConvertRegexWithAllOptionsBson()
    {
      Regex regex = new Regex(
        "/",
        RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture);

      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);
      JsonSerializer serializer = new JsonSerializer();
      serializer.Converters.Add(new RegexConverter());

      serializer.Serialize(writer, new RegexTestClass { Regex = regex });

      string expected = "14-00-00-00-0B-52-65-67-65-78-00-2F-00-69-6D-73-75-78-00-00";
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());

      Assert.AreEqual(expected, bson);

      ms.Seek(0, SeekOrigin.Begin);
      BsonReader reader = new BsonReader(ms);
      serializer.Converters.Add(new RegexConverter());

      RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);

      Assert.AreEqual("/", c.Regex.ToString());
      Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.ExplicitCapture, c.Regex.Options);
    }
コード例 #29
0
    private string WriteAndReadStringPropertyName(string val)
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter bs = new BsonWriter(ms);
      bs.WriteStartObject();
      bs.WritePropertyName(val);
      bs.WriteValue("Dummy");
      bs.WriteEnd();

      ms.Seek(0, SeekOrigin.Begin);

      BsonReader reader = new BsonReader(ms);
      // object
      reader.Read();
      // property name
      reader.Read();
      return (string)reader.Value;
    }
コード例 #30
0
    public void WriteEmptyStrings()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      writer.WriteStartObject();
      writer.WritePropertyName("");
      writer.WriteValue("");
      writer.WriteEndObject();

      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
      Assert.AreEqual("0C-00-00-00-02-00-01-00-00-00-00-00", bson);
    }