Example #1
0
 public static void TestSuccess <TDeserialized>(
     string input,
     TDeserialized expected,
     System.Text.Json.Serialization.JsonConverter systemTextJsonSerializer,
     Newtonsoft.Json.JsonConverter newtonsoftJsonConverter)
 {
     TestSuccess <TDeserialized>(input, expected, expected, systemTextJsonSerializer, newtonsoftJsonConverter);
 }
        internal static void AssertConversions <T>(T value, string expectedJson, JsonConverter converter)
        {
            var options = new JsonSerializerOptions
            {
                Converters = { converter },
            };

            AssertConversions(value, expectedJson, options);
        }
Example #3
0
        public static void TestSuccess <TDeserialized>(
            string input,
            TDeserialized systemTextJsonExpected, TDeserialized newtonsoftJsonExpected,
            System.Text.Json.Serialization.JsonConverter systemTextJsonSerializer, Newtonsoft.Json.JsonConverter newtonsoftJsonConverter)
        {
            var deserialized = DeserializeSystemTextJson <TDeserialized>(input, systemTextJsonSerializer);

            Console.WriteLine("Checking System.Text.Json deserialized value");
            Assert.AreEqual(systemTextJsonExpected, deserialized);

            var deserializedNewtonsoft = DeserializeNewtonsoftJson <TDeserialized>(input, newtonsoftJsonConverter);

            Console.WriteLine("Checking Newtonsoft.Json deserialized value");
            Assert.AreEqual(newtonsoftJsonExpected, deserializedNewtonsoft);
        }
Example #4
0
 private void AddConverters(ref System.Text.Json.JsonSerializerOptions settings)
 {
     if (Converters != null && Converters.Any())
     {
         foreach (var converter in Converters.Where(_ => _ != null))
         {
             var assembly = AppDomain.CurrentDomain.GetAssemblies().AsEnumerable().Where(_ => _.FullName?.Split(',')[0] == converter.Assembly).FirstOrDefault();
             if (null != assembly)
             {
                 try
                 {
                     Type converterType = System.Reflection.Assembly.LoadFrom(assembly.Location).GetType(converter.Type);
                     if (typeof(System.Text.Json.Serialization.JsonConverter).IsAssignableFrom(converterType))
                     {
                         System.Text.Json.Serialization.JsonConverter obj = null;
                         try
                         {
                             // try parms object[] parameters
                             obj = (System.Text.Json.Serialization.JsonConverter)Activator.CreateInstance(
                                 converterType,
                                 new object[] {
                                 new Microsoft.AspNetCore.Http.HttpContextAccessor()
                             });
                         }
                         catch
                         {
                             // Try parameterless ctor
                             obj = (System.Text.Json.Serialization.JsonConverter)Activator.CreateInstance(converterType);
                         }
                         if (obj != null)
                         {
                             settings.Converters.Add(obj);
                         }
                     }
                 }
                 catch { }
             }
         }
     }
 }
Example #5
0
 public DelegatedConverter(System.Text.Json.Serialization.JsonConverter <TDelegated> converter)
 {
     this.converter = converter;
     this.options.Converters.Add(converter);
 }