This project is read-only.

Deserialization of simple class fails with "cannot determine type"

Mar 5, 2013 at 8:53 PM
I have a wrapper class that I'm using on both the client and server end of an application, using fastJSON to serialize my custom message classes for transmission.

On the server side, I'm using a simple function
    public static string SerializeSimpleMsgToString(CSimpleMsg cMsg)
    {
        return fastJSON.JSON.Instance.ToJSON(cMsg);
    }
The resulting serialized data looks fine, however when I try to deserialize this on the client side (has access to the same CSimpleMsg class), using another simple function...
    public static CSimpleMsg DeserializeSimpleMsg(string szData)
    {
        return (CSimpleMsg)fastJSON.JSON.Instance.ToObject(szData);
    }
I get a "cannot determine type" error. I have tried some variations of this call, such as
return (CSimpleMsg)fastJSON.JSON.Instance.ToObject<CSimpleMsg>(szData);
return (CSimpleMsg)fastJSON.JSON.Instance.ToObject(szData, typeof(CMsgData));
return (CSimpleMsg)fastJSON.JSON.Instance.Parse(szData);
    etc...
I've also tried (as suggested on another thread) playing around with the jsonparameters, specifically setting UsingGlobalTypes and UseExtensions to false. This changed the error to another point in the JSON code (different exception), but didn't resolve the issue.

Is there something simple I'm missing here??
Mar 5, 2013 at 9:02 PM
Edited Mar 5, 2013 at 9:25 PM
Ok, figured it out. It NEVER fails, does it? You can search and search and search, and not find a solution, but as soon as you ask for help in a public forum, BOOM! You find it.

Apparently the serialized object must have a default constructor, which my CSimpleMsg did not. Adding an empty constructor solved the issue.

Edit:

Apparently, I had to make several of the above mentioned changes for everything to work smoothly. In addition to the default constructor, this form of the call:
(CSimpleMsg)fastJSON.JSON.Instance.ToObject<CSimpleMsg>(szData)
Still has to be used. Also, both the configuration settings (UsingGlobalTypes and UseExtensions) did have to be set to false, changing the serialization function to:
    public static string SerializeSimpleMsgToString(CSimpleMsg cMsg)
    {
        fastJSON.JSONParameters paramTest = new fastJSON.JSONParameters();
        paramTest.UsingGlobalTypes = false;
        paramTest.UseExtensions = false;

        return fastJSON.JSON.Instance.ToJSON(cMsg, paramTest);
    }
Jul 15, 2015 at 7:53 PM
Hi,

Please find the below code, I am still not able to deserialize my json string back to customobject

public class Company
    {
        public string Title { get; set; }
        public List<Program.Employee> Employees { get; set; } 
        public Company()
        {}
    }

    public class Employee
    {
        public Employee()
        {}
        public string Name { get; set; }
        public EmployeeType Employeetype { get; set; }
    }

    public enum EmployeeType
    {
        CEO,
        Developer
    }
public static Company GetData()
    {
        return new Company()
        {
            Title = "Tesco HSC",
            Employees = new List<Employee>()
            {
                new Employee() {Name = "Mark CEO", Employeetype = EmployeeType.CEO},
                new Employee() {Name = "Praveen", Employeetype = EmployeeType.Developer},
                new Employee() {Name = "Yash", Employeetype = EmployeeType.Developer}
            }
        };
    }
Company company = new Company();
        company = GetData();

        JSONParameters paramTest = new JSONParameters();
        paramTest.UsingGlobalTypes = false;
        paramTest.UseExtensions = false;
        string json = JSON.ToJSON(company,paramTest);

        var a = JSON.ToObject<Company>(json);
Failed to fast create instance for type 'Serialization.Program+Company' from assembly 'Serialization.Program+Company, Serialization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'