示例#1
0
        private BSONElement jToBCore(string name, object value, TemplateArg[] args)
        {
            if (name != null)  //array elm may be null
            {
                name = substName(name, args);
            }

            var bType = BSONElementType.Null;

            if (value != null)
            {
                if (value is string)
                {
                    var sv = (string)value;
                    if (args != null && sv.Length > ARG_TPL_PREFIX.Length && sv.StartsWith(ARG_TPL_PREFIX)) //parameter name
                    {
                        sv = sv.Substring(ARG_TPL_PREFIX.Length);                                           //get rid of "$$"
                        var arg = args.FirstOrDefault(a => a.Name.EqualsOrdIgnoreCase(sv));
                        if (arg.Name != null)
                        {
                            if (!arg.BSONType.HasValue)
                            {
                                var be = jToB(arg.Name, arg.Value, null);
                                return(BSONElement.MakeOfType(be.ElementType, name, be.ObjectValue));
                            }

                            bType = arg.BSONType.Value;
                            value = arg.Value;
                        }
                        else
                        {
                            throw new BSONException("Template JSON parameter '{0}' not found in args".Args(sv));
                        }
                    }
                    else
                    {
                        bType = BSONElementType.String;
                    }
                }
                else if (value is UInt64)
                {
                    bType = BSONElementType.Int64;
                }
                else if (value is Int32)
                {
                    bType = BSONElementType.Int32;
                }
                else if (value is Int64)
                {
                    bType = BSONElementType.Int64;
                }
                else if (value is DateTime)
                {
                    bType = BSONElementType.DateTime;
                }
                else if (value is bool)
                {
                    bType = BSONElementType.Boolean;
                }
                else if (value is double || value is float)
                {
                    bType = BSONElementType.Double;
                }
                else if (value is byte[])
                {
                    return(RowConverter.ByteBuffer_CLRtoBSON(name, (byte[])value));
                }
                else if (value is JSONDataMap)
                {
                    var doc    = value as JSONDataMap;
                    var subdoc = new BSONDocument();
                    buildFromTemplateArgs(subdoc, doc, args);
                    value = subdoc;
                    bType = BSONElementType.Document;
                }
                else if (value is IEnumerable)
                {
                    var arr  = value as IEnumerable;
                    var list = new List <BSONElement>();
                    foreach (var e in arr)
                    {
                        list.Add(jToB(null, e, args));
                    }
                    value = list.ToArray();
                    bType = BSONElementType.Array;
                }
                else if (value is NFX.DataAccess.Distributed.GDID)
                {
                    return(RowConverter.GDID_CLRtoBSON(name, (NFX.DataAccess.Distributed.GDID)value));
                }
                else if (value is decimal)
                {
                    return(RowConverter.Decimal_CLRtoBSON(name, (decimal)value));
                }

                else if (value is NFX.Financial.Amount)
                {
                    return(RowConverter.Amount_CLRtoBSON(name, (NFX.Financial.Amount)value));
                }
                else
                {
                    throw new BSONException("Template JSON value type '{0}' not supported in BSON binding".Args(value.GetType()));
                }
            }

            return(BSONElement.MakeOfType(bType, name, value));
        }
示例#2
0
        public static BSONDocument Add(this BSONDocument document, string name, object value, bool skipNull = false, bool required = false)
        {
            if (value == null)
            {
                return(onNullOrEmpty(document, name, skipNull, required));
            }

            switch (Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.Empty:
            case TypeCode.DBNull:   return(document.Set(new BSONNullElement(name)));

            case TypeCode.Boolean:  return(document.Set(new BSONBooleanElement(name, (bool)value)));

            case TypeCode.Char:     return(document.Set(new BSONStringElement(name, value.ToString())));

            case TypeCode.SByte:    return(document.Set(new BSONInt32Element(name, (sbyte)value)));

            case TypeCode.Byte:     return(document.Set(new BSONInt32Element(name, (byte)value)));

            case TypeCode.Int16:    return(document.Set(new BSONInt32Element(name, (short)value)));

            case TypeCode.UInt16:   return(document.Set(new BSONInt32Element(name, (ushort)value)));

            case TypeCode.Int32:    return(document.Set(new BSONInt32Element(name, (int)value)));

            case TypeCode.UInt32:   return(document.Set(new BSONInt32Element(name, (int)(uint)value)));

            case TypeCode.Int64:    return(document.Set(new BSONInt64Element(name, (long)value)));

            case TypeCode.UInt64:   return(document.Set(new BSONInt64Element(name, (long)(ulong)value)));

            case TypeCode.Single:   return(document.Set(new BSONDoubleElement(name, (float)value)));

            case TypeCode.Double:   return(document.Set(new BSONDoubleElement(name, (double)value)));

            case TypeCode.Decimal:  return(document.Set(RowConverter.Decimal_CLRtoBSON(name, (decimal)value)));

            case TypeCode.DateTime: return(document.Set(new BSONDateTimeElement(name, (DateTime)value)));

            case TypeCode.String:   return(document.Set(new BSONStringElement(name, (string)value)));

            case TypeCode.Object:
            {
                if (value is Guid)
                {
                    var guid = (Guid)value;
                    if (guid == Guid.Empty)
                    {
                        return(onNullOrEmpty(document, name, skipNull, required));
                    }
                    return(document.Set(new BSONBinaryElement(name, new BSONBinary(BSONBinaryType.UUID, ((Guid)value).ToByteArray()))));
                }
                else if (value is GDID)
                {
                    var gdid = (GDID)value;
                    if (gdid.IsZero)
                    {
                        return(onNullOrEmpty(document, name, skipNull, required));
                    }
                    return(document.Set(RowConverter.GDID_CLRtoBSON(name, gdid)));
                }
                else if (value is TimeSpan)
                {
                    return(document.Set(new BSONInt64Element(name, ((TimeSpan)value).Ticks)));
                }
                else if (value is BSONDocument)
                {
                    return(document.Set(new BSONDocumentElement(name, (BSONDocument)value)));
                }
                else if (value is byte[])
                {
                    return(document.Set(new BSONBinaryElement(name, new BSONBinary(BSONBinaryType.GenericBinary, (byte[])value))));
                }
                throw new BSONException("BSONDocument.Add(not supported object type '{0}')".Args(value.GetType().Name));
            }

            default: throw new BSONException("BSONDocument.Add(not supported object type '{0}')".Args(value.GetType().Name));
            }
        }