Example #1
0
        public bool TryDeserialize(string obj)
        {
            bool ret = false;

            try
            {
                IVoluntario vol = new Voluntario.IoCManager.Model.ModelIoCManager(_conf).GetIVoluntarioCurrentImplementation();
                //var JObject = JsonConvert.DeserializeObject(vol);
                JObject     jObj = JObject.Parse(obj);
                List <bool> listContainsToken = new List <bool>();

                //List<string> listProps = new List<string>();
                foreach (PropertyInfo pi in vol.GetType().GetProperties())
                {
                    listContainsToken.Add(jObj.ContainsKey(pi.Name.ToLower()));
                }

                ret = (jObj.Count.Equals(listContainsToken.Count) && !listContainsToken.Contains(false));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (!ret)
            {
                string message = @"Invalid Request Body. Follow the model above
                                        {
                                          'id': 'Guid',
                                          'cpf': 'Long',
                                          'cep': 'string',
                                          'datanascimento': 'dd/MM/yyy',
                                          'email': '*****@*****.**',
                                          'fotobase64': 'string',
                                          'nome': 'Full Name',
                                          'senha': 'Senha',
                                          'telefone': 'string',
                                          'areasinteresse': [
                                            'test01',
                                            'test02'
                                          ]
                                        }";
                throw new Exception(message);
            }
            return(ret);
        }
        private void AddListVolunt()
        {
            RequestId = Guid.NewGuid().ToString();
            for (int i = 0; i < 5; i++)
            {
                Voluntario                = new Voluntario.IoCManager.Model.ModelIoCManager(base.Config).GetIVoluntarioCurrentImplementation();
                Voluntario.Cep            = "11703680";
                Voluntario.Cpf            = 31495307840;
                Voluntario.DataNascimento = "16/02/1982";
                Voluntario.Email          = $"teste{i.ToString()}@gmail.com";
                Voluntario.Id             = Guid.NewGuid().ToString();
                Voluntario.Nome           = $"Teste : {Guid.NewGuid().ToString()}";
                Voluntario.Senha          = "12345678";
                Voluntario.Telefone       = "12323123";

                AddVoluntario();
                Voluntario = null;
            }
        }
Example #3
0
        private IVoluntario DeserializeJSonVol(string vol)
        {
            IVoluntario ret = new Voluntario.IoCManager.Model.ModelIoCManager(_conf).GetIVoluntarioCurrentImplementation();
            //var JObject = JsonConvert.DeserializeObject(vol);
            JObject obj = JObject.Parse(vol);

            //var teste = obj.GetValue("areasinteresse");
            foreach (PropertyInfo pi in ret.GetType().GetProperties())
            {
                if (obj.GetValue(pi.Name.ToLower()) != null)
                {
                    //TODO - verificar como o NewtonSoft serializa byte[]
                    if (!pi.Name.ToLower().Equals("foto"))
                    {
                        if (!obj.GetValue(pi.Name.ToLower()).ToString().Contains("["))
                        {
                            pi.SetValue(ret, Convert.ChangeType(obj.GetValue(pi.Name.ToLower()).ToString(), pi.PropertyType));
                        }
                        else
                        {
                            //TODO
                            string        val     = obj.GetValue(pi.Name.ToLower()).ToString().Replace("[", string.Empty).Replace("]", string.Empty).Replace("\"", string.Empty).Replace(System.Environment.NewLine, string.Empty);
                            string[]      arrVal  = val.Split(',');
                            List <string> listStr = new List <string>();
                            for (int i = 0; i < arrVal.Length; i++)
                            {
                                listStr.Add(arrVal[i]);
                            }
                            pi.SetValue(ret, listStr);
                        }
                    }
                }
            }

            return(ret);
        }