lista ho doppiamente collegato che sto cercando di deserialise.JSON .Net non rispettando PreserveReferencesHandling sulla deserializzazione
mio scenario è strettamente legata a questo SO: Doubly Linked List to JSON
ho le seguenti impostazioni JSON:
_jsonSettings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Auto,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
ObjectCreationHandling = ObjectCreationHandling.Auto
};
Quando guardo in uscita serializzato, sembra corretto, ed i riferimenti tra i nodi sono adeguatamente rappresentato.
Quando i dati sono deserialised, le proprietà principali nella oggetti figlio sono nulli, anche se sono popolati con $ rif correttamente.
Di seguito è riportato un esempio di JSON (tagliati per migliorare la leggibilità)
Nel processo di battitura a questa domanda - forse ho visto la fonte del problema ...
gli oggetti nel "Bambini "la proprietà dell'array non ha attributi di tipo $.
Ciò può essere perché i bambini e le proprietà Parent sono di tipo generico T.
noti che il tipo effettivo viene serializzato è una classe derivata di TemplateDataLinkedListBase
public class TemplateDataQueryElement : TemplateDataLinkedListBase<TemplateDataQueryElement>
Ecco un estratto della base classe:
public class TemplateDataLinkedListBase<T> where T : TemplateDataLinkedListBase<T>
{
[JsonProperty(TypeNameHandling = TypeNameHandling.Objects)]
public T Parent { get; set; }
[JsonProperty(TypeNameHandling=TypeNameHandling.Objects)]
public List<T> Children { get; set; }
}
Come posso deserialise questo JSON in modo tale che la proprietà Parent non è nullo e contiene un riferimento a th e oggetto genitore?
{
"$id": "9",
"$type": "Contracts.Models.TemplateDataQueryElement, Contracts",
"Query": null,
"Parent": null,
"Children": [
{
"$id": "11",
"Query": null,
"Parent": {
"$ref": "9"
},
"Children": [
{
"$id": "13",
"Query": null,
"Parent": {
"$ref": "11"
},
"Children": [],
"EntityName": "Widgets",
"Fields": [
"Id"
],
"Key": ""
},
Ecco i link Pastebin al codice relativo:
http://pastebin.com/i1jxVGG3 http://pastebin.com/T1xqEWW2 http://pastebin.com/ha42SeF7 http://pastebin.com/cezwZqx6 http://pastebin.com/uFbTbUZe http://pastebin.com/sRhNQgzh
Puoi pubblicare l'intera definizione di classi per capire meglio cosa sta succedendo? –
E anche un esempio di come si sta eseguendo la serializzazione e la deserializzazione? –
Ciao @IlijaDimov Ho incluso i collegamenti al codice sorgente – RobD