Serialize to JSON with C #

0

I'm trying to serialize an object that carries internally other objects with different properties, but when creating the file it only shows me the main attributes nombre y ubicacion , instead it does not appear dispositivo , which is in a List<Dispositivo> with the rest of information that is inside this ... Looking inside the object I see that dispositivos has a lock. Do I have any problem accessing these? I have tried to set the Device and Type classes as Public but the problem still has the same ...

The class Estacion is the following, the list is private because I do not want it to be modified from the outside, unless its methods are used ... Can I do something to keep it private but through the JSON? see its properties?

    namespace Proyecto
    {
       class Estacion
       {
           private string _nombre, _ubicacion;
           private List<Dispositivo> dispositivos = new List<Dispositivo>();

           public string nombre
           {
               get { return _nombre; }
               set { _nombre = value; }
           }

           public string ubicacion
           {
               get { return _ubicacion; }
               set { _ubicacion = value; }
           }

           public List<Dispositivo> getDispositivos()
           {
               return this.dispositivos;
           }

           public void añadirDispositivo(Dispositivo dispositivo)
           {
               this.dispositivos.Add(dispositivo);
           }

           public void eliminarDispositivo(Dispositivo dispositivo)
           {
               this.dispositivos.Remove(dispositivo);
           }
       }
  }
    
asked by Edulon 26.06.2018 в 18:56
source

2 answers

4

Serialization only applies to properties, not to methods, or private variables

If you want devices to be serialized you should add the public property

class Estacion
{
    private string _nombre, _ubicacion;

    public List<Dispositivo> dispositivos { get; set; }

    public Estacion()
    {
        dispositivos = new List<Dispositivo>();
    }

    //resto

}

In addition, initialization should be done in the class constructor

    
answered by 26.06.2018 / 19:26
source
0

adding to the previous answer:

If you do not make a public property of your list then you will not be able to do anything

public List<Dispositivo> PublicDispositivos    
{
    get{return Dispositivos;}
    set{Publicdispositivos = value;}
}
    
answered by 27.06.2018 в 20:53