Sto provando a serializzare/deserializzare un Dictionary<string, object>
che sembra funzionare bene se l'oggetto è un tipo semplice ma non funziona quando l'oggetto è più complesso.Serializzazione/deserializzazione Dizionario di oggetti con JSON.NET
ho questa classe:
public class UrlStatus
{
public int Status { get; set; }
public string Url { get; set; }
}
nel mio dizionario aggiungo una List<UrlStatus>
con una chiave di "catena di reindirizzamento" e un paio di semplici stringhe con i tasti "Stato", "URL", "Parent Url" . La stringa che sto tornando da JSON.Net assomiglia a questo:
{"$type":"System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib","Status":"OK","Url":"http://www.ehow.com/m/how_5615409_create-pdfs-using-bean.html","Parent Url":"http://www.ehow.com/mobilearticle35.xml","Redirect Chain":[{"$type":"Demand.TestFramework.Core.Entities.UrlStatus, Demand.TestFramework.Core","Status":301,"Url":"http://www.ehow.com/how_5615409_create-pdfs-using-bean.html"}]}
Il codice che sto usando per serializzare assomigliare:
JsonConvert.SerializeObject(collection, Formatting.None, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
});
deserializzare che sto facendo:
JsonConvert.DeserializeObject<T>(collection, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
});
Il dizionario torna in ordine, tutte le stringhe tornano bene, ma l'elenco non viene deserializzato correttamente. E 'appena ritorna come
{[
{
"$type": "XYZ.TestFramework.Core.Entities.UrlStatus, XYZ.TestFramework.Core",
"Status": 301,
"Url": "/how_5615409_create-pdfs-using-bean.html"
}
]}
Certo che posso deserailize di nuovo questa stringa e ottengo l'oggetto giusto, ma sembra che JSON.Net avrebbe dovuto fare questo per me. Chiaramente sto facendo qualcosa di sbagliato, ma non so cosa sia.
sto usando Json.NET 3,5 uscita 8. Ho anche scaricato lo zip di nuovo per essere sicuri. –
Ho appena provato l'ultima build 53965, ancora non ha funzionato. –
TypeNameHandling = TypeNameHandling.All era il trucco. –