本文整理汇总了C#中Newtonsoft.Json.Bson.BsonWriter.Flush方法的典型用法代码示例。如果您正苦于以下问题:C# BsonWriter.Flush方法的具体用法?C# BsonWriter.Flush怎么用?C# BsonWriter.Flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Bson.BsonWriter
的用法示例。
在下文中一共展示了BsonWriter.Flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendCmdRequest
public bool SendCmdRequest(CmdRequest request)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect("127.0.0.1", Port);
using (NetworkStream stream = new NetworkStream(socket))
{
using (BsonWriter writer = new BsonWriter(stream))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, request);
writer.Flush();
writer.Close();
}
stream.Close();
}
}
catch(Exception ex)
{
Logger.AddError(string.Format("Failed to send cmd request to checker with port = {0}", Port), ex);
return false;
}
return true;
}
示例2: OnWriteToStream
protected override void OnWriteToStream(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext context)
{
var serializer = new JsonSerializer();
using (var writer = new BsonWriter(stream.PreventClose())) {
serializer.Serialize(writer, value);
writer.Flush();
}
}
示例3: WriteNewtonsoftBson
private static long WriteNewtonsoftBson(string path)
{
FileStream fs = File.OpenWrite(path);
using (var bsonWriter = new BsonWriter(fs) { CloseOutput = false })
{
var jsonSerializer = new JsonSerializer();
jsonSerializer.Serialize(bsonWriter, GetTestObjects());
bsonWriter.Flush();
}
fs.Close();
return new FileInfo(path).Length;
}
示例4: WriteToStreamAsync
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, TransportContext transportContext)
{
if (type == null) throw new ArgumentNullException("type is null");
if (writeStream == null) throw new ArgumentNullException("Write stream is null");
var tcs = new TaskCompletionSource<object>();
using (BsonWriter bsonWriter = new BsonWriter(writeStream) { CloseOutput = false })
{
JsonSerializer jsonSerializer = JsonSerializer.Create(jsonNetSettings);
jsonSerializer.Serialize(bsonWriter, value);
bsonWriter.Flush();
tcs.SetResult(null);
}
return tcs.Task;
}
示例5: GetBytes
public byte[] GetBytes(UncommittedEvent evnt)
{
string eventName;
JObject serializedPayload = _formatter.Serialize(evnt.Payload, out eventName);
var output = new MemoryStream();
var writer = new BinaryWriter(output);
writer.Write(evnt.CommitId.ToByteArray());
writer.Write(evnt.EventIdentifier.ToByteArray());
writer.Write(evnt.EventSequence);
writer.Write(evnt.EventTimeStamp.Ticks);
writer.Write(eventName);
writer.Write(evnt.EventVersion.ToString());
var bsonWriter = new BsonWriter(output);
serializedPayload.WriteTo(bsonWriter);
bsonWriter.Flush();
output.Flush();
return output.ToArray();
}
示例6: WriteResponseBodyAsync
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (selectedEncoding == null)
{
throw new ArgumentNullException(nameof(selectedEncoding));
}
var response = context.HttpContext.Response;
using (var bsonWriter = new BsonWriter(response.Body) { CloseOutput = false })
{
var jsonSerializer = CreateJsonSerializer();
jsonSerializer.Serialize(bsonWriter, context.Object);
bsonWriter.Flush();
}
}
示例7: UriGuidTimeSpanTestClassValuesTest
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);
}
示例8: UriGuidTimeSpanTestClassEmptyTest
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);
}
示例9: MultibyteCharacterPropertyNamesAndStrings
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);
}
示例10: WriteNestedArray
public void WriteNestedArray()
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.WriteStartObject();
writer.WritePropertyName("_id");
writer.WriteValue(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF"));
writer.WritePropertyName("a");
writer.WriteStartArray();
for (int i = 1; i <= 8; i++)
{
double value = (i != 5)
? Convert.ToDouble(i)
: 5.78960446186581E+77d;
writer.WriteValue(value);
}
writer.WriteEndArray();
writer.WritePropertyName("b");
writer.WriteValue("test");
writer.WriteEndObject();
writer.Flush();
ms.Seek(0, SeekOrigin.Begin);
string expected = "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";
string bson = BytesToHex(ms.ToArray());
Assert.AreEqual(expected, bson);
}
示例11: WriteArrayBsonFromSite
public void WriteArrayBsonFromSite()
{
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.WriteStartArray();
writer.WriteValue("a");
writer.WriteValue("b");
writer.WriteValue("c");
writer.WriteEndArray();
writer.Flush();
ms.Seek(0, SeekOrigin.Begin);
string expected = "20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00";
string bson = BytesToHex(ms.ToArray());
Assert.AreEqual(expected, bson);
}
示例12: ReadNestedArrayIntoLinq
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);
}
示例13: WriteBytes
public void WriteBytes()
{
byte[] data = Encoding.UTF8.GetBytes("Hello world!");
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.WriteStartArray();
writer.WriteValue("a");
writer.WriteValue("b");
writer.WriteValue(data);
writer.WriteEndArray();
writer.Flush();
ms.Seek(0, SeekOrigin.Begin);
string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00";
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
Assert.AreEqual(expected, bson);
}
示例14: WriteReply
private void WriteReply(NetworkStream stream, CmdServerReply reply)
{
JsonSerializer serializer = new JsonSerializer();
using (BsonWriter writer = new BsonWriter(stream))
{
serializer.Serialize(writer, reply);
writer.Flush();
writer.Close();
}
}
示例15: WriteValueOutsideOfObjectOrArray
public void WriteValueOutsideOfObjectOrArray()
{
MemoryStream stream = new MemoryStream();
using (BsonWriter writer = new BsonWriter(stream))
{
writer.WriteValue("test");
writer.Flush();
}
}