Ignore DataMember depending on a value

2

I have a service developed with WCF , in this service I call a stored procedure , it returns 4 fields: id , id_visita , fecha , observaciones . It may happen that the field id_visita comes null and this is because the corresponding visit has not been made, therefore the value fecha is one day before today and the field observaciones is also null.

In my service I do the following:

int gestion_ruta_visita_id = Convert.ToInt32(dr["id_visita"]);
if (gestion_ruta_visita_id != 0)
{
   //Si es que ya fue visitada, obtener detalles de la visita
   gestionRuta.estado = 1;
   gestionRuta.gestion_ruta_visita_fecha = Convert.ToDateTime(dr["fecha"]).ToString("dd-MM-yyyy HH:mm");
   gestionRuta.gestion_ruta_visita_observaciones = Convert.ToString(dr["observaciones"]);
}

In this code I get whether or not it has the id_visita , if it is different from 0, it means that the visit has already taken place, therefore my object gestionRuta has the following:

[DataMember]
public string gestion_ruta_visita_fecha { get; set; }
[DataMember]
public string gestion_ruta_visita_observaciones { get; set; }

These fields are ONLY filled when the id_visita is different from 0, but in the other case, that is 0, I do not want that in my json they appear as null, simply that they do not appear.

Current result:

"rutas": [
  {
    "estado": 1,
    "gestion_ruta_codigo_sala": "3200007286",
    "gestion_ruta_direccion_sala": "La Florida",
    "gestion_ruta_latitud_sala": "-33.4141632",
    "gestion_ruta_longitud_sala": "-70.627686",
    "gestion_ruta_nombre_sala": "Jumbo La Florida",
    "gestion_ruta_programacion_fecha": "25-05-2015",
    "gestion_ruta_programacion_id": 391,
    "gestion_ruta_visita_fecha": "02-06-2016 16:11",
    "gestion_ruta_visita_observaciones": "Sin observaciones"
  },
  {
    "estado": 2,
    "gestion_ruta_codigo_sala": "3200007286",
    "gestion_ruta_direccion_sala": "La Florida",
    "gestion_ruta_latitud_sala": "-33.4141632",
    "gestion_ruta_longitud_sala": "-70.627686",
    "gestion_ruta_nombre_sala": "Jumbo La Florida",
    "gestion_ruta_programacion_fecha": "27-05-2015",
    "gestion_ruta_programacion_id": 392,
    "gestion_ruta_visita_fecha": null,
    "gestion_ruta_visita_observaciones": null
  },

Expected result:

"rutas": [
  {
    "estado": 1,
    "gestion_ruta_codigo_sala": "3200007286",
    "gestion_ruta_direccion_sala": "La Florida",
    "gestion_ruta_latitud_sala": "-33.4141632",
    "gestion_ruta_longitud_sala": "-70.627686",
    "gestion_ruta_nombre_sala": "Jumbo La Florida",
    "gestion_ruta_programacion_fecha": "25-05-2015",
    "gestion_ruta_programacion_id": 391,
    "gestion_ruta_visita_fecha": "02-06-2016 16:11",
    "gestion_ruta_visita_observaciones": "Sin observaciones"
  },
  {
    "estado": 2,
    "gestion_ruta_codigo_sala": "3200007286",
    "gestion_ruta_direccion_sala": "La Florida",
    "gestion_ruta_latitud_sala": "-33.4141632",
    "gestion_ruta_longitud_sala": "-70.627686",
    "gestion_ruta_nombre_sala": "Jumbo La Florida",
    "gestion_ruta_programacion_fecha": "27-05-2015",
    "gestion_ruta_programacion_id": 392
  },
    
asked by sioesi 15.11.2016 в 12:55
source

1 answer

1

I have found a solution

[DataContract]
public class GestionRuta

Define as DataContract my class and the elements I do not want to return when they are null as [DataMember(EmitDefaultValue = false)]

[DataMember(EmitDefaultValue = false)]
public string gestion_ruta_visita_fecha { get; set; }
[DataMember(EmitDefaultValue = false)]
public string gestion_ruta_visita_observaciones { get; set; }

If there is another better solution I hope it: D

    
answered by 15.11.2016 / 13:40
source