Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Need help decoding Bson
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  17 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
sLaVeN  
View profile  
 More options May 24 2012, 6:38 pm
From: sLaVeN <slug...@gmail.com>
Date: Thu, 24 May 2012 15:38:33 -0700 (PDT)
Local: Thurs, May 24 2012 6:38 pm
Subject: Need help decoding Bson
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile  
 More options May 24 2012, 6:43 pm
From: Robert Stam <rob...@10gen.com>
Date: Thu, 24 May 2012 18:43:30 -0400
Local: Thurs, May 24 2012 6:43 pm
Subject: Re: [mongodb-csharp] Need help decoding Bson

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);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile  
 More options May 24 2012, 6:49 pm
From: Robert Stam <rob...@10gen.com>
Date: Thu, 24 May 2012 18:49:11 -0400
Local: Thurs, May 24 2012 6:49 pm
Subject: Re: [mongodb-csharp] Need help decoding Bson

You can also deserialize BSON data directly from a binary stream, like this:

    Stream stream;
    var document = BsonSerializer.Deserialize<BsonDocument>(stream);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sLaVeN  
View profile  
 More options May 24 2012, 7:57 pm
From: sLaVeN <slug...@gmail.com>
Date: Thu, 24 May 2012 16:57:09 -0700 (PDT)
Local: Thurs, May 24 2012 7:57 pm
Subject: Re: Need help decoding Bson
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile  
 More options May 24 2012, 10:29 pm
From: Robert Stam <rob...@10gen.com>
Date: Thu, 24 May 2012 22:29:41 -0400
Local: Thurs, May 24 2012 10:29 pm
Subject: Re: [mongodb-csharp] Re: Need help decoding Bson

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);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sLaVeN  
View profile   Translate to Translated (View Original)
 More options May 25 2012, 1:15 pm
From: sLaVeN <slug...@gmail.com>
Date: Fri, 25 May 2012 10:15:45 -0700 (PDT)
Local: Fri, May 25 2012 1:15 pm
Subject: Re: Need help decoding Bson
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
craiggwilson  
View profile   Translate to Translated (View Original)
 More options May 25 2012, 2:44 pm
From: craiggwilson <craiggwil...@gmail.com>
Date: Fri, 25 May 2012 11:44:55 -0700 (PDT)
Local: Fri, May 25 2012 2:44 pm
Subject: Re: Need help decoding Bson

Is the stream you are passing in encoded in BSON or is it a JSON string
encoded as binary?  As in, does the stream match the bson spec here:  
http://bsonspec.org/#/specification?

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sLaVeN  
View profile  
 More options May 25 2012, 4:16 pm
From: sLaVeN <slug...@gmail.com>
Date: Fri, 25 May 2012 13:16:28 -0700 (PDT)
Local: Fri, May 25 2012 4:16 pm
Subject: Re: Need help decoding Bson
Hmm, I am not sure how the server is returning it to me but I would
assume it would be BSON because we have some other apps that do
similar things and display it as JSON. However, I could go with an
assumption that it could be JSON encoded as binary. If thats the case,
how would I got about decoding the Json Binary to regular JSON?

Thanks

On May 25, 11:44 am, craiggwilson <craiggwil...@gmail.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
craiggwilson  
View profile   Translate to Translated (View Original)
 More options May 25 2012, 4:38 pm
From: craiggwilson <craiggwil...@gmail.com>
Date: Fri, 25 May 2012 13:38:27 -0700 (PDT)
Local: Fri, May 25 2012 4:38 pm
Subject: Re: Need help decoding Bson

Well, here's a test.  Take the stream you are getting from the server, use
Encoding.UTF8.GetString(byetArray) and save that to a file.  if it is
legible, then it is a json string.  If not, we'll go from there.

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
craiggwilson  
View profile  
 More options May 25 2012, 5:12 pm
From: craiggwilson <craiggwil...@gmail.com>
Date: Fri, 25 May 2012 14:12:40 -0700 (PDT)
Local: Fri, May 25 2012 5:12 pm
Subject: Re: Need help decoding Bson

Cool, that looks like it is probably bson.  Here's what to do next.  Take
the byte array and save it to a file on disk.  Then we can use the
bsondump.exe in the bin directory of your mongo installation and inspect
the file.

bsondump.exe {filename}

If you can, post the results here.  If not, tell me if everything looks
valid.

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sLaVeN  
View profile  
 More options May 25 2012, 7:08 pm
From: sLaVeN <slug...@gmail.com>
Date: Fri, 25 May 2012 16:08:30 -0700 (PDT)
Local: Fri, May 25 2012 7:08 pm
Subject: Re: Need help decoding Bson
Hi Craig,

I just ran the bsondump and it seems everything is looking good now.
Unfortunately I cant post the results since its company confidential
data but if there is a way to diagrammatically do this now, that would
be job done :).

Thanks

On May 25, 2:12 pm, craiggwilson <craiggwil...@gmail.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
craiggwilson  
View profile   Translate to Translated (View Original)
 More options May 25 2012, 7:26 pm
From: craiggwilson <craiggwil...@gmail.com>
Date: Fri, 25 May 2012 16:26:55 -0700 (PDT)
Local: Fri, May 25 2012 7:26 pm
Subject: Re: Need help decoding Bson

So if the bsondump prints good data, then I'm not sure what to say about
your problem.  Robert gave the way the .net driver uses to deserialize
bson.  BsonSerializer.Deserialize<BsonDocument>(bson) //or some other
overload

Can you post the code you are using from receipt of the bson to where you
are decoding it (or as much as possible)?

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sLaVeN  
View profile  
 More options May 26 2012, 11:00 am
From: sLaVeN <slug...@gmail.com>
Date: Sat, 26 May 2012 08:00:52 -0700 (PDT)
Local: Sat, May 26 2012 11:00 am
Subject: Re: Need help decoding Bson
This is sort of what I am using right now to just test it out

        public static HttpRequest(string url)
        {
            WebRequest request = null;

            string url = url;
            request = WebRequest.Create(url);

            request.Method = "GET";
            request.Proxy = null;
            request.Timeout = 60000;

            Stream stream = request.GetResponse().GetResponseStream();
            var document =
MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(stream) ; //
Tried Byte array, string and few other types.
    }

On May 25, 4:26 pm, craiggwilson <craiggwil...@gmail.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
craiggwilson  
View profile   Translate to Translated (View Original)
 More options May 28 2012, 6:25 pm
From: craiggwilson <craiggwil...@gmail.com>
Date: Mon, 28 May 2012 15:25:26 -0700 (PDT)
Local: Mon, May 28 2012 6:25 pm
Subject: Re: Need help decoding Bson

That looks right to me.  We are going to be kinda stuck here unless you can
provide us with a sample of the bson you are trying to decode, or at least
an example that breaks.  If you are okay providing 10gen with a copy of the
data so we can debug through it, then you can create a jira issue for the
project "Community Private".  It will only be seen by 10gen employees.  You
can assign it to me, Craig Wilson and I'll look at it asap.  
https://jira.mongodb.org/secure/CreateIssue!default.jspa

Hope this will help.

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile  
 More options May 28 2012, 6:56 pm
From: Robert Stam <rob...@10gen.com>
Date: Mon, 28 May 2012 18:56:46 -0400
Local: Mon, May 28 2012 6:56 pm
Subject: Re: [mongodb-csharp] Re: Need help decoding Bson

In one of your earlier messages you mentioned that you got this exception:

Unhandled Exception: System.InvalidOperationException: Duplicate element
name 'm_fleg'

If you got that exception it would seem that you were in the process of
properly decoding a binary BSON stream but that the C# driver found a
duplicate element name (even though you don't think there are any
duplicates).

The BSON spec is silent on whether duplicate names are allowed. The various
drivers and the server differ on whether they allow duplicate names to be
created in the first place and how they respond when they encounter a
duplicate name. The C# driver is on the strict side and throws exceptions
when a duplicate element is encountered. The server often doesn't even
notice when duplicate element names are stored. Some drivers handle
duplicate names by overwriting the value with the last value encountered.

I strongly suspect that you really do have a duplicate element name, even
if you don't think you do.

On Mon, May 28, 2012 at 6:25 PM, craiggwilson <craiggwil...@gmail.com>wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sLaVeN  
View profile  
 More options May 29 2012, 9:01 pm
From: sLaVeN <slug...@gmail.com>
Date: Tue, 29 May 2012 18:01:47 -0700 (PDT)
Local: Tues, May 29 2012 9:01 pm
Subject: Re: Need help decoding Bson
I just double check and yes indeed, there are some duplicate elements.
Now I downloaded the source code and I do see that you guys have a
flag to allow duplicate elements. How can I use this flag to allow
duplicate elements? I think that would solve the problem.

Thanks

On May 28, 3:56 pm, Robert Stam <rob...@10gen.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile  
 More options May 29 2012, 10:59 pm
From: Robert Stam <rob...@10gen.com>
Date: Tue, 29 May 2012 22:59:19 -0400
Local: Tues, May 29 2012 10:59 pm
Subject: Re: [mongodb-csharp] Re: Need help decoding Bson

I would strongly recommend that you never allow duplicate element names to
occur. They behave differently in different situations and lead to
unpredictable outcomes.

However, in the C# driver you can tell it to always allow duplicate names
during deserialization if you want to. I would recommend you use this
technique only while cleaning up the data.

I created a test document with duplicate elements like this:

    var server = MongoServer.Create("mongodb://localhost/?safe=true");
    var database = server.GetDatabase("test");
    var collection = database.GetCollection("test");

    var document = new BsonDocument(true)
    {
        { "x", 1 },
        { "x", 2 },
        { "embedded", new BsonDocument(true)
            {
                { "y", 1 },
                { "y", 2 }
            }
        }
    };

    collection.Drop();
    collection.Insert(document);

You can read documents with duplicate elements like this:

    DocumentSerializationOptions.Defaults = new
DocumentSerializationOptions { AllowDuplicateNames = true };
    document = collection.FindOne();
    Console.WriteLine(document.ToJson());

But keep in mind that setting DocumentSerializationOptions.Defaults is a
global setting and should only be done once when the program is starting
up. Manipulating DocumentSerializationOptions.Defaults back and forth
between different values is not thread safe (which may not be an issue if
your program is single threaded).

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »