Is there any way to set data types while converting JSON to BSON with c# driver?

I’m wondering, is there any way to set data types while converting JSON to BSON with c# driver?
I already tried this :

BsonDocument doc = BsonDocument.Parse(“{ t : -54542356354645452145 }”);

and this is the result!

‘Value was either too large or too small for an Int64.’

Data is in JSON and receives by a REST API, I can’t define any C# model for this data. There are so many irregularities.

Thanks in advance!

Hello @Nima_Niazmand, welcome to the MongoDB Community forum.

The number is too big to be converted to a int or a long - you can store it as NumberDecimal BSON type. You may have to create a BsonDocument with the number field of type Decimal128 / BsonDecimal128 and save to the database.

Do you mean modifying the content of the JSON before converting to BSON? I can’t modify the JSON, I have no idea what is inside them.

Extract the fields of the JSON and convert to BSON document. The JSON data is as strings (key-value). Based upon the key the value can be formatted to appropriate BSON.

I can’t modify the JSON, I have no idea what is inside them.

What do you intend to do with the data? Just parse and store as it comes. In such case store all fields as strings - and convert them later based upon the field name.

Well… That’s what I’m looking for but, the parser does not accept any config/mapping! Data are already stored in SQLSERVER as strings. There is no point in converting them later. Besides, there are more than 100 million records. I have no idea about the future of the data. I’m the migrator! I guess somebody chose the wrong database for storing JSON data! now I have to manually convert them!

I guess I have to answer my own question! There are two options. Since C# driver’s serialization is based on a wrong assumption. I have to write my own parser. it would be tricky but anything is better than what’s shipped with the driver or using a real JSON document database like I_have_no_idea_DB.