public Type GetActualType(IBsonReader bsonReader, Type nominalType) { if (nominalType == typeof(OrderModel)) { var ret = nominalType; var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); if (bsonReader.FindElement(orderType)) { var value = bsonReader.ReadString(); ret = OrderTypeResolver.ResolveOrderType(value); if (ret == null) { throw new Exception("Could not find type " + value); } if (!ret.IsSubclassOf(typeof(OrderModel))) { throw new Exception("Database type does not inherit from OrderModel."); } } bsonReader.ReturnToBookmark(bookmark); return(ret); } else { return(nominalType); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var obj = JObject.Load(reader); OrderModel orderModel; var type = obj["Type"]; if (type == null) { throw new ArgumentNullException("Type", "Order type is null"); } string orderType = type.Value <string>(); orderModel = OrderTypeResolver.CreateOrderInstance(orderType); serializer.Populate(obj.CreateReader(), orderModel); return(orderModel); }