How to validate the data of a json to deserealizar?

1

I have a function that receives a string, the string is a json, In which I need to validate the data of the json that send me to the function, this is my json, there is an array called eye products.

   {"comprobante_tipo":1,"comprobante_emision":"10-05-2017","comprobante_moneda":2,"documento_tipo":6,"documento_numero":"20536161199","direccion":"Av.La Tienda de Charlie","nombre":"Cesar Vallejo","productos":[{"nombre":"Caramelos2","codigo":741,"cantidad":12,"unidad":"2","precio_unidad":2000},{"nombre":"Chocolates2","codigo":231,"cantidad":12,"unidad":"UNIDADES","precio_unidad":20002}]}

I wanted to deselate the json first, and put it in a dynamic, then validate it, and if everything was "fine", I made it and sent it to a webservice. But when trying the code below I get that I must apply a pointer.

   if (x->comprobante_moneda.GetType() == typeof(int)) { } else { throw new Exception("Ingreso no Valido en Moneda"); }

Pass with this, and with the following

if (x.comprobante_tipo.GetType() == typeof(int)) { } else { throw new Exception("Ingreso no Valido en Tipo de Comprobante "); }
        if (x.comprobante_emision.GetType() == typeof(string)) { } else { throw new Exception("Ingreso no Valido en Emision"); }
        if (x.documento_tipo.GetType() == typeof(int)) { } else { throw new Exception("Ingreso no Valido en Tipo de Documento"); }
        if (x.documento_numero.GetType() == typeof(string)) { } else { throw new Exception("Ingreso no Valido en Numero de Documento"); }
        if (x.direccion.GetType() == typeof(string)) { } else { throw new Exception("Ingreso no Valido en Direccion"); }
        if (x.nombre.GetType() == typeof(string)) { } else { throw new Exception("Ingreso no Valido en Nombre"); }
        if (x.productos.codigo.GetType() == typeof(string)) { } else { throw new Exception("Ingreso no Valido en Productos"); }
        if (x.productos.cantidad.GetType() == typeof(int)) { } else { throw new Exception("Ingreso no Valido en Cantidad"); }
        if (x.productos.unidad.GetType() == typeof(int)) { } else { throw new Exception("Ingreso no Valido en Unidad"); }
        if (x.productos.precio_unidad.GetType() == typeof(int)) { } else { throw new Exception("Ingreso no Valido en Precio de unidad"); }

What could I do? or what else should I try? Thanks

    
asked by franco vargas 10.05.2017 в 19:52
source

1 answer

1

Ideally, you serialize a class, although it depends a lot on what you refer to with validate , that is, the integers are greater than zero, strings of text without special characters, etc. With this, you would be accessing and directly evaluating the properties of the class.

Class definition:

public class Datos
{
    public int comprobante_tipo { get; set; }
    public string comprobante_emision { get; set; }
    public int comprobante_moneda { get; set; }
    public int documento_tipo { get; set; }
    public string documento_numero { get; set; }
    public string direccion { get; set; }
    public string nombre { get; set; }
    public List<Productos> productos { get; set; }
}

public class Productos
{
    public string nombre { get; set; }
    public int codigo { get; set; }
    public int cantidad { get; set; }
    public string unidad { get; set; }
    public decimal precio_unidad { get; set; }
}

Receive the json and serialize it to type Datos :

Datos datos = new Datos();
datos = Newtonsoft.Json.JsonConvert.DeserializeObject<Datos>(json);

Now, you can perform all the necessary validations on the object datos . For example:

if(datos.comprobante_tipo == 0)
{
    throw new Exception("Ingreso no Valido en Tipo de Comprobante ");
}

To perform validations in the list, you have to go through using foreach :

foreach(Productos item in datos.productos) { 
    if(string.IsNullOrEmpty(item.nombre)) {
        throw new Exception("Ingreso no Valido en Nombre"); }
    }
}
    
answered by 10.05.2017 / 21:11
source