Пример #1
0
        static BsonValue ToBsonValue(object value, ScriptBlock convert, int depth)
        {
            IncSerializationDepth(ref depth);

            if (value == null)
            {
                return(BsonNull.Value);
            }

            value = BaseObject(value, out PSObject custom);

            // case: custom
            if (custom != null)
            {
                return(ToBsonDocumentFromProperties(null, custom, convert, null, depth));
            }

            // case: BsonValue
            if (value is BsonValue bson)
            {
                return(bson);
            }

            // case: string
            if (value is string text)
            {
                return(new BsonString(text));
            }

            // case: document
            if (value is IConvertibleToBsonDocument cd)
            {
                return(cd.ToBsonDocument());
            }

            // case: dictionary
            if (value is IDictionary dictionary)
            {
                return(ToBsonDocumentFromDictionary(null, dictionary, convert, null, depth));
            }

            // case: bytes or collection
            if (value is IEnumerable en)
            {
                //_191108_183844
                if (en is byte[] bytes)
                {
                    return(new BsonBinaryData(bytes));
                }

                var array = new BsonArray();
                foreach (var it in en)
                {
                    array.Add(ToBsonValue(it, convert, depth));
                }
                return(array);
            }

            // try to map BsonValue
            if (BsonTypeMapper.TryMapToBsonValue(value, out BsonValue bson2))
            {
                return(bson2);
            }

            // try to serialize class
            var type = value.GetType();

            if (TypeIsDriverSerialized(type))
            {
                return(BsonExtensionMethods.ToBsonDocument(value, type));
            }

            // no converter? die
            if (convert == null)
            {
                //! use this type
                throw new ArgumentException(Res.CannotConvert2(type, nameof(BsonValue)));
            }

            try
            {
                value = DocumentInput.ConvertValue(convert, value);
            }
            catch (RuntimeException re)
            {
                //! use this type
                throw new ArgumentException($"Converter script was called for '{type}' and failed with '{re.Message}'.", re);
            }

            // do not pass converter twice
            return(ToBsonValue(value, null, depth));
        }
Пример #2
0
        static BsonValue ToBsonValue(object value, DocumentInput input, int depth)
        {
            IncSerializationDepth(ref depth);

            if (value == null)
            {
                return(BsonNull.Value);
            }

            PSObject custom;

            value = BaseObject(value, out custom);

            // case: custom
            if (custom != null)
            {
                return(ToBsonDocumentFromProperties(null, custom, input, null, depth));
            }

            // case: BsonValue
            var bson = value as BsonValue;

            if (bson != null)
            {
                return(bson);
            }

            // case: string
            var text = value as string;

            if (text != null)
            {
                return(new BsonString(text));
            }

            // case: document
            var cd = value as IConvertibleToBsonDocument;

            if (cd != null)
            {
                return(cd.ToBsonDocument());
            }

            // case: dictionary
            var dictionary = value as IDictionary;

            if (dictionary != null)
            {
                return(ToBsonDocumentFromDictionary(null, dictionary, input, null, depth));
            }

            // case: collection
            var enumerable = value as IEnumerable;

            if (enumerable != null)
            {
                var array = new BsonArray();
                foreach (var it in enumerable)
                {
                    array.Add(ToBsonValue(it, input, depth));
                }
                return(array);
            }

            // try to create BsonValue
            try
            {
                return(BsonValue.Create(value));
            }
            catch (ArgumentException ae)
            {
                if (input == null)
                {
                    throw;
                }

                try
                {
                    value = input.ConvertValue(value);
                }
                catch (RuntimeException re)
                {
                    throw new ArgumentException(                     //! use this type
                              string.Format(null, @"Converter script was called on ""{0}"" and failed with ""{1}"".", ae.Message, re.Message), re);
                }

                if (value == null)
                {
                    throw;
                }

                // do not call converter twice
                return(ToBsonValue(value, null, depth));
            }
        }
Пример #3
0
        static BsonValue ToBsonValue(object value, DocumentInput input, int depth)
        {
            IncSerializationDepth(ref depth);

            if (value == null)
                return BsonNull.Value;

            PSObject custom;
            value = BaseObject(value, out custom);

            // case: custom
            if (custom != null)
                return ToBsonDocumentFromProperties(null, custom, input, null, depth);

            // case: BsonValue
            var bson = value as BsonValue;
            if (bson != null)
                return bson;

            // case: string
            var text = value as string;
            if (text != null)
                return new BsonString(text);

            // case: document
            var cd = value as IConvertibleToBsonDocument;
            if (cd != null)
                return cd.ToBsonDocument();

            // case: dictionary
            var dictionary = value as IDictionary;
            if (dictionary != null)
                return ToBsonDocumentFromDictionary(null, dictionary, input, null, depth);

            // case: collection
            var enumerable = value as IEnumerable;
            if (enumerable != null)
            {
                var array = new BsonArray();
                foreach (var it in enumerable)
                    array.Add(ToBsonValue(it, input, depth));
                return array;
            }

            // try to create BsonValue
            try
            {
                Register();
                return BsonValue.Create(value);
            }
            catch (ArgumentException ae)
            {
                if (input == null)
                    throw;

                try
                {
                    value = input.ConvertValue(value);
                }
                catch (RuntimeException re)
                {
                    throw new ArgumentException( //! use this type
                        string.Format(null, @"Converter script was called on ""{0}"" and failed with ""{1}"".", ae.Message, re.Message), re);
                }

                if (value == null)
                    throw;

                // do not call converter twice
                return ToBsonValue(value, null, depth);
            }
        }