[CSHARP-2275] Ignore name on GetCurrentBsonType() Created: 18/May/18  Updated: 25/Feb/19  Resolved: 25/Feb/19

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.6.0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Vyacheslav Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I have been getting an issue with regards to the GetCurrentBsonType() method. 
I have looked around but the only fix I have come across is to skip the name as in the VerifyBsonType() method.

My situation for some context:

I read a value and then proceed to a custom serializer 
1) read start document
2) read string
3) get current type

Doing this results in an exception thrown by

if (_state != BsonReaderState.Value)
 {
 ThrowInvalidState("GetCurrentBsonType", BsonReaderState.Value);
 }

as _state is name.

However, if I change my operations to 

1) read start document
2) read string
3) read name
4) get current type

I get another exception when the value to be deserialized is in an array - where there is no name.

My proposed solution is to add

if(_state == BsonReaderState.Name)
 {
 SkipName();
 }

before the state check as in the VerifyBsonType() method.



 Comments   
Comment by Ian Whalen (Inactive) [ 25/Feb/19 ]

Closing since we haven't heard from the reporter in 3+ weeks.

Comment by Jeffrey Yemin [ 29/Jan/19 ]

Hi bliz937,

I'm not sure what you mean. Can you post a more complete example that demonstrates clearly what you're trying to do?

Regards,
Jeff

Comment by Jeffrey Yemin [ 07/Dec/18 ]

Hi bliz937, I suspect your serializer is not using BsonReader in the way it is intended to be used. The general pattern is:

  • ReadStartDocument
  • While ReadBsonType != EndOfDocument
    • ReadName
    • GetCurrentBsonType
    • Based on the type, call one of the read methods, e.g. ReadString. If type is array or document, recurse
  • ReadEndDocument
    Have a look at BsonDocumentSerializer.DeserializeValue, BsonArray.DeserializeValue, and BsonValueSerializer.DeserializeValue for example of proper use.
Comment by Vyacheslav [ 18/May/18 ]

Added a PR

https://github.com/mongodb/mongo-csharp-driver/pull/325

Generated at Wed Feb 07 21:42:06 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.