Exemplo n.º 1
0
        public T Parse(string body)
        {
            T rsp = null;

            IDictionary json = JsonConvert.Import(body) as IDictionary;

            if (json != null)
            {
                IDictionary data = null;

                // 忽略根节点的名称
                foreach (object key in json.Keys)
                {
                    data = json[key] as IDictionary;
                    break;
                }

                if (data != null)
                {
                    ITopReader reader = new TopJsonReader(data);
                    rsp = (T)TopJsonConvert(reader, typeof(T));
                }
            }

            if (rsp == null)
            {
                rsp = Activator.CreateInstance <T>();
            }

            if (rsp != null)
            {
                rsp.Body = body;
            }

            return(rsp);
        }
Exemplo n.º 2
0
        public T Parse(string body, Type type)
        {
            T rsp = null;

            IDictionary json = JSON.Parse(body) as IDictionary;

            if (json != null)
            {
                IDictionary data = null;

                // 忽略根节点的名称
                foreach (object key in json.Keys)
                {
                    data = json[key] as IDictionary;
                    break;
                }

                if (data != null)
                {
                    ITopReader reader = new TopJsonReader(data);
                    rsp = (T)FromJson(reader, type);
                }
            }

            if (rsp == null)
            {
                rsp = Activator.CreateInstance(type) as T;
            }

            if (rsp != null)
            {
                rsp.Body = body;
            }

            return(rsp);
        }