J'utilise des bibliothèques Newtonsoft pour la sérialisation et la désérialisation de l'objet json. Le code de référence est ci-dessous:Valeur de chaîne de désérialisation JSON à la liste <T> en utilisant Newtonsoft - Constructeur arg est null en C#
public abstract class BaseNode
{
[JsonConstructor]
protected BaseNode(string [] specifications)
{
if (specifications == null)
{
throw new ArgumentNullException();
}
if (specifications.Length == 0)
{
throw new ArgumentException();
}
Name = specifications[0];
Identifier = specifications[1];
//Owner = specifications[2];
}
public string Name{ get; protected set; }
public string Identifier { get; protected set; }
public string Owner { get; protected set; }
}
public class ComputerNode: BaseNode
{
[JsonConstructor]
public ComputerNode(string[] specifications):base(specifications)
{
Owner = specifications[2];
}
}
sérialisation fonctionne très bien et je peux enregistrer les données au format JSON dans un fichier. Je stocke une liste d'objets ComputerNode dans un fichier. code suivant pour le fichier opération de lecture/écriture:
public class Operation<T>
{
public string path;
public Operation()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "nodes.txt");
if (File.Exists(path) == false)
{
using (File.Create(path))
{
}
}
this.path = path;
}
public void Write(string path, List<T> nodes)
{
var ser = JsonConvert.SerializeObject(nodes);
File.WriteAllText(path, ser);
}
public List<T> Read(string path)
{
var text = File.ReadAllText(path);
var res = JsonConvert.DeserializeObject<List<T>>(text);
return res;
}
}
Le fichier résultat escompté Lire devrait être une liste d'objets ComputerNode stockés dans un fichier. Cependant, lors de la désérialisation - création d'un objet de ComputerNode, le constructeur correct est appelé mais l'argument (string [] specifications) est null.
Existe-t-il une meilleure façon de travailler avec.
S'il vous plaît ne pas suggérer de changer BaseNode.cs ou ComputerNode.cs
Merci pour la suggestion ...
réponse correcte
outrepassée avec nouveau constructeur JSON. Dans BaseNode.cs
[JsonConstructor]
public BaseNode(string Owner, string Name, string Identifier)
{
this.Name = Name;
this.Identifier = Identifier;
}
et un constructeur OPPOSER JSON - ComputerNode.cs
[JsonConstructor]
public ComputerNode(string Owner, string Name, string Identifier):base(Owner, Name, Identifier)
{
this.Owner = Owner;
}
À quoi ressemble votre JSON? Je ne vois pas pourquoi vous auriez besoin de ceux-ci passés dans le constructeur lors de la désérialisation d'un objet JSON de structure de puits. Il devrait juste peupler les propriétés comme vous vous y attendiez. – Skintkingle
[ { "Nom": "jack", "Identifier": "123345", "Propriétaire": "CiiW" }, { "Nom": "didi", "Identifier": " 1dd123345 ", " Propriétaire ":" C12W " } ] – JayeshThamke
Désolé, il n'a pas ajouté d'indentation dans l'éditeur SO. – JayeshThamke