/// <summary>
        /// Serializes a value.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="args">The serialization args.</param>
        /// <param name="value">The value.</param>
        protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, GeoJsonBoundingBox <TCoordinates> value)
        {
            var bsonWriter = context.Writer;

            // serialize min and max to a dummy document and then flatten the two arrays and serialize that
            var document = new BsonDocument();

            using (var documentWriter = new BsonDocumentWriter(document))
            {
                var documentContext = BsonSerializationContext.CreateRoot(documentWriter);
                documentWriter.WriteStartDocument();
                documentWriter.WriteName("min");
                _coordinatesSerializer.Serialize(documentContext, value.Min);
                documentWriter.WriteName("max");
                _coordinatesSerializer.Serialize(documentContext, value.Max);
                documentWriter.WriteEndDocument();
            }

            var flattenedArray = new BsonArray();

            flattenedArray.AddRange(document["min"].AsBsonArray);
            flattenedArray.AddRange(document["max"].AsBsonArray);

            BsonArraySerializer.Instance.Serialize(context, flattenedArray);
        }
示例#2
0
        public override BsonDocument Render(
            IBsonSerializer documentSerializer,
            IBsonSerializerRegistry serializerRegistry)
        {
            StringFieldDefinitionHelper.Resolve(
                _path,
                documentSerializer,
                out string?resolvedFieldName,
                out IBsonSerializer? resolvedFieldSerializer);

            resolvedFieldSerializer ??= documentSerializer;

            if (_value is BsonDocument bsonDocument)
            {
                return(new BsonDocument(resolvedFieldName, bsonDocument));
            }

            if (_value is BsonValue bsonValue)
            {
                return(new BsonDocument(resolvedFieldName, bsonValue));
            }

            if (_value is MongoDbFilterDefinition mongoDbOperation)
            {
                if (_path is "")
                {
                    return(mongoDbOperation.Render(resolvedFieldSerializer, serializerRegistry));
                }

                return(new BsonDocument(
                           resolvedFieldName,
                           mongoDbOperation.Render(resolvedFieldSerializer, serializerRegistry)));
            }

            var document = new BsonDocument();

            using var bsonWriter = new BsonDocumentWriter(document);
            var context = BsonSerializationContext.CreateRoot(bsonWriter);

            bsonWriter.WriteStartDocument();
            bsonWriter.WriteName(resolvedFieldName);
            if (_value is IList values)
            {
                bsonWriter.WriteStartArray();
                foreach (var value in values)
                {
                    resolvedFieldSerializer.Serialize(context, value);
                }

                bsonWriter.WriteEndArray();
            }
            else
            {
                resolvedFieldSerializer.Serialize(context, _value);
            }

            bsonWriter.WriteEndDocument();

            return(document);
        }
        public static BsonArray SerializeValues(this ISerializationExpression field, Type itemType, IEnumerable values)
        {
            Ensure.IsNotNull(field, nameof(field));
            Ensure.IsNotNull(itemType, nameof(itemType));
            Ensure.IsNotNull(values, nameof(values));

            var itemSerializer = FieldValueSerializerHelper.GetSerializerForValueType(field.Serializer, BsonSerializer.SerializerRegistry, itemType);

            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("values");
                bsonWriter.WriteStartArray();
                foreach (var value in values)
                {
                    itemSerializer.Serialize(context, value);
                }
                bsonWriter.WriteEndArray();
                bsonWriter.WriteEndDocument();

                return((BsonArray)tempDocument[0]);
            }
        }
        private static BsonDocumentWriter CreateVersionWriter()
        {
            var writer = new BsonDocumentWriter(new BsonDocument());

            writer.WriteStartDocument();
            writer.WriteName("version");
            return(writer);
        }
示例#5
0
    /// <inheritdoc />
    public override BsonDocument Render(IBsonSerializer <TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
    {
        var renderedFieldDefinition = _field.Render(documentSerializer, serializerRegistry, true);
        var document = new BsonDocument();

        using var writer = new BsonDocumentWriter(document);
        writer.WriteStartDocument();
        writer.WriteName(renderedFieldDefinition.FieldName);
        RenderEqualityQuery(writer);
        writer.WriteEndDocument();
        return(document);
    }
        /// <summary>
        /// Converts a value to a BsonValue by serializing it.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="serializer">The serializer.</param>
        /// <param name="value">The value.</param>
        /// <returns>The serialized value.</returns>
        public static BsonValue ToBsonValue <TValue>(this IBsonSerializer <TValue> serializer, TValue value)
        {
            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                var context = BsonSerializationContext.CreateRoot(writer);
                writer.WriteStartDocument();
                writer.WriteName("x");
                serializer.Serialize(context, value);
                writer.WriteEndDocument();
            }
            return(document[0]);
        }
        /// <summary>
        /// Serializes the value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The serialized value.</returns>
        public BsonValue SerializeValue(object value)
        {
            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot <BsonDocument>(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("value");
                context.SerializeWithChildContext(_serializer, value);
                bsonWriter.WriteEndDocument();
                return(tempDocument[0]);
            }
        }
示例#8
0
        public static BsonValue SerializeValue(IBsonSerializer serializer, object value)
        {
            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("_v");
                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, value);
                writer.WriteEndDocument();
            }
            return(document["_v"]);
        }
示例#9
0
        public void ReserializingStringIdEntityMaintainsStateExceptNulls()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(StringIdModel)).GetIdProperty();
            var serializer      = new EntityNavigationCollectionSerializer <StringIdModel>(foreignProperty);

            var initialCollection = new EntityNavigationCollection <StringIdModel>(foreignProperty)
            {
                new StringIdModel
                {
                    Description = "1"
                },
                new StringIdModel
                {
                    Id          = "5ac383379a5f1303784400f8",
                    Description = "2"
                }
            };
            EntityNavigationCollection <StringIdModel> deserializedCollection = null;

            initialCollection.AddForeignId("5ac383379a5f1303784400f9");

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }

            using (var reader = new BsonDocumentReader(document))
            {
                reader.ReadBsonType();
                reader.ReadStartDocument();
                reader.ReadBsonType();
                reader.SkipName();

                var context = BsonDeserializationContext.CreateRoot(reader);
                deserializedCollection = serializer.Deserialize(context) as EntityNavigationCollection <StringIdModel>;
            }

            Assert.AreEqual(3, initialCollection.GetForeignIds().Count());
            Assert.AreEqual(2, deserializedCollection.GetForeignIds().Count());
            Assert.IsTrue(deserializedCollection.GetForeignIds().All(id => initialCollection.GetForeignIds().Contains(id)));
        }
示例#10
0
        public static BsonValue SerializeValue(this ISerializationExpression field, object value)
        {
            Ensure.IsNotNull(field, nameof(field));

            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("value");
                field.Serializer.Serialize(context, value);
                bsonWriter.WriteEndDocument();
                return(tempDocument[0]);
            }
        }
        public UpdateDefinition <BsonDocument> BuildUpdatesForSave(
            UpdateDefinition <BsonDocument> update, TrackablePocoTracker <T> tracker, params object[] keyValues)
        {
            var keyNamespace = DocumentHelper.ToDotPathWithTrailer(keyValues);

            // mimic BsonClassMapSerializer.Serialize to serialize changed value correctly.
            // build bson elements containing set field values

            var setDocument   = new BsonDocument();
            var setBsonWriter = new BsonDocumentWriter(setDocument);
            var setContext    = BsonSerializationContext.CreateRoot(setBsonWriter);

            setBsonWriter.WriteStartDocument();

            foreach (var change in tracker.ChangeMap)
            {
                if (change.Value.NewValue != null)
                {
                    BsonMemberMap memberMap;
                    if (_propertyToMemberMap.TryGetValue(change.Key, out memberMap) == false)
                    {
                        continue;
                    }

                    setBsonWriter.WriteName(memberMap.ElementName);
                    memberMap.GetSerializer().Serialize(setContext, change.Value.NewValue);
                }
                else
                {
                    update = (update == null)
                        ? Builders <BsonDocument> .Update.Unset(keyNamespace + change.Key.Name)
                        : update.Unset(keyNamespace + change.Key.Name);
                }
            }

            setBsonWriter.WriteEndDocument();
            setBsonWriter.Dispose();

            foreach (var element in setDocument.Elements)
            {
                update = (update == null)
                    ? Builders <BsonDocument> .Update.Set(keyNamespace + element.Name, element.Value)
                    : update.Set(keyNamespace + element.Name, element.Value);
            }

            return(update);
        }
示例#12
0
        public void ReserializingObjectIdIdEntityMaintainsState()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(ObjectIdIdModel)).GetIdProperty();
            var serializer      = new EntityNavigationCollectionSerializer <ObjectIdIdModel>(foreignProperty);

            var initialCollection = new EntityNavigationCollection <ObjectIdIdModel>(foreignProperty)
            {
                new ObjectIdIdModel
                {
                    Id          = ObjectId.GenerateNewId(),
                    Description = "1"
                }
            };
            EntityNavigationCollection <ObjectIdIdModel> deserializedCollection = null;

            initialCollection.AddForeignId(ObjectId.GenerateNewId());

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }

            using (var reader = new BsonDocumentReader(document))
            {
                reader.ReadBsonType();
                reader.ReadStartDocument();
                reader.ReadBsonType();
                reader.SkipName();

                var context = BsonDeserializationContext.CreateRoot(reader);
                deserializedCollection = serializer.Deserialize(context) as EntityNavigationCollection <ObjectIdIdModel>;
            }

            Assert.AreEqual(2, initialCollection.GetForeignIds().Count());
            Assert.AreEqual(2, deserializedCollection.GetForeignIds().Count());
            Assert.IsTrue(initialCollection.GetForeignIds().All(id => deserializedCollection.GetForeignIds().Contains(id)));
        }
示例#13
0
        public static BsonArray SerializeValues(IBsonSerializer itemSerializer, IEnumerable values)
        {
            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("_v");
                writer.WriteStartArray();
                var context = BsonSerializationContext.CreateRoot(writer);
                foreach (var value in values)
                {
                    itemSerializer.Serialize(context, value);
                }
                writer.WriteEndArray();
                writer.WriteEndDocument();
            }
            return(document["_v"].AsBsonArray);
        }
示例#14
0
        public void SerializeICollectionCompatibleButIsntEntityNavigationCollection()
        {
            var serializer = new EntityNavigationCollectionSerializer <ObjectIdIdModel>("Id");

            var initialCollection = new List <ObjectIdIdModel>
            {
                new ObjectIdIdModel
                {
                    Id          = ObjectId.GenerateNewId(),
                    Description = "1"
                }
            };
            EntityNavigationCollection <ObjectIdIdModel> deserializedCollection = null;

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }

            using (var reader = new BsonDocumentReader(document))
            {
                reader.ReadBsonType();
                reader.ReadStartDocument();
                reader.ReadBsonType();
                reader.SkipName();

                var context = BsonDeserializationContext.CreateRoot(reader);
                deserializedCollection = serializer.Deserialize(context) as EntityNavigationCollection <ObjectIdIdModel>;
            }

            Assert.AreEqual(1, initialCollection.Count());
            Assert.AreEqual(1, deserializedCollection.GetForeignIds().Count());
            Assert.IsTrue(initialCollection.Select(e => e.Id).All(id => deserializedCollection.GetForeignIds().Contains(id)));
        }
示例#15
0
        public static BsonArray SerializeValues(this ISerializationExpression field, IEnumerable values)
        {
            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("values");
                bsonWriter.WriteStartArray();
                foreach (var value in values)
                {
                    field.Serializer.Serialize(context, ConvertIfNecessary(field.Serializer.ValueType, value));
                }
                bsonWriter.WriteEndArray();
                bsonWriter.WriteEndDocument();

                return((BsonArray)tempDocument[0]);
            }
        }
        /// <summary>
        /// Serializes the values.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns>The serialized values.</returns>
        public BsonArray SerializeValues(IEnumerable values)
        {
            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot <BsonDocument>(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("values");
                bsonWriter.WriteStartArray();
                foreach (var value in values)
                {
                    context.SerializeWithChildContext(_serializer, value);
                }
                bsonWriter.WriteEndArray();
                bsonWriter.WriteEndDocument();

                return(tempDocument[0].AsBsonArray);
            }
        }
示例#17
0
        public void DeleteResource(object targetResource)
        {
            var resourceType = GetResourceType(targetResource);
            var collection   = GetCollection(resourceType);
            var annotation   = (TypedResourceTypeAnnotation)resourceType.CustomState;

            var idValue      = annotation.ClassMap.IdMemberMap.Getter(targetResource);
            var idSerializer = annotation.ClassMap.IdMemberMap.GetSerializer();
            var doc          = new BsonDocument();

            using (var writer = new BsonDocumentWriter(doc))
            {
                writer.WriteStartDocument();
                writer.WriteName("_id");
                idSerializer.Serialize(BsonSerializationContext.CreateRoot(writer), idValue);
                writer.WriteEndDocument();
            }

            _rememberedInstances.Add(targetResource);
            _actions.Add(() => collection.Remove(Query.EQ("_id", doc[0])));
        }
        private string SerializeKeyString(object key)
        {
            var keyDocument = new BsonDocument();

            using (var keyWriter = new BsonDocumentWriter(keyDocument))
            {
                var context = BsonSerializationContext.CreateRoot(keyWriter);
                keyWriter.WriteStartDocument();
                keyWriter.WriteName("k");
                _keySerializer.Serialize(context, key);
                keyWriter.WriteEndDocument();
            }

            var keyValue = keyDocument["k"];

            if (keyValue.BsonType != BsonType.String)
            {
                throw new BsonSerializationException("When using DictionaryRepresentation.Document key values must serialize as strings.");
            }

            return((string)keyValue);
        }
示例#19
0
        public void SerializeUnsupportedType()
        {
            var serializer = new EntityNavigationCollectionSerializer <ObjectIdIdModel>("Id");

            var initialCollection = new List <DateTime>
            {
                DateTime.Now
            };

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }
        }
示例#20
0
        public void SerializeUnsupportedType()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(ObjectIdIdModel)).GetIdProperty();
            var serializer      = new EntityNavigationCollectionSerializer <ObjectIdIdModel>(foreignProperty);

            var initialCollection = new List <DateTime>
            {
                DateTime.Now
            };

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }
        }
示例#21
0
        public static BsonDocument Concat(BsonDocument doc1, BsonDocument doc2)
        {
            BsonDocument       dest   = new BsonDocument();
            BsonDocumentWriter writer = new BsonDocumentWriter(dest);
            var context = BsonSerializationContext.CreateRoot(writer);

            writer.WriteStartDocument();

            foreach (var field in doc1)
            {
                writer.WriteName(field.Name);
                BsonValueSerializer.Instance.Serialize(context, field.Value);
            }

            foreach (var field in doc2)
            {
                writer.WriteName(field.Name);
                BsonValueSerializer.Instance.Serialize(context, field.Value);
            }

            writer.WriteEndDocument();
            return(writer.Document);
        }