简体   繁体   中英

“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?

您需要在Currency类中有一个空构造函数才能反序列化它。

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
{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM