Repository navigation
BSON Serializer
atheken edited this page Aug 13, 2010
·
28 revisions
NoRM’s BSON serializer is very powerful, but you must be careful not to abuse it. Only public instance properties are de/serialized by the serializer. Fields & static members are ignored.
- All readable properties may be:
- int
- bool
- double (floats will be stored as doubles in the db)
- datetime
- string
- byte[]
- BSONOID (this is the default "_id" value that’s generated if you don’t specify something different)
- Regex
- Guid
- IEnumerable<T> (where T follows these rules for serialization)
- A few other types should be supported soon.
- Cyclic graphs are not checked and attempting to serialize them will crash your app, maybe someday this will change, but not this day.
- All public instance properties of the type must be writeable
- All of the types above are supported with four important caveats:
- Value-types must use their correspondant Nullable type (i.e. the class must define "int?" properties, rather than "int" properties)
- Members of type T must follow these rules for Deserialization and "T:class, new()"
- IEnumerable<T> is always deserialized as List<T>, therefore, your IEnumerable properties must be assignable from List<T>
- Remember how floats are stored as doubles? Well, they are deserialized to "double?"
- Deserializing property names is done in a case-insensitive way, although MongoDB is case-sensitive. So it is recommended that you don’t store documents where property names vary only by case.
- The serializer doesn’t hold your hand, if you ask it to deserialize a value and cast to a type that is illegal, you’ll pay the price for your sins.
- Know what you put into MongoDB and what you’re getting out.
- A general rule in MongoDB is that no document be larger than 4MB, you must take care to adhere to this rule, although a future version of NoRM will produce exceptions if you attempt to use documents larger than this size.