0

I have a problem where DataContractSerializer returns null for all my variable. It's like it doesn't see them or something. I'm using it to deserialize a json file into an object. I had it working for another json file that was using another class with 3 string attributes. This one is composed of 40 attributes mostly string and a few bool. I have been working on it for hours and I just can't seem to find what I'm doing wrong. I even tried it with only 1 string attribute and it still returned null. Here is a simplified version with only 1 string attribute and 1 bool attribute. Any advice is more than appreciated.

Thank you

Json : [{"Proposal_x0020_Type":"Lite Proposal","BI_x0020_Criteria_x0020_1":true}]

Function that tries to deserialize the string:

public Proposal[] Deserializer(string jsonFile)
    {

        MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonFile));

        DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Proposal[]));

        Proposal[] projectArr = (Proposal[])deserializer.ReadObject(ms);

        Console.WriteLine(jsonFile);

        Console.ReadLine();

        return projectArr;

    }

Class of the object that the deserializer should create: namespace PMIS

{

 [DataContract]

public class Proposal

{

    [DataMember(Order = 0)]

    public string Proposal_x0020_Type { get; set; }

    [DataMember(Order = 1)]

    public bool BI_x0020_Criteria_x0020_1 { get; set; }

}

}

dev2134
  • 5
  • 2
  • Using `DataContractJsonSerializer` this json can be deserialized to `Dictionary[]`. Otherwise, you can use some other serializer. For example, json.net. – Alexander Petrov Sep 25 '20 at 21:16

1 Answers1

0

I managed to find the problem by comparing the code of this Deserialization to a previous one that I had. The only difference was the name of the variables inside the Json file. The fact that they contain the Unicode for space (x00200) causes some problem inside the DataContractDeserializer. I believe that it is seeing it as space, so instead of seeing "Proposal_x0020_Type", it is seeing "Proposal_ _Type", but I'm not sure about it. Anyway, the solution was to remove x0020 from all the variables inside the Json file. After that it worked perfectly fine.

dev2134
  • 5
  • 2