Repository navigation
BSON Serializer
karlseguin edited this page Aug 13, 2010
·
28 revisions
strong the new serializer/deserilizer are more forgiving than what is listed here. For example, values types don’t HAVE to be nullable, floats are supported, better matching for lists is attempted and so on.
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
- Enums backed by int or long (uint, ulong should work, too)
- ScopedCode (a class that can define code for use inside of MongoDB)
- IEnumerable<T> (where T follows these rules for serialization)
- A few other types should be supported soon.
- The type must declare an identifier property, "_id", or "ID" – if these are Guid or OID, NoRM will take care of assigning a value on insert (in an upcoming version)
- 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.
- Due to MongoDB constraints, DateTime are stored with millisecond precision. If you need greater precision, use double.