1

Closed

cannot serialize Uri using BsonWriter

description

Error writing String value. BSON must start with an Object or Array
in BsonWriter.cs:212: internal void AddToken(BsonToken token)
 
[Test]
public void SerializeUriWithBsonWriter()
{
var test = new Uri("http://www.google.com/");
 
JsonSerializer serializer = new JsonSerializer();
serializer.TypeNameHandling = TypeNameHandling.All;
 
byte[] bytes;
using (var memoryStream = new MemoryStream())
using (var bsonWriter = new BsonWriter(memoryStream))
{
    serializer.Serialize(bsonWriter, test); //throws Exception "JsonWriterException: Error writing String value. BSON must start with an Object or Array."
    bsonWriter.Flush();
 
    bytes = memoryStream.ToArray();
}
 
using (var bsonStream = new MemoryStream(bytes))
using (var bsonReader = new BsonReader(bsonStream))
{
    Uri newObject = (Uri)serializer.Deserialize(bsonReader);
 
    Assert.AreEqual("http://www.google.com/", newObject.AbsoluteUri);
}
}
Closed Jul 25, 2012 at 2:42 AM by JamesNK
The error message is correct.

comments