zrg
2025-04-10 f2ea3997112e66bf0d8d9bcf5dd34b25ecb50dd6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections;
using System.Collections.Generic;
 
namespace Top.Api.Parser
{
    /// <summary>
    /// TOP JSON响应通用读取器。
    /// </summary>
    public class TopJsonReader : ITopReader
    {
        private IDictionary json;
 
        public TopJsonReader(IDictionary json)
        {
            this.json = json;
        }
 
        public bool HasReturnField(object name)
        {
            return json.Contains(name);
        }
 
        public object GetPrimitiveObject(object name)
        {
            return json[name];
        }
 
        public object GetReferenceObject(object name, Type type, DTopConvert convert)
        {
            IDictionary dict = json[name] as IDictionary;
            if (dict != null && dict.Count > 0)
            {
                return convert(new TopJsonReader(dict), type);
            }
            else
            {
                return null;
            }
        }
 
        public IList GetListObjects(string listName, string itemName, Type type, DTopConvert convert)
        {
            IList listObjs = null;
 
            IDictionary jsonMap = json[listName] as IDictionary;
            if (jsonMap != null && jsonMap.Count > 0)
            {
                IList jsonList = jsonMap[itemName] as IList;
                if (jsonList != null && jsonList.Count > 0)
                {
                    Type listType = typeof(List<>).MakeGenericType(new Type[] { type });
                    listObjs = Activator.CreateInstance(listType) as IList;
                    foreach (object item in jsonList)
                    {
                        if (typeof(IDictionary).IsAssignableFrom(item.GetType())) // object
                        {
                            IDictionary subMap = item as IDictionary;
                            object subObj = convert(new TopJsonReader(subMap), type);
                            if (subObj != null)
                            {
                                listObjs.Add(subObj);
                            }
                        }
                        else if (typeof(IList).IsAssignableFrom(item.GetType())) // list or array
                        {
                            // TODO not support yet
                        }
                        else // string, bool, long, double, null, other
                        {
                            listObjs.Add(item);
                        }
                    }
                }
            }
 
            return listObjs;
        }
    }
}