Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class

I am developing an app in Xamarin Android. I have a splash screen where I serialize a class and pass it to MainActivity using Intent. When I try to deserialize it in MainActivity I get an error message :

"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 began experiencing this problem after I added the parameter Context context to the constructor. What has caused this? How can I solve it? Is it possible to serialize/deserialize class which has parameters?

like image 512
Nika Kurdadze Avatar asked Jun 26 '15 09:06

Nika Kurdadze


2 Answers

One other thing might cause this issue is if you have a full linking enabled in your android project. you need to preserve class and members using

[Runtime.Preserve(AllMembers = true)]
public class Currency
{}
like image 144
Emil Avatar answered Oct 06 '22 01:10

Emil


You need to have an empty constructor in Currency class in order to deserialize it.

like image 29
IdoT Avatar answered Oct 06 '22 00:10

IdoT