Exemplo n.º 1
0
        static void Main(string[] args)
        {
            AFoo a = new AFoo();

            a.FooBarBuzz = "A";
            a.A          = "Hello World";

            BFoo b = new BFoo();

            b.FooBarBuzz = "B";
            b.B          = "Hello World";

            List <BaseFoo> allFoos = new List <BaseFoo>();

            allFoos.Add(a);
            allFoos.Add(b);

            var result = JsonConvert.SerializeObject(allFoos);

            // issue here:
            //Additional information: Could not create an instance of type ConsoleApplication6.BaseFoo. Type is an interface or abstract class and cannot be instantiated. Path '[0].A', line 1, position 6.
            //var test = JsonConvert.DeserializeObject<List<BaseFoo>>(result);
            JsonConverter[] converters = { new FooConverter() };

            var test = JsonConvert.DeserializeObject <List <BaseFoo> >(result, new JsonSerializerSettings()
            {
                Converters = converters
            });
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            AFoo a = new AFoo();
            a.FooBarBuzz = "A";
            a.A = "Hello World";

            BFoo b = new BFoo();
            b.FooBarBuzz = "B";
            b.B = "Hello World";

            List<BaseFoo> allFoos = new List<BaseFoo>();
            allFoos.Add(a);
            allFoos.Add(b);

            var result = JsonConvert.SerializeObject(allFoos);

            // issue here:
            //Additional information: Could not create an instance of type ConsoleApplication6.BaseFoo. Type is an interface or abstract class and cannot be instantiated. Path '[0].A', line 1, position 6.
            //var test = JsonConvert.DeserializeObject<List<BaseFoo>>(result);
            JsonConverter[] converters = { new FooConverter()};

            var test = JsonConvert.DeserializeObject<List<BaseFoo>>(result, new JsonSerializerSettings() { Converters = converters });

        }