problem with json when using interfaces

Feb 5, 2009 at 7:16 PM
Hi to all. I have this problem

"Could not find a public constructor for type ConsoleApplication1.Program+ICo."

i put below the example of code that crashing


static void Main(string[] args)
        {
            Test testClass = new Test();
            testClass.co = new Co();
            String strFromTest = Newtonsoft.Json.JavaScriptConvert.SerializeObject(testClass);
            Test testFromDe = (Test)Newtonsoft.Json.JavaScriptConvert.DeserializeObject(strFromTest, typeof(Test));


        }

        public class Co : ICo
        {
            public String Name { get; set; }
        }

        public interface ICo
        {
            String Name { get; set; }
        }

        public interface ITest
        {
            ICo co { get; set; }
        }

        public class Test : ITest
        {
            public ICo co { get; set; }

            public Test()
            {
            }
        }

can anyone help me please?

Thank you
Feb 5, 2009 at 10:14 PM
also i forgot to tell that version im using is json version 3.0 (stable)
Coordinator
Feb 5, 2009 at 11:34 PM
The problem is the deserializer has no idea what class to create for the ICo interface. The error message should be better.
Feb 6, 2009 at 7:28 AM
Edited Feb 6, 2009 at 7:30 AM
this mean that i cant use it with this sample of code right? have to take out the using of interface with the way like example?

Or can i tell to the json to do serialize and deserialize only in specific objects of the example? So with this way i keep the interfaces but i don't serialize or deserialize them.?
Coordinator
Feb 8, 2009 at 8:59 AM
I've improved the error message in the latest version of the source.

You could put a JsonIgnore attribute on the "public ICo co { get; set; }" property to ignore it.