Пример #1
0
 public BsonJsonWriter(
     TextWriter writer,
     BsonJsonWriterSettings settings
 )
 {
     this.textWriter = writer;
     this.settings = settings;
     context = null;
     state = BsonWriteState.Initial;
 }
 public BsonJsonWriter(
     TextWriter writer,
     BsonJsonWriterSettings settings
 )
 {
     this.textWriter = writer;
     this.settings = settings;
     context = new BsonJsonWriterContext(null, ContextType.TopLevel, "");
     state = BsonWriteState.Initial;
 }
 public void TestDateTime()
 {
     DateTime jan_1_2010 = new DateTime(2010, 1, 1);
     double expectedValue = (jan_1_2010.ToUniversalTime() - BsonConstants.UnixEpoch).TotalMilliseconds;
     BsonDocument document = new BsonDocument() {
         { "date", jan_1_2010 }
     };
     var settings = new BsonJsonWriterSettings { OutputMode = BsonJsonOutputMode.Strict };
     string json = document.ToJson(settings);
     string expected = "{ \"date\" : { \"$date\" : # } }".Replace("#", expectedValue.ToString());
     Assert.AreEqual(expected, json);
     settings = new BsonJsonWriterSettings { OutputMode = BsonJsonOutputMode.JavaScript };
     json = document.ToJson(settings);
     expected = "{ \"date\" : Date(#) }".Replace("#", expectedValue.ToString());
     Assert.AreEqual(expected, json);
 }
 public void TestRegularExpressionTenGen()
 {
     var json = "/pattern/gim";
     using (bsonReader = BsonReader.Create(json)) {
         Assert.AreEqual(BsonType.RegularExpression, bsonReader.ReadBsonType());
         string pattern, options;
         bsonReader.ReadRegularExpression(out pattern, out options);
         Assert.AreEqual("pattern", pattern);
         Assert.AreEqual("gim", options);
         Assert.AreEqual(BsonReadState.Done, bsonReader.ReadState);
     }
     var tenGen = new BsonJsonWriterSettings { OutputMode = BsonJsonOutputMode.TenGen };
     Assert.AreEqual(json, BsonSerializer.Deserialize<BsonRegularExpression>(new StringReader(json)).ToJson(tenGen));
 }
Пример #5
0
 public static BsonWriter Create(
     TextWriter writer,
     BsonJsonWriterSettings settings
 )
 {
     return new BsonJsonWriter(writer, settings);
 }
 public void TestRegex()
 {
     var query = Query.Matches("name", new BsonRegularExpression("acme.*corp", "i"));
     var expected = "{ \"name\" : /acme.*corp/i }";
     BsonJsonWriterSettings settings = new BsonJsonWriterSettings { OutputMode = BsonJsonOutputMode.JavaScript };
     var actual = query.ToJson(settings);
     Assert.AreEqual(expected, actual);
 }
 public void TestIndentedTwoElements()
 {
     BsonDocument document = new BsonDocument() { { "a", "x" }, { "b", "y" } };
     var settings = new BsonJsonWriterSettings { Indent = true };
     string json = document.ToJson(settings);
     string expected = "{\r\n  \"a\" : \"x\",\r\n  \"b\" : \"y\"\r\n}";
     Assert.AreEqual(expected, json);
 }
 public void TestIndentedOneElement()
 {
     BsonDocument document = new BsonDocument() { { "name", "value" } };
     var settings = new BsonJsonWriterSettings { Indent = true };
     string json = document.ToJson(settings);
     string expected = "{\r\n  \"name\" : \"value\"\r\n}";
     Assert.AreEqual(expected, json);
 }
 public void TestIndentedEmptyDocument()
 {
     BsonDocument document = new BsonDocument();
     var settings = new BsonJsonWriterSettings { Indent = true };
     string json = document.ToJson(settings);
     string expected = "{ }";
     Assert.AreEqual(expected, json);
 }
 public void TestIndentedEmbeddedDocument()
 {
     BsonDocument document = new BsonDocument() {
         { "doc", new BsonDocument { { "a", 1 }, { "b", 2 } } }
     };
     var settings = new BsonJsonWriterSettings { Indent = true };
     string json = document.ToJson(settings);
     string expected = "{\r\n  \"doc\" : {\r\n    \"a\" : 1,\r\n    \"b\" : 2\r\n  }\r\n}";
     Assert.AreEqual(expected, json);
 }