On Friday, May 25, 2012 1:15:45 PM UTC-4, sLaVeN wrote:
> Hi Robert,
> yes, there could have been an issue there because I was trying to
> convert the stream to string as well as bytes array which was giving
> me those issues. However, I tried now to pass in the stream directly
> to the Deserializer and it looks like its trying to do something but
> complains about Duplicate element. There is no duplicate element. We
> have WebApp where we can make these calls and Json that Im getting
> back is fine, no duplicates but when i try to do it from my C# code, i
> get the following error. Could there be a bug in the Driver? Thanks
> Unhandled Exception: System.InvalidOperationException: Duplicate
> element name 'm
> _fleg'.
> at MongoDB.Bson.BsonDocument.Add(BsonElement element)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonArray.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonArray.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.BsonDocument.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonArray.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonValue.ReadFrom(BsonReader bsonReader)
> at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader,
> BsonElement& elem
> ent)
> at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader,
> Type nominalT
> ype, IBsonSerializationOptions options)
> at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader
> bsonReade
> r, Type nominalType, IBsonSerializationOptions options)
> at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(Stream
> stream, Type
> nominalType)
> at
> MongoDB.Bson.Serialization.BsonSerializer.Deserialize[TNominalType]
> (Stream
> stream)
> at Test.Program.HttpRequest() in C:\project\XPathFormatingTools\Test
> \Test\Pro
> gram.cs:line 86
> at Test.Program.Main(String[] args) in C:\project
> \XPathFormatingTools\Test\Te
> st\Program.cs:line 61
> Press any key to continue . . .
> On May 24, 7:29 pm, Robert Stam <rob...@10gen.com> wrote:
> > BSON is a binary encoding. If your file is not a binary file you won't
> be
> > able to decode it as BSON.
> > If your file is a text file containing JSON you will want to parse it a
> > different way:
> > string json; // load this from your file
> > BsonDocument document = BsonSerializer.Deserialize<BsonDocument>(json);
> > On Thu, May 24, 2012 at 7:57 PM, sLaVeN <slug...@gmail.com> wrote:
> > > Thanks Robert for the quick reply. I tried that but I am getting an
> > > exception.
> > > string bson = File.ReadAllText(@"C:\Users\myuser\Desktop\bson.txt");
> > > byte[] bytesarr = Encoding.UTF8.GetBytes(bson); // Tried different
> > > Encoding here but that didn't seem to matter.
> > > BsonDocument document =
> MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(bytesar
> r);
> > > Steps:
> > > 1) Convert bsonresponse to bytes array
> > > 2) Call the Deserializer you have provided
> > > 3) Execute and i get following exception
> > > Unhandled Exception: System.IO.EndOfStreamException: Attempted to read
> > > past the
> > > end of the stream.
> > > at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream, Int32 count)
> > > at MongoDB.Bson.IO.BsonBuffer.LoadFrom(Stream stream)
> > > at MongoDB.Bson.IO.BsonReader.Create(Stream stream,
> > > BsonBinaryReaderSettings
> > > settings)
> > > at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(Stream
> > > stream, Type
> > > nominalType)
> > > at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(Byte[]
> > > bytes, Type n
> > > ominalType)
> > > at
> > > MongoDB.Bson.Serialization.BsonSerializer.Deserialize[TNominalType]
> > > (Byte[]
> > > bytes)
> > > at Test.Program.Main(String[] args) in C:\project
> > > \XPathFormatingTools\Test\Te
> > > st\Program.cs:line 54
> > > Thanks Alot!
> > > On May 24, 3:49 pm, Robert Stam <rob...@10gen.com> wrote:
> > > > You can also deserialize BSON data directly from a binary stream,
> like
> > > this:
> > > > Stream stream;
> > > > var document = BsonSerializer.Deserialize<BsonDocument>(stream);
> > > > On Thu, May 24, 2012 at 6:43 PM, Robert Stam <rob...@10gen.com>
> wrote:
> > > > > Write whatever code you need to get the BSON data as a byte array,
> and
> > > > > then you can decode it using:
> > > > > byte[] bytes;
> > > > > BsonDocument document =
> > > > > BsonSerializer.Deserialize<BsonDocument>(bytes);
> > > > > On Thu, May 24, 2012 at 6:38 PM, sLaVeN <slug...@gmail.com>
> wrote:
> > > > >> Hi,
> > > > >> I need help decoding Bson. I am making a WebRequest and getting
> > > > >> encoded Bson blob which I need to decode in C#. IN java you would
> do
> > > > >> something like this
> > > > >> static main(args)
> > > > >> {
> > > > >> if(args.length != 1)
> > > > >> {
> > > > >> println("Usage
> > > > >> <binFile>")
> > > > >> }
> > > > >> BSONDecoder decoder = new
> > > > >> BSONDecoder()
> > > > >> BSONObject bson =
> decoder.readObject
> > > > >> (new FileInputStream(args[0]))
> > > > >> OutputBson(System.out, bson, 0)
> > > > >> }
> > > > >> but I need to do similar in C#. Could somebody help me do this in
> C#?
> > > > >> What would be BSONDecoder equivalant in C#? I have the Mongo
> > > > >> Driver,BSON DLL so i just need to find out the right set of calls
> to
> > > > >> decode it.
> > > > >> Thanks