Esempio n. 1
0
    protected object ConvertCtor(object o, JSONStructAttribute ji)
    {
        D("############ creating new object ");
        if (o == null)
        {
            throw new NotSupportedException("passing null as object?");
        }
        if (ji == null)
        {
            throw new NotSupportedException("You forgot an attribute!");
        }
        D("############ creating new object " + ji.Target.FullName);
        ConstructorInfo ctor = ji.Target.GetConstructor(new Type[] { typeof(Hashtable) });

        Hashtable ht = (Hashtable)o;

        D(ht.Count.ToString());

        if (ctor != null)
        {
            return(ctor.Invoke(new object[] { o }));
        }
        else
        {
            throw new NotSupportedException("ctor ");
        }
    }
Esempio n. 2
0
    protected object Convert(object o, JSONStructAttribute ji)
    {
        D("############ converting... " + o);

        Type t = o.GetType();

        if (t == typeof(double))
        {
            return(o);
        }
        if (t == typeof(string))
        {
            return(o);
        }
        if (t == typeof(bool))
        {
            return(o);
        }
        if (t == typeof(Hashtable))
        {
            return(ConvertCtor(o, ji));
        }

        throw new NotSupportedException("convert:" + o);
    }
Esempio n. 3
0
    protected void Parser(Hashtable ht)
    {
        Dictionary <string, PropertyInfo>        pia = new Dictionary <string, PropertyInfo> ();
        Dictionary <string, JSONStructAttribute> jsa = new Dictionary <string, JSONStructAttribute> ();

        string c = this.GetType().FullName;

        if (ht == null)
        {
            D(c + " ... passed null. abort.");
            return;
        }

        D("Parser: ht size:" + ht.Count);


        D("scanning properties... " + c + " / " + this.GetType().GetProperties().Length);

        foreach (PropertyInfo pi in this.GetType().GetProperties())
        {
            string name = pi.Name;
            D("####property: " + name);
            foreach (JSONStructAttribute sa in pi.GetCustomAttributes(typeof(JSONStructAttribute), true))
            {
                D("########struct attribute: " + sa.ToString());
                name = sa.Name;
                jsa.Add(name, sa);
            }

            pia.Add(name, pi);
        }

        D("scanning hashtable...");
        foreach (string key in ht.Keys)
        {
            D("####key:" + key);
            if (pia.ContainsKey(key))
            {
                D("########key found" + key);
                PropertyInfo        pi = pia[key];
                MethodInfo          mi = pi.GetSetMethod();
                JSONStructAttribute ji = null;
                if (jsa.ContainsKey(key))
                {
                    ji = jsa[key];
                }

                Type t = ht[key].GetType();
                if (t == typeof(Hashtable))
                {
                    D("############ struct");
                    D("############ struct count: " + ((Hashtable)ht[key]).Count);
                    //hashtable = new class, convention adds to property
                    mi.Invoke(this, new object[] { ConvertCtor(ht[key], ji) });
                }
                else if (t == typeof(ArrayList))
                {
                    D("############ array ");
                    D("############ array count " + ((ArrayList)ht[key]).Count);
                    mi = pi.GetGetMethod();
                    object     target = mi.Invoke(this, null);
                    MethodInfo mAdd   = pi.GetGetMethod().ReturnType.GetMethod("Add");
                    D("KEY:" + key);
                    D("PI::" + pi.Name);
                    D("PIGT:" + pi.GetGetMethod().ReturnType.Name);
                    D("TGT::" + target.ToString());
                    if (mAdd == null)
                    {
                        throw new NotSupportedException("add method, not found.");
                    }
                    int index = 0;
                    foreach (object oo in (ArrayList)ht[key])
                    {
                        object x = Convert(oo, ji);
                        D("applying " + key + " index#" + index);
                        D("adding " + x);
                        D("to ... " + mAdd.ToString());
                        mAdd.Invoke(target, new object[] { x });
                        index++;
                    }
                }
                else
                {
                    D("############ " + t.FullName);
                    mi.Invoke(this, new object[] { Convert(ht[key], ji) });
                }
            }
        }
    }