本文整理汇总了C#中Raven.Imports.Newtonsoft.Json.Bson.BsonReader类的典型用法代码示例。如果您正苦于以下问题:C# BsonReader类的具体用法?C# BsonReader怎么用?C# BsonReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BsonReader类属于Raven.Imports.Newtonsoft.Json.Bson命名空间,在下文中一共展示了BsonReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadSingleObject
public void ReadSingleObject()
{
byte[] data = MiscellaneousUtils.HexToBytes("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00");
MemoryStream ms = new MemoryStream(data);
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.AreEqual("Blah", reader.Value);
Assert.AreEqual(typeof(string), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
Assert.AreEqual(1L, reader.Value);
Assert.AreEqual(typeof(long), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
}
示例2: DeserializeLargeBsonObject
public void DeserializeLargeBsonObject()
{
byte[] data = System.IO.File.ReadAllBytes(@"SpaceShipV2.bson");
MemoryStream ms = new MemoryStream(data);
BsonReader reader = new BsonReader(ms);
JObject o = (JObject)JToken.ReadFrom(reader);
Assert.AreEqual("1", (string)o["$id"]);
}
示例3: DoStreamedTest
public static void DoStreamedTest(Db db, int count)
{
List<Object> cached = new List<Object>();
for(var i = 0 ; i < count ; i++) {
var stream = new ReadStream(db, i);
var reader = new BsonReader(stream);
var obj = RavenJObject.ReadFrom(reader);
cached.Add(obj);
if(cached.Count > 5)
cached.RemoveAt(0);
Console.WriteLine("Loaded {0}", i);
}
}
示例4: CloseInput
public void CloseInput()
{
MemoryStream ms = new MemoryStream();
BsonReader reader = new BsonReader(ms);
Assert.IsTrue(ms.CanRead);
reader.Close();
Assert.IsFalse(ms.CanRead);
ms = new MemoryStream();
reader = new BsonReader(ms) { CloseInput = false };
Assert.IsTrue(ms.CanRead);
reader.Close();
Assert.IsTrue(ms.CanRead);
}
示例5: DoUnStreamedTest
public static void DoUnStreamedTest(Db db, int count)
{
List<Object> cached = new List<Object>();
for(var i = 0 ; i < count ; i++) {
var size = db.GetSize(i);
var lo = new Byte[size];
db.Get(i, lo, 0, lo.Length);
var stream = new MemoryStream(lo);
var reader = new BsonReader(stream);
var obj = RavenJObject.ReadFrom(reader);
cached.Add(obj);
if(cached.Count > 5)
cached.RemoveAt(0);
Console.WriteLine("Loaded {0}", i);
}
}
示例6: WriteReadEmptyAndNullStrings
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());
}
示例7: WriteOid
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);
CollectionAssert.AreEquivalent(oid, (byte[])reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
示例8: SerializeGoogleGeoCode
public void SerializeGoogleGeoCode()
{
string json = @"{
""name"": ""1600 Amphitheatre Parkway, Mountain View, CA, USA"",
""Status"": {
""code"": 200,
""request"": ""geocode""
},
""Placemark"": [
{
""address"": ""1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA"",
""AddressDetails"": {
""Country"": {
""CountryNameCode"": ""US"",
""AdministrativeArea"": {
""AdministrativeAreaName"": ""CA"",
""SubAdministrativeArea"": {
""SubAdministrativeAreaName"": ""Santa Clara"",
""Locality"": {
""LocalityName"": ""Mountain View"",
""Thoroughfare"": {
""ThoroughfareName"": ""1600 Amphitheatre Pkwy""
},
""PostalCode"": {
""PostalCodeNumber"": ""94043""
}
}
}
}
},
""Accuracy"": 8
},
""Point"": {
""coordinates"": [-122.083739, 37.423021, 0]
}
}
]
}";
GoogleMapGeocoderStructure jsonGoogleMapGeocoder = JsonConvert.DeserializeObject<GoogleMapGeocoderStructure>(json);
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, jsonGoogleMapGeocoder);
ms.Seek(0, SeekOrigin.Begin);
BsonReader reader = new BsonReader(ms);
GoogleMapGeocoderStructure bsonGoogleMapGeocoder = (GoogleMapGeocoderStructure)serializer.Deserialize(reader, typeof (GoogleMapGeocoderStructure));
Assert.IsNotNull(bsonGoogleMapGeocoder);
Assert.AreEqual("1600 Amphitheatre Parkway, Mountain View, CA, USA", bsonGoogleMapGeocoder.Name);
Assert.AreEqual("200", bsonGoogleMapGeocoder.Status.Code);
Assert.AreEqual("geocode", bsonGoogleMapGeocoder.Status.Request);
IList<Placemark> placemarks = bsonGoogleMapGeocoder.Placemark;
Assert.IsNotNull(placemarks);
Assert.AreEqual(1, placemarks.Count);
Placemark placemark = placemarks[0];
Assert.AreEqual("1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", placemark.Address);
Assert.AreEqual(8, placemark.AddressDetails.Accuracy);
Assert.AreEqual("US", placemark.AddressDetails.Country.CountryNameCode);
Assert.AreEqual("CA", placemark.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName);
Assert.AreEqual("Santa Clara", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName);
Assert.AreEqual("Mountain View", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName);
Assert.AreEqual("1600 Amphitheatre Pkwy", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName);
Assert.AreEqual("94043", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
Assert.AreEqual(-122.083739m, placemark.Point.Coordinates[0]);
Assert.AreEqual(37.423021m, placemark.Point.Coordinates[1]);
Assert.AreEqual(0m, placemark.Point.Coordinates[2]);
}
示例9: 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-00-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00";
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
Assert.AreEqual(expected, bson);
BsonReader reader = new BsonReader(new MemoryStream(ms.ToArray()));
reader.ReadRootValueAsArray = true;
reader.Read();
reader.Read();
reader.Read();
reader.Read();
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
CollectionAssert.AreEquivalent(data, (byte[])reader.Value);
}
示例10: ReadEmptyStrings
public void ReadEmptyStrings()
{
string bson = "0C-00-00-00-02-00-01-00-00-00-00-00";
BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson)));
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);
Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
}
示例11: ReadEndOfStream
public void ReadEndOfStream()
{
BsonReader reader = new BsonReader(new MemoryStream());
Assert.IsFalse(reader.Read());
}
示例12: ReadNestedArray
public void ReadNestedArray()
{
string hexdoc = "82-00-00-00-07-5F-69-64-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 = HexToBytes(hexdoc);
MemoryStream ms = new MemoryStream(data);
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.AreEqual("_id", reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
CollectionAssert.AreEquivalent(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF"), (byte[])reader.Value);
Assert.AreEqual(typeof(byte[]), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("a", reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
for (int i = 1; i <= 8; i++)
{
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
double value = (i != 5)
? Convert.ToDouble(i)
: 5.78960446186581E+77d;
Assert.AreEqual(value, reader.Value);
}
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("b", reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("test", reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
}
示例13: ReadOid
public void ReadOid()
{
byte[] data = HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000");
MemoryStream ms = new MemoryStream(data);
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.AreEqual("_id", reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
CollectionAssert.AreEquivalent(HexToBytes("4ABBED9D1D8B0F0218000001"), (byte[])reader.Value);
Assert.AreEqual(typeof(byte[]), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("test", reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("1234£56", reader.Value);
Assert.AreEqual(typeof(string), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
}
示例14: ReadBytes
public void ReadBytes()
{
byte[] data = HexToBytes("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");
MemoryStream ms = new MemoryStream(data);
BsonReader reader = new BsonReader(ms, true, DateTimeKind.Utc);
#pragma warning disable 612,618
reader.JsonNet35BinaryCompatibility = true;
#pragma warning restore 612,618
Assert.AreEqual(true, reader.ReadRootValueAsArray);
Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("a", reader.Value);
Assert.AreEqual(typeof(string), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("b", reader.Value);
Assert.AreEqual(typeof(string), reader.ValueType);
byte[] encodedStringData = reader.ReadAsBytes();
Assert.IsNotNull(encodedStringData);
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
Assert.AreEqual(encodedStringData, reader.Value);
Assert.AreEqual(typeof(byte[]), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
Assert.IsFalse(reader.Read());
Assert.AreEqual(JsonToken.None, reader.TokenType);
string decodedString = Encoding.UTF8.GetString(encodedStringData, 0, encodedStringData.Length);
Assert.AreEqual("Hello world!", decodedString);
}
示例15: ReadAsInt32BadString
public void ReadAsInt32BadString()
{
ExceptionAssert.Throws<JsonReaderException>(() =>
{
byte[] data = HexToBytes("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");
MemoryStream ms = new MemoryStream(data);
BsonReader reader = new BsonReader(ms);
Assert.AreEqual(false, reader.ReadRootValueAsArray);
Assert.AreEqual(DateTimeKind.Local, reader.DateTimeKindHandling);
reader.ReadRootValueAsArray = true;
reader.DateTimeKindHandling = DateTimeKind.Utc;
Assert.AreEqual(true, reader.ReadRootValueAsArray);
Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
reader.ReadAsInt32();
}, "Could not convert string to integer: a. Path '[0]'.");
}