[CSHARP-78] C# Driver should serialize shorts (System.Int16) as if they were ints Created: 22/Oct/10  Updated: 19/Oct/16  Resolved: 22/Oct/10

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

Type: Bug Priority: Major - P3
Reporter: Justin Dearing Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

if I have an object with a member "short People" it gets serialized into a mongo collection like so:

"People" : {
"_t" : "System.Int16",
"_v" : 300
},

Why not serialize as an int? If its too big let it throw on deserialization if its too big.



 Comments   
Comment by Justin Dearing [ 22/Oct/10 ]

Thanks for the fix.

I also weighted in on Adding short, and two other types to the BSON Spec. Would like to know your opinion on that.

http://groups.google.com/group/bson/browse_thread/thread/30f15798837847ec#

Comment by Robert Stam [ 22/Oct/10 ]

I know I need to go add a section to the tutorial about serialization, but here's the scoop: sometimes it is necessary to serialize shorts the way you saw them (for example if the property were of type object we would need a discriminator to tell us it's a short and not an int). However, sometimes the context is enough to determine what needs to be done, as it is in the example you gave.

Also, primitive types all have two serialization formats: compact and full. In the compact form we are trying to minimize space used. In the full form we are trying to be very precise and not lose any information.

Anyway, the fix to your bug report is to default UseCompactRepresentation to true for primitive properties (so this is a small change to AutoMap).

And as bugs often go, fixing one thing disturbs another, so while this started out as a simple fix quite a few things were changed until all unit tests (including new ones) were passing. See the commit comment for a bit more information on what changed.

Thanks for the bug report.

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