The method JsonConvert.DeserializeObjects works when the Linking is set to SDK Assemblies Only but when I build the project using Link SDK and User Assemblies option in the Linker Properties, it does not work, it returns null in all the fields of the Deserialized object.
Asked
Active
Viewed 2,155 times
1 Answers
7
You need to put the [Preserve(AllMembers = true)]
attribute on any classes that are dynamically generated. This will prevent them from being stripped out.
You can read more on the iOS linker here.
So lets say you currently have a list of Users you want to deserialize you would say something like.
var user = JsonConvert.DeserializeObject<User>(json);
In your User class you will want to place the attribute over that class.
[Preserve(AllMembers = true)]
public class User{
public string Email { get; set; }
public string FirstName { get; set; }
public string Lastname { get; set; }
}

Andres Castro
- 1,848
- 16
- 16
-
Thanks @Andres but Newtonsoft is a library, I cannot add Preserve Attribute in it. The problem is in the method DeserializeObjects of class JsonConvert which is in the Newtonsoft Library. I hope you get my point. – Akash Amin May 10 '16 at 03:56
-
@AkashAmin You don't put the Preserve attribute inside the Newtonsoft library. You put it on the Class you want to deserialize into. I added an example to my answer. – Andres Castro May 10 '16 at 12:41
-
Thank You @Andres, it solved my problem. I thought the linker would be stripping of code from the library, never thought it would strip my model also. I saw the difference fields from my model were stripped and so deserialize did not happen. Now its working. – Akash Amin May 10 '16 at 13:03
-
If it's still not working, you can also add `[Preserve(AllMembers = true)]` to the class which calls `JsonConvert.DeserializeObject`. – Felix Aug 24 '17 at 02:26