// public methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <param name="args">The deserialization args.</param>
        /// <returns>A deserialized value.</returns>
        public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var bsonReader = context.Reader;

            var    bsonType = bsonReader.GetCurrentBsonType();
            string message;

            switch (bsonType)
            {
            case BsonType.Document:
                var dynamicContext = context.With(ConfigureDeserializationContext);
                bsonReader.ReadStartDocument();
                var document = CreateDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    var name  = bsonReader.ReadName();
                    var value = _objectSerializer.Deserialize(dynamicContext);
                    SetValueForMember(document, name, value);
                }
                bsonReader.ReadEndDocument();
                return(document);

            case BsonType.Null:
                bsonReader.ReadNull();
                return(null);

            default:
                message = string.Format("Cannot deserialize a '{0}' from BsonType '{1}'.", BsonUtils.GetFriendlyTypeName(typeof(T)), bsonType);
                throw new FormatException(message);
            }
        }
 // public methods
 /// <inheritdoc />
 public override ChangeStreamDocument <TDocument> Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
 {
     context = context.With(b => b.AllowDuplicateElementNames = true);
     return(base.Deserialize(context, args));
 }
예제 #3
0
        /*******************************************/

        public override CustomObject Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var bsonReader = context.Reader;

            var bsonType = bsonReader.CurrentBsonType;

            if (bsonReader.State != BsonReaderState.Type)
            {
                bsonType = bsonReader.GetCurrentBsonType();
            }

            string message;

            switch (bsonType)
            {
            case BsonType.Document:
                var dynamicContext = context.With(ConfigureDeserializationContext);
                bsonReader.ReadStartDocument();
                CustomObject document           = new CustomObject();
                Dictionary <string, object> dic = document.CustomData;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    var name  = bsonReader.ReadName();
                    var value = m_ObjectSerializer.Deserialize(dynamicContext);
                    switch (name)
                    {
                    case "Name":
                        document.Name = value as string;
                        break;

                    case "Tags":
                        document.Tags = new HashSet <string>(((List <object>)value).Cast <string>());
                        break;

                    case "BHoM_Guid":
                        document.BHoM_Guid = new Guid(value as string);
                        break;

                    case "CustomData":
                        while (value is CustomObject)
                        {
                            value = ((CustomObject)value).CustomData;
                        }
                        if (value is Dictionary <string, object> )
                        {
                            Dictionary <string, object> customData = value as Dictionary <string, object>;
                            if (customData.Count > 0)
                            {
                                dic["AdditionalData"] = value;
                            }
                        }
                        break;

                    default:
                        dic[name] = value;
                        break;
                    }
                }
                bsonReader.ReadEndDocument();
                return(document);

            default:
                message = string.Format("Cannot deserialize a '{0}' from BsonType '{1}'.", BsonUtils.GetFriendlyTypeName(typeof(CustomObject)), bsonType);
                throw new FormatException(message);
            }
        }
예제 #4
0
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var deserializationContext = context.With(ConfigureDeserializationContext);

            return(DeserializeValue(deserializationContext));
        }