Exemplo n.º 1
0
 private static ConektaObject aux(JArray jsonArray, String className, int i)
 {
     try
     {
         String  key;
         JObject jsonObject;
         key        = className ?? jsonArray[i]["object"].ToString();
         jsonObject = jsonArray[i].ToObject <JObject>();
         var conektaObject = ConektaObjectFromJSONFactory.ConektaObjectFactory(jsonObject, key);
         conektaObject.LoadFromObject(jsonObject);
         return(conektaObject);
     }
     catch (Exception ex)
     {
         throw new Error(ex.Message);
     }
 }
Exemplo n.º 2
0
        public virtual void LoadFromObject(JObject jsonObject)
        {
            IEnumerator itr = jsonObject.Properties().GetEnumerator();
            FieldInfo   field;

            while (itr.MoveNext())
            {
                var key = ((JProperty)itr.Current).Name;
                field = GetType().GetField(key);
                // Si el campo no existe, omitir
                if (field == null)
                {
                    continue;
                }
                Object obj = jsonObject[key];
                try
                {
                    var isConektaObject = (field.FieldType.Namespace ?? "").Equals("ConektaCSharp");

                    var objStr = obj.ToString();

                    if (!String.IsNullOrWhiteSpace(objStr))
                    {
                        switch ((int)objStr[0])
                        {
                        // {
                        case 123:
                            var o = JObject.Parse(objStr);
                            if (o["object"] != null)
                            {
                                var conektaObject = ConektaObjectFromJSONFactory.ConektaObjectFactory(o,
                                                                                                      o["object"].ToString());
                                field.SetValue(this, conektaObject);
                                SetVal(key, conektaObject);
                            }
                            else
                            {
                                if (isConektaObject)
                                {
                                    var handle = Activator.CreateInstance(null, field.FieldType.FullName);
                                    var attr   = (ConektaObject)handle.Unwrap();

                                    attr.LoadFromObject((JObject)obj);
                                    field.SetValue(this, attr);
                                    SetVal(key, attr);
                                }
                                else
                                {
                                    var tipoCampo       = Type.GetType(field.FieldType.FullName);
                                    var valorConvertido = Convert.ChangeType(obj, tipoCampo);
                                    field.SetValue(this, valorConvertido);
                                    SetVal(key, valorConvertido);
                                }
                            }

                            break;

                        // [
                        case 91:
                            var jsonArray = JArray.Parse(objStr);
                            if (jsonArray.Count > 0)
                            {
                                var conektaObject = new ConektaObject();
                                if (isConektaObject)
                                {
                                    var handle = Activator.CreateInstance(null, field.FieldType.FullName);
                                    conektaObject = (ConektaObject)handle.Unwrap();
                                }

                                foreach (var jItem in jsonArray)
                                {
                                    if (jsonArray[0]["object"] != null)
                                    {
                                        conektaObject.Add(
                                            ConektaObjectFromJSONFactory.ConektaObjectFactory(
                                                jItem.ToObject <JObject>(),
                                                jItem["object"].ToString()));
                                    }
                                    else
                                    {
                                        conektaObject.Add(
                                            ConektaObjectFromJSONFactory.ConektaObjectFactory(
                                                jItem.ToObject <JObject>(), key));
                                    }
                                }
                                field.SetValue(this, conektaObject);
                                SetVal(key, conektaObject);
                            }
                            break;

                        default:
                            if (isConektaObject)
                            {
                                var handle = Activator.CreateInstance(null, field.FieldType.FullName);
                                var attr   = (ConektaObject)handle.Unwrap();

                                attr.LoadFromObject((JObject)obj);
                                field.SetValue(this, attr);
                                SetVal(key, attr);
                            }
                            else
                            {
                                var tipoCampo       = Type.GetType(field.FieldType.FullName);
                                var valorConvertido = Convert.ChangeType(obj, tipoCampo);
                                field.SetValue(this, valorConvertido);
                                SetVal(key, valorConvertido);
                            }
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    // No field found
                    //System.out.println(e.toString());
                    throw new Error(e.Message);
                }
            }
        }