"Newtonsoft.Json.JsonSerializationException cannot find a constructor to use for types" error while deserializing class
I am developing an application in Xamarin
Android. I have a splash screen where I serialize a class and pass it MainActivity
using Intent
. When I try to deserialize it in MainActivity
, I get an error:
"SerializationException unable to find constructor to use for types"
Serialization:
void LoadData() {
Currency currency = new Currency(this);
intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("currency", Newtonsoft.Json.JsonConvert.SerializeObject(currency));
StartActivity(intent);
}
Deserialization:
cur = Newtonsoft.Json.JsonConvert.DeserializeObject<Currency> (Intent.GetStringExtra("currency")); // I get the error here
Class constructor:
public Currency(Context context) {
thiscontext = context;
}
I started experiencing this issue after adding a parameter Context context
to the constructor. What caused this? How can I solve this? Is it possible to serialize / deserialize a class with parameters?
To deserialize it, you need to have an empty constructor in the Currency class.
One more thing can cause this problem if you have full reference included in your Android project. you need to keep the class and members using
[Runtime.Preserve(AllMembers = true)]
public class Currency
{}