Exemple #1
0
        /// <summary>
        /// Adds a field named TypeIDFieldName with the type id of the object instance, force=true to emit the field even if it is a known type.
        /// Returns true if type id element was added
        /// </summary>
        public virtual bool AddTypeIDField(BSONDocument doc, IBSONSerializable parent, IBSONSerializable self, object ctx, bool force = false)
        {
            if (doc == null)
            {
                return(false);
            }
            if (self == null)
            {
                return(false);
            }

            var add = force || parent == null;
            var t   = self.GetType();

            if (!add)
            {
                add = !parent.IsKnownTypeForBSONDeserialization(t);
            }

            if (add)
            {
                var id = BSONSerializableAttribute.GetGuidTypeAttribute <object, BSONSerializableAttribute>(t)
                         .TypeGuid;

                var telm = new BSONBinaryElement(TypeIDFieldName, new BSONBinary(BSONBinaryType.UUID, id.ToNetworkByteOrder()));
                doc.Set(telm);
                return(true);
            }

            return(false);
        }
Exemple #2
0
 public static GDID GDID_BSONtoCLR(BSONBinaryElement el)
 {
     try
     {
         var buf = el.Value.Data;
         return(new GDID(buf));
     }
     catch (Exception e)
     {
         throw new BSONException(StringConsts.BSON_GDID_BUFFER_ERROR.Args(e.ToMessageWithType()), e);
     }
 }
Exemple #3
0
 public static Guid GUID_BSONtoCLR(BSONBinaryElement el)
 {
     try
     {
         var val = el.Value;
         if (val.Type != BSONBinaryType.UUID && val.Type != BSONBinaryType.UUIDOld)
         {
             throw new BSONException("type != BSONBinaryType.UUID");
         }
         var buf = val.Data;
         return(buf.GuidFromNetworkByteOrder());
     }
     catch (Exception e)
     {
         throw new BSONException(StringConsts.BSON_GUID_BUFFER_ERROR.Args(e.ToMessageWithType()), e);
     }
 }