Пример #1
0
        public void SerializeObject_without_type_serializes_to_json_using_DefaultSerializerSettings()
        {
            // If Default is being used then there should be new lines
            var dog = new Dog(5, "spud", FurColor.Brindle);

            var json = DefaultJsonSerializer.SerializeObject(dog);

            Assert.That(json, Is.EqualTo("{\r\n  \"name\": \"spud\",\r\n  \"furColor\": \"brindle\",\r\n  \"dogTag\": \"my name is spud\",\r\n  \"nickname\": null,\r\n  \"age\": 5\r\n}"));
        }
        public void IExcludedInterfaceSerializer_RegistersSerializeAsToString_InvokesToString()
        {
            var testObject = BuildSampleObject();

            var sb      = new StringBuilder();
            var options = new JsonSerializeOptions();

            var logFactory = new LogFactory();

            logFactory.Setup().SetupSerialization(s => s.RegisterObjectTransformation <IExcludedInterface>(o => o.ToString()));

            var jsonSerializer = new DefaultJsonSerializer(logFactory.ServiceRepository);

            jsonSerializer.SerializeObject(testObject, sb, options);
            const string expectedValue =
                @"{""S"":""sample"", ""Excluded"":""Skipped"", ""Included"":{""IncludedString"":""serialized""}}";

            Assert.Equal(expectedValue, sb.ToString());
        }
        public void IExcludedInterfaceSerializer_RegistersSerializeAsToString_InvokesToString()
        {
            try
            {
                var testObject = BuildSampleObject();

                var sb      = new StringBuilder();
                var options = new JsonSerializeOptions();

                LogManager.Setup().SetupSerialization(s => s.RegisterObjectTransformation <IExcludedInterface>(o => o.ToString()));

                var jsonSerializer = new DefaultJsonSerializer();
                jsonSerializer.SerializeObject(testObject, sb, options);
                const string expectedValue =
                    @"{""S"":""sample"", ""Excluded"":""Skipped"", ""Included"":{""IncludedString"":""serialized""}}";
                Assert.Equal(expectedValue, sb.ToString());
            }
            finally
            {
                NLog.Config.ConfigurationItemFactory.Default.ObjectTypeTransformer = null;
            }
        }
Пример #4
0
 protected override string SerializeObject(object o)
 {
     return(_serializer.SerializeObject(o));
 }