3

I am using Json.Net for serialization and unserialization on a class. When I try to obsfucate the class in an assembly, I will not be able to unserialize the JSON string correctly. I have try using JsonProperty Attribute but it still not able to unserialize correctly.

Does anyone know how to get around this other than setting the class not to obsfucate?

Thanks in advance.

Updates:

I have created a simple class for JSON object as below:

public class JsonTestClass
{
    [JsonProperty("JsonID")]
    public int Id { get; set; }

    [JsonProperty("JsonName")]
    public string Name { get; set; }

    [JsonProperty("JsonYesNo")]
    public bool YesNo { get; set; }
}

And the code to serialize JsonTestClass objects into Json string is:

 //Serialize
 var jsonObj = new JsonTestClass()
 {
     Id = 1,
     Name = "John",
     YesNo = true,
 };

 var JsonStr = JsonConvert.SerializeObject(jsonObj);
 Trace.WriteLine(">>" + JsonStr);

And unserialize Json string to JsonTestClass is as follow:

//Unserialize
var JsonStr = "{\"JsonID\":1,\"JsonName\":\"John Abc\",\"JsonYesNo\":true}";
Trace.WriteLine("<<" + JsonStr);

var jsonObj = JsonConvert.DeserializeObject<JsonTestClass>(JsonStr);
if (jsonObj == null)
{
    Trace.WriteLine("-- JsonObj is null");
}
else
{
    Trace.WriteLine(string.Format("-- Id={0} Name={1} YesNo={2}", jsonObj.Id, jsonObj.Name, jsonObj.YesNo));
}

Everything works fine unobsfucated and the debug output for serialization is:

[64200] >>{"JsonID":1,"JsonName":"John Abc","JsonYesNo":true} 

And debug output for deserialization is:

[64200] <<{"JsonID":1,"JsonName":"John Abc","JsonYesNo":true} 
[64200] -- Id=1 Name=John Abc YesNo=True 

After the assembly has been obsfucated (using SmartAssembly to obsfucate this) and using dotPeek to see the obsfucated assembly, the only one which somewhat resemble the JsonTestClass are:

namespace
{
   internal class
   {
   }
}

The debug output for serialization is:

[65956] >>{} 

The debug output for deserialization is:

[65956] <<{"JsonID":1,"JsonName":"John Abc","JsonYesNo":true} 
[65956] -- Id=0 Name= YesNo=False 

Hope this helps to clear things up.

danielnixon
  • 3,989
  • 1
  • 23
  • 37
Darren
  • 41
  • 1
  • 3

1 Answers1

1

If you are using Dotfuscator which is shipped as Community Edition with Visual Studio 15 and Visual Studio 17, you have to exclude the property names of the object you serialize from renaming. You do this in the Dotfuscator renaming tab.

After that you can serialize and deserialize like normal.

You find a detailed description here in the support corner of the Preemtive Solutions web site.

palota
  • 333
  • 2
  • 7