Beispiel #1
0
        public void CanSerializeOneOfValueTransparently()
        {
            //Given an object with a OneOf property
            var x = new SomeThing
            {
                Value = "A string value"
            };
            //When that object is serialized
            var json = JsonConvert.SerializeObject(x, new JsonSerializerSettings
            {
                Converters = { new OneOfJsonConverter() }
            });

            //Then the OneOfs underlying value should have been written
            Assert.AreEqual("{\"Value\":\"A string value\"}", json);
        }
Beispiel #2
0
        public void CanSerializeOneOfValueTransparently()
        {
            //Given an object with a OneOf property 
            var x = new SomeThing()
            {
                Value = "A string value"
            };
            //When that object is serialized
            var json = JsonConvert.SerializeObject(x, new JsonSerializerSettings()
            {
                Converters = {new OneOfJsonConverter()}
            });

            //Then the OneOfs underlying value should have been written
            Assert.AreEqual("{\"Value\":\"A string value\"}", json);
        }
Beispiel #3
0
        public void CanDeserializeOneOfInherited()
        {
            //Given an object with a OneOf property
            var x = new SomeThing()
            {
                Prop = "A string value"
            };
            var serializerSettings = new JsonSerializerSettings()
            {
                Converters = { new OneOfJsonConverter() }
            };

            //When that object is serialized
            var json = JsonConvert.SerializeObject(x, serializerSettings);

            var deserializedX = JsonConvert.DeserializeObject <SomeThing>(json, serializerSettings);

            Assert.AreEqual(x.Prop, deserializedX.Prop);
        }
Beispiel #4
0
        public void CanWriteAndReadOneOfFromMongoDb()
        {
            var client = new MongoClient(new MongoClientSettings {
                Server = new MongoServerAddress("localhost", 27017)
            });
            var database = client.GetDatabase("test");

            BsonSerializer.RegisterSerializationProvider(new OneOfBsonSerializationProvider());
            var mongoCollection = database.GetCollection <SomeThing>(nameof(SomeThing));

            var prop = Guid.NewGuid().ToString();

            var expected = new SomeThing {
                Prop = prop
            };

            mongoCollection.InsertOne(expected);

            var builder = new FilterDefinitionBuilder <SomeThing>();

            var actual = mongoCollection.FindSync(builder.Eq(thing => thing.Prop, prop)).Single();

            Assert.AreEqual(expected.Prop.Value, actual.Prop.Value);
        }