public FieldHiderBookmark(BsonReaderBookmark parentBookmark, BsonReaderState?currentState, string currentName, int currentDepth) : base(currentState ?? parentBookmark.State, parentBookmark.CurrentBsonType, currentName ?? parentBookmark.CurrentName) { _currentState = currentState; _currentName = currentName; _currentDepth = currentDepth; _parentBookmark = parentBookmark; }
public void ReturnToBookmark(BsonReaderBookmark bookmark) { var fieldHiderBookmark = (FieldHiderBookmark)bookmark; _parent.ReturnToBookmark(fieldHiderBookmark._parentBookmark); _currentDepth = fieldHiderBookmark._currentDepth; _currentName = fieldHiderBookmark._currentName; _currentState = fieldHiderBookmark._currentState; }
public Type GetActualType(BsonReader bsonReader, Type nominalType) { BsonReaderBookmark bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); Type actualType = nominalType; if (bsonReader.FindElement("Type")) { BsonValue discriminator = BsonSerializer.Deserialize <BsonValue>(bsonReader); actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } bsonReader.ReturnToBookmark(bookmark); return(actualType); }
/***************************************************/ public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { // This is where we can call the Version_Engine to return the new type string from the old on if exist BsonReaderBookmark bookmark = context.Reader.GetBookmark(); IBsonSerializer bSerializer = BsonSerializer.LookupSerializer(typeof(BsonDocument)); BsonDocument doc = bSerializer.Deserialize(context, args) as BsonDocument; BsonDocument newDoc = Versioning.Convert.ToNewVersion(doc); if (newDoc == null || newDoc.Equals(doc)) { Engine.Reflection.Compute.RecordWarning("The type " + doc["_t"] + " is unknown -> data returned as custom objects."); context.Reader.ReturnToBookmark(bookmark); IBsonSerializer customSerializer = BsonSerializer.LookupSerializer(typeof(CustomObject)); return(customSerializer.Deserialize(context, args)); } else { return(Convert.FromBson(newDoc)); } }
/// <summary> /// Deserializes an object from a binary source. /// </summary> /// <param name="Reader">Binary deserializer.</param> /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param> /// <param name="Embedded">If the object is embedded into another.</param> /// <returns>Deserialized object.</returns> public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded) { BsonReaderBookmark Bookmark = Reader.GetBookmark(); BsonType? DataTypeBak = DataType; if (!DataType.HasValue) { DataType = Reader.ReadBsonType(); } switch (DataType.Value) { case BsonType.Document: break; case BsonType.Boolean: return(Reader.ReadBoolean()); case BsonType.Int32: return(Reader.ReadInt32()); case BsonType.Int64: return(Reader.ReadInt64()); case BsonType.Decimal128: return((decimal)Reader.ReadDecimal128()); case BsonType.Double: return(Reader.ReadDouble()); case BsonType.DateTime: return(ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime())); case BsonType.String: case BsonType.Symbol: case BsonType.JavaScript: case BsonType.JavaScriptWithScope: return(Reader.ReadString()); case BsonType.Binary: return(Reader.ReadBytes()); case BsonType.Null: Reader.ReadNull(); return(null); default: throw new Exception("Object or value expected."); } LinkedList <KeyValuePair <string, object> > Properties = new LinkedList <KeyValuePair <string, object> >(); LinkedList <KeyValuePair <string, object> > LowerCase = null; string TypeName = string.Empty; Guid ObjectId = Guid.Empty; string CollectionName = string.Empty; string FieldName; BsonType ValueType; object Value; Reader.ReadStartDocument(); while (Reader.State == BsonReaderState.Type) { ValueType = Reader.ReadBsonType(); if (ValueType == BsonType.EndOfDocument) { break; } FieldName = Reader.ReadName(); switch (ValueType) { case BsonType.Array: Value = GeneratedObjectSerializerBase.ReadArray(null, this.Provider, Reader, ValueType); break; case BsonType.Binary: Value = Reader.ReadBytes(); break; case BsonType.Boolean: Value = Reader.ReadBoolean(); break; case BsonType.DateTime: Value = ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()); break; case BsonType.Decimal128: Value = (decimal)Reader.ReadDecimal128(); break; case BsonType.Document: Value = this.Deserialize(Reader, ValueType, true); break; case BsonType.Double: Value = Reader.ReadDouble(); break; case BsonType.Int32: Value = Reader.ReadInt32(); break; case BsonType.Int64: Value = Reader.ReadInt64(); break; case BsonType.JavaScript: Value = Reader.ReadJavaScript(); break; case BsonType.JavaScriptWithScope: Value = Reader.ReadJavaScriptWithScope(); break; case BsonType.Null: Value = null; Reader.ReadNull(); break; case BsonType.ObjectId: Value = Reader.ReadObjectId(); break; case BsonType.String: Value = Reader.ReadString(); break; case BsonType.Symbol: Value = Reader.ReadSymbol(); break; default: throw new Exception("Unrecognized data type: " + ValueType.ToString()); } switch (FieldName) { case "_id": if (Value is Guid Guid) { ObjectId = Guid; } else if (Value is string s) { ObjectId = new Guid(s); } else if (Value is byte[] A) { ObjectId = new Guid(A); } else if (Value is ObjectId ObjId) { ObjectId = GeneratedObjectSerializerBase.ObjectIdToGuid(ObjId); } else { throw new Exception("Unrecognized Object ID type: " + Value.GetType().FullName); } break; case "_type": TypeName = Value?.ToString(); if (this.returnTypedObjects && !string.IsNullOrEmpty(TypeName)) { Type DesiredType = Types.GetType(TypeName); if (DesiredType is null) { DesiredType = typeof(GenericObject); } if (DesiredType != typeof(GenericObject)) { IObjectSerializer Serializer2 = this.provider.GetObjectSerializer(DesiredType); Reader.ReturnToBookmark(Bookmark); return(Serializer2.Deserialize(Reader, DataTypeBak, Embedded)); } } break; case "_collection": CollectionName = Value?.ToString(); break; default: if (FieldName.EndsWith("_L")) { string s = FieldName.Substring(0, FieldName.Length - 2); bool Ignore = false; foreach (KeyValuePair <string, object> P in Properties) { if (P.Key == s) { Ignore = true; break; } } if (!Ignore) { if (LowerCase is null) { LowerCase = new LinkedList <KeyValuePair <string, object> >(); } LowerCase.AddLast(new KeyValuePair <string, object>(s, Value)); } } else { Properties.AddLast(new KeyValuePair <string, object>(FieldName, Value)); } break; } } if (!(LowerCase is null)) { foreach (KeyValuePair <string, object> P in LowerCase) { bool Ignore = false; foreach (KeyValuePair <string, object> P2 in Properties) { if (P2.Key == P.Key) { Ignore = true; break; } } if (!Ignore) { Properties.AddLast(new KeyValuePair <string, object>(P.Key + "_L", P.Value)); } } } Reader.ReadEndDocument(); return(new GenericObject(CollectionName, TypeName, ObjectId, Properties)); }
/***************************************************/ private object DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args) { // First try to recover the object type IBsonReader reader = context.Reader; BsonReaderBookmark bookmark = reader.GetBookmark(); Type actualType = typeof(CustomObject); try { actualType = _discriminatorConvention.GetActualType(reader, typeof(object)); } catch (Exception e) { BsonDocument doc = null; try { context.Reader.ReturnToBookmark(bookmark); IBsonSerializer bSerializer = BsonSerializer.LookupSerializer(typeof(BsonDocument)); doc = bSerializer.Deserialize(context, args) as BsonDocument; } catch { } if (doc != null && doc.Contains("_t") && doc["_t"].AsString == "DBNull") { return(null); } else { actualType = typeof(IDeprecated); } } context.Reader.ReturnToBookmark(bookmark); if (actualType == null) { return(null); } // Make sure the type is not deprecated if (Config.AllowUpgradeFromBson && actualType.IIsDeprecated() && !Config.TypesWithoutUpgrade.Contains(actualType)) { actualType = typeof(IDeprecated); } // Handle the special case where the type is object if (actualType == typeof(object)) { BsonType currentBsonType = reader.GetCurrentBsonType(); if (currentBsonType == BsonType.Document && context.DynamicDocumentSerializer != null) { return(context.DynamicDocumentSerializer.Deserialize(context, args)); } reader.ReadStartDocument(); reader.ReadEndDocument(); return(new object()); } // Handle the general case of finding the correct deserialiser and calling it try { IBsonSerializer bsonSerializer = BsonSerializer.LookupSerializer(actualType); if (bsonSerializer.GetType().Name == "EnumerableInterfaceImplementerSerializer`2" && context.Reader.CurrentBsonType == BsonType.Document) { if (!m_FallbackSerialisers.ContainsKey(actualType)) { CreateFallbackSerialiser(actualType); } bsonSerializer = m_FallbackSerialisers[actualType]; } return(bsonSerializer.Deserialize(context, args)); } catch (Exception e) { context.Reader.ReturnToBookmark(bookmark); if (e is FormatException && e.InnerException != null && (e.InnerException is FormatException || e.InnerException is BsonSerializationException)) { // A child of the object is causing problems. Try to recover from custom object IBsonSerializer customSerializer = BsonSerializer.LookupSerializer(typeof(CustomObject)); object result = customSerializer.Deserialize(context, args); Guid objectId = ((CustomObject)result).BHoM_Guid; if (!Config.TypesWithoutUpgrade.Contains(actualType)) { if (m_StackCounter.ContainsKey(objectId)) { m_StackCounter[objectId] += 1; } else { m_StackCounter[objectId] = 1; } if (m_StackCounter[objectId] < 10) { result = Convert.FromBson(result.ToBson()); m_StackCounter.Remove(objectId); } } if (result is CustomObject) { Engine.Reflection.Compute.RecordWarning("The type " + actualType.FullName + " is unknown -> data returned as custom objects."); Config.TypesWithoutUpgrade.Add(actualType); } return(result); } else if (actualType != typeof(IDeprecated)) { // Try the deprecated object serialiser IBsonSerializer deprecatedSerializer = BsonSerializer.LookupSerializer(typeof(IDeprecated)); return(deprecatedSerializer.Deserialize(context, args)); } else { // Last resort: just return the custom object Engine.Reflection.Compute.RecordWarning("The type " + actualType.FullName + " is unknown -> data returned as custom objects."); IBsonSerializer customSerializer = BsonSerializer.LookupSerializer(typeof(CustomObject)); return(customSerializer.Deserialize(context, args)); } } }
/***************************************************/ private object DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args) { // First try to recover the object type IBsonReader reader = context.Reader; BsonReaderBookmark bookmark = reader.GetBookmark(); Type actualType = typeof(CustomObject); try { actualType = _discriminatorConvention.GetActualType(reader, typeof(object)); } catch { try { context.Reader.ReturnToBookmark(bookmark); DeprecatedSerializer deprecatedSerialiser = new DeprecatedSerializer(); return(deprecatedSerialiser.Deserialize(context, args)); } catch { } } context.Reader.ReturnToBookmark(bookmark); if (actualType == null) { return(null); } // Handle the special case where the type is object if (actualType == typeof(object)) { BsonType currentBsonType = reader.GetCurrentBsonType(); if (currentBsonType == BsonType.Document && context.DynamicDocumentSerializer != null) { return(context.DynamicDocumentSerializer.Deserialize(context, args)); } reader.ReadStartDocument(); reader.ReadEndDocument(); return(new object()); } // Handle the general case of finding the correct deserialiser and calling it try { if (!BsonClassMap.IsClassMapRegistered(actualType)) { Compute.RegisterClassMap(actualType); // LookupSerializer creates the classMap if it doesn't exist so important to do it through our own method } IBsonSerializer bsonSerializer = BsonSerializer.LookupSerializer(actualType); if (bsonSerializer.GetType().Name == "EnumerableInterfaceImplementerSerializer`2" && context.Reader.CurrentBsonType == BsonType.Document) { if (!m_FallbackSerialisers.ContainsKey(actualType)) { CreateFallbackSerialiser(actualType); } bsonSerializer = m_FallbackSerialisers[actualType]; } else if (actualType.Name == "Dictionary`2" && context.Reader.CurrentBsonType == BsonType.Document) { DictionarySerializer dicSerialiser = new DictionarySerializer(); return(dicSerialiser.Deserialize(context, args)); } return(bsonSerializer.Deserialize(context, args)); } catch (Exception e) { if (e.Message.Contains("Could not load file or assembly")) { Engine.Reflection.Compute.RecordError(e.Message); } context.Reader.ReturnToBookmark(bookmark); DeprecatedSerializer deprecatedSerialiser = new DeprecatedSerializer(); return(deprecatedSerialiser.Deserialize(context, args)); } }
/***************************************************/ private object DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args) { // First try to recover the object type IBsonReader reader = context.Reader; BsonReaderBookmark bookmark = reader.GetBookmark(); Type actualType = typeof(CustomObject); try { actualType = _discriminatorConvention.GetActualType(reader, typeof(object)); } catch { // This is were we can call the Version_Engine to return the new type string from the old one if exists string recordedType = GetCurrentTypeValue(reader); // If failed, return Custom object context.Reader.ReturnToBookmark(bookmark); Engine.Reflection.Compute.RecordWarning("The type " + recordedType + " is unknown -> data returned as custom objects."); IBsonSerializer customSerializer = BsonSerializer.LookupSerializer(typeof(CustomObject)); return(customSerializer.Deserialize(context, args)); } // Handle the special case where the type is object if (actualType == typeof(object)) { BsonType currentBsonType = reader.GetCurrentBsonType(); if (currentBsonType == BsonType.Document && context.DynamicDocumentSerializer != null) { return(context.DynamicDocumentSerializer.Deserialize(context, args)); } reader.ReadStartDocument(); reader.ReadEndDocument(); return(new object()); } // Handle the genral case of finding the correct deserialiser and calling it IBsonSerializer bsonSerializer = BsonSerializer.LookupSerializer(actualType); IBsonPolymorphicSerializer bsonPolymorphicSerializer = bsonSerializer as IBsonPolymorphicSerializer; if (bsonPolymorphicSerializer != null && bsonPolymorphicSerializer.IsDiscriminatorCompatibleWithObjectSerializer) { bookmark = context.Reader.GetBookmark(); try { return(bsonSerializer.Deserialize(context, args)); } catch { context.Reader.ReturnToBookmark(bookmark); Engine.Reflection.Compute.RecordWarning("Cannot find a definition of type " + actualType.FullName + " that matches the object to deserialise -> data returned as custom objects."); IBsonSerializer customSerializer = BsonSerializer.LookupSerializer(typeof(CustomObject)); CustomObject fallback = customSerializer.Deserialize(context, args) as CustomObject; //This is where we will try to get the correct object type from the custom object using the versionning engine // If failed, just return the custom object return(fallback); } } object result = null; bool flag = false; reader.ReadStartDocument(); while (reader.ReadBsonType() != 0) { string text = reader.ReadName(); if (text == _discriminatorConvention.ElementName) { reader.SkipValue(); } else { if (!(text == "_v")) { throw new FormatException($"Unexpected element name: '{text}'."); } result = bsonSerializer.Deserialize(context); flag = true; } } reader.ReadEndDocument(); if (!flag) { throw new FormatException("_v element missing."); } return(result); }