示例#1
0
        private ICollection <object> ParsePayload(TubularGridDataResponse response)
        {
            var result = new List <object>();
            var props  = DataItemType.PropertyProxies().Values.Where(t => t.IsFlatType);

            foreach (var itemData in response.Payload)
            {
                var valueIndex = 0;
                var targetItem = Activator.CreateInstance(DataItemType);

                foreach (var targetProperty in props)
                {
                    var v            = itemData[valueIndex++];
                    var nullableType = Nullable.GetUnderlyingType(targetProperty.PropertyType);
                    var isNullable   = nullableType != null;

                    try
                    {
                        if (v is JsonElement element)
                        {
                            v = ParseValue(element, targetProperty.PropertyType);
                        }
                        else if (v != null && v.GetType() != (isNullable ? nullableType : targetProperty.PropertyType))
                        {
                            v = Convert.ChangeType(v?.ToString(), isNullable ? nullableType : targetProperty.PropertyType);
                        }

                        targetProperty.SetValue(targetItem, v);
                    }
                    catch
                    {
                        // ignore
                    }
                }

                result.Add(targetItem);
            }

            return(result);
        }
示例#2
0
        private object ParseAggregateItem(TubularGridDataResponse response)
        {
            var payload = response.AggregationPayload;

            if (payload == null || payload.Count <= 0)
            {
                return(null);
            }

            var result  = Activator.CreateInstance(DataItemType);
            var proxies = DataItemType.PropertyProxies();

            foreach (var kvp in payload)
            {
                if (kvp.Value is not JsonElement value)
                {
                    continue;
                }

                var property = proxies.ContainsKey(kvp.Key) ? proxies[kvp.Key] : null;
                if (property == null)
                {
                    continue;
                }

                try
                {
                    property.SetValue(result, ParseValue(value, property.PropertyType));
                }
                catch
                {
                    // ignore
                }
            }

            return(result);
        }