Source/NFX/Serialization/BSON/RowConverter.cs
/// <summary>
/// Tries to convert the BSON value into target CLR type. Returns true if conversion was successfull
/// </summary>
Tries to convert the BSON value into target CLR type. Returns true if conversion was successfull
Tries to convert the BSON value into target CLR type . Returns true if conversion was successfull
protected virtual bool TryConvertBSONtoCLR(Type target, BSONElement element, string targetName, out object clrValue, Func<BSONDocument, BSONElement, bool> filter)
        {
          if (element==null || element is BSONNullElement)
          {
            clrValue = null;
            return true;
          }
          if (target == typeof(object))
          {
            clrValue = DirectConvertBSONValue( element, filter );
            return true;
          }
          clrValue = null;
          if (target.IsSubclassOf(typeof(TypedRow)))
          {
            var bsonDocumentElement = element as BSONDocumentElement;
            var doc = bsonDocumentElement != null ? bsonDocumentElement.Value : null;
            if (doc==null) return false;
            var tr = (TypedRow)Activator.CreateInstance(target);
            BSONDocumentToRow(doc, tr, targetName, filter: filter);
            clrValue = tr;
            return true;
          }
          if (target.IsArray &&
              target.GetArrayRank()==1 &&
              target!=typeof(byte[]))
          {
            var bsonArrayElement = element as BSONArrayElement;
            var arr = bsonArrayElement != null ? bsonArrayElement.Value : null;
            if (arr==null) return false;
            var telm = target.GetElementType();
            var clrArray = Array.CreateInstance(telm, arr.Length);
            for(var i=0; i<arr.Length; i++)
            {
              object clrElement;
              if (!TryConvertBSONtoCLR(telm, arr[i], targetName, out clrElement, filter))
              {
                return false;
              }
              clrArray.SetValue(clrElement, i);
            }
            clrValue = clrArray;
            return true;
          }
          if (target.IsGenericType && target.GetGenericTypeDefinition() == typeof(List<>))
          {
            var bsonArrayElement = element as BSONArrayElement;
            var arr = bsonArrayElement != null ? bsonArrayElement.Value : null;
            if (arr==null) return false;
            var gargs = target.GetGenericArguments();
            var telm = gargs[0];
            var clrList = Activator.CreateInstance(target) as System.Collections.IList;
            for(var i=0; i<arr.Length; i++)
            {
              object clrElement;
              if (!TryConvertBSONtoCLR(telm, arr[i], targetName, out clrElement, filter))
              {
                return false; into element of List<t>
              }
              clrList.Add( clrElement );
            }
            clrValue = clrList;
            return true;
          }
          if (target==typeof(JSONDataMap))
          {
            var bsonDocumentElement = element as BSONDocumentElement;
            var doc = bsonDocumentElement != null ? bsonDocumentElement.Value : null;
            clrValue = BSONDocumentToJSONMap(doc, filter);
            return true;
          }
          if (target.IsEnum)
          {
            try
            {
              clrValue = Enum.Parse(target, ((BSONStringElement)element).Value, true);
              return true;
            }
            catch
            {
              return false;
            }
          }
          Func<BSONElement, object> func;
          if (m_BSONtoCLR.TryGetValue(target, out func))
          {
            try
            {
              clrValue = func(element);
            }
            catch(Exception error)
            {
              Debug.Fail("Error in BSONRowConverter.TryConvertBSONtoCLR(): " + error.ToMessageWithType());
              return false;
            }
            return true;
          }
          return false;
        }
protected virtual bool TryConvertBSONtoCLR ( Type target , BSONElement element , string targetName , out object clrValue , Func < BSONDocument , BSONElement , bool > filter ) { if ( element == null || element is BSONNullElement ) { clrValue = null ; return true ; } if ( target == typeof ( object ) ) { clrValue = DirectConvertBSONValue ( element , filter ) ; return true ; } clrValue = null ; if ( target . IsSubclassOf ( typeof ( TypedRow ) ) ) { var bsonDocumentElement = element as BSONDocumentElement ; var doc = bsonDocumentElement != null ? bsonDocumentElement . Value : null ; if ( doc == null ) return false ; var tr = ( TypedRow ) Activator . CreateInstance ( target ) ; BSONDocumentToRow ( doc , tr , targetName , filter : filter ) ; clrValue = tr ; return true ; } if ( target . IsArray && target . GetArrayRank ( ) == 1 && target != typeof ( byte [ ] ) ) { var bsonArrayElement = element as BSONArrayElement ; var arr = bsonArrayElement != null ? bsonArrayElement . Value : null ; if ( arr == null ) return false ; var telm = target . GetElementType ( ) ; var clrArray = Array . CreateInstance ( telm , arr . Length ) ; for ( var i = 0 ; i < arr . Length ; i ++ ) { object clrElement ; if ( ! TryConvertBSONtoCLR ( telm , arr [ i ] , targetName , out clrElement , filter ) ) { return false ; } clrArray . SetValue ( clrElement , i ) ; } clrValue = clrArray ; return true ; } if ( target . IsGenericType && target . GetGenericTypeDefinition ( ) == typeof ( List < > ) ) { var bsonArrayElement = element as BSONArrayElement ; var arr = bsonArrayElement != null ? bsonArrayElement . Value : null ; if ( arr == null ) return false ; var gargs = target . GetGenericArguments ( ) ; var telm = gargs [ 0 ] ; var clrList = Activator . CreateInstance ( target ) as System . Collections . IList ; for ( var i = 0 ; i < arr . Length ; i ++ ) { object clrElement ; if ( ! TryConvertBSONtoCLR ( telm , arr [ i ] , targetName , out clrElement , filter ) ) { return false ; } clrList . Add ( clrElement ) ; } clrValue = clrList ; return true ; } if ( target == typeof ( JSONDataMap ) ) { var bsonDocumentElement = element as BSONDocumentElement ; var doc = bsonDocumentElement != null ? bsonDocumentElement . Value : null ; clrValue = BSONDocumentToJSONMap ( doc , filter ) ; return true ; } if ( target . IsEnum ) { try { clrValue = Enum . Parse ( target , ( ( BSONStringElement ) element ) . Value , true ) ; return true ; } catch { return false ; } } Func < BSONElement , object > func ; if ( m_BSONtoCLR . TryGetValue ( target , out func ) ) { try { clrValue = func ( element ) ; } catch ( Exception error ) { Debug . Fail ( " " + error . ToMessageWithType ( ) ) ; return false ; } return true ; } return false ; }
Tries to convert the BSON value into target CLR type.
Tries to convert the BSON value into target CLR type .
//just unwrap Bson:CLR = 1:1, without type conversion //not document //ARRAY //exclude byte[] as it is treated with m_BSONtoCLR //not array //could not convert some element of array //LIST<T> //not array //could not convert some element of array into element of List<t> //JSONDataMap //Primitive type-targeted value //functor could not convert //could not convert
target
Type
element
BSONElement
targetName
string
object
out
filter
Func<BSONDocument, BSONElement, bool>