Exemple #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject jsonObject = JObject.Load(reader);
            IEnumerable <JProperty>        properties      = jsonObject.Properties();
            Dictionary <string, JProperty> otherProperties = new Dictionary <string, JProperty>();

            IPageResponse message = Activator.CreateInstance(objectType) as IPageResponse;

            foreach (JProperty property in properties)
            {
                if (property.Name == "draw")
                {
                    message.Draw = property.Value.ToObject <int>();
                }

                else if (property.Name == "recordsTotal")
                {
                    message.TotalRecords = property.Value.ToObject <int>();
                }

                else if (property.Name == "recordsFiltered")
                {
                    message.TotalFilteredRecords = property.Value.ToObject <int>();
                }

                else if (property.Name == "data")
                {
                    if (objectType.IsGenericType)
                    {
                        Type   genericType  = objectType.GetGenericArguments()[0].MakeArrayType();
                        object genericArray = property.Value.ToObject(genericType);
                        message.Data = (object[])genericArray;
                    }
                    else
                    {
                        message.Data = property.Value.ToObject <object[]>();
                    }
                }

                else if (property.Name == "error")
                {
                    message.Error = property.Value.ToObject <string>();
                }

                else
                {
                    otherProperties.Add(property.Name, property);
                }
            }

            JsonConvertHelper.ReadJson(message, otherProperties, serializer,
                                       prop => JsonConvertHelper.GetPropertiesFromType(typeof(IPageResponse)).Select(x => x.Name).Contains(prop.Name) ||
                                       JsonConvertHelper.GetPropertiesFromType(typeof(IPageResponse <>)).Select(x => x.Name).Contains(prop.Name));

            return(message);
        }
Exemple #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject jsonObject = JObject.Load(reader);
            IEnumerable <JProperty>        properties      = jsonObject.Properties();
            Dictionary <string, JProperty> otherProperties = new Dictionary <string, JProperty>();

            IFilterRequest message = Activator.CreateInstance(objectType) as IFilterRequest;

            foreach (JProperty property in properties)
            {
                if (property.Name == "draw")
                {
                    message.Draw = property.Value.ToObject <int>();
                }

                else if (property.Name == "start")
                {
                    message.Start = property.Value.ToObject <int>();
                }

                else if (property.Name == "length")
                {
                    message.Length = property.Value.ToObject <int>();
                }

                else if (property.Name == "search[value]")
                {
                    message.Search.Value = property.Value.ToObject <string>();
                }

                else if (property.Name == "search[regex]")
                {
                    message.Search.IsRegex = property.Value.ToObject <bool>();
                }

                else if (property.Name.StartsWith("order"))
                {
                    ReadSortConfiguration(ref message, property);
                }

                else if (property.Name.StartsWith("columns"))
                {
                    ReadColumnConfiguration(ref message, property);
                }

                else
                {
                    otherProperties.Add(property.Name, property);
                }
            }

            JsonConvertHelper.ReadJson(message, otherProperties, serializer,
                                       prop => JsonConvertHelper.GetPropertiesFromType(typeof(IFilterRequest)).Select(x => x.Name).Contains(prop.Name));

            return(message);
        }
Exemple #3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject jsonObject = JObject.Load(reader);
            IEnumerable <JProperty>        properties      = jsonObject.Properties();
            Dictionary <string, JProperty> otherProperties = new Dictionary <string, JProperty>();

            IFilterRequest message = Activator.CreateInstance(objectType) as IFilterRequest;

            foreach (JProperty property in properties)
            {
                if (property.Name == "sEcho")
                {
                    message.Draw = property.Value.ToObject <int>();
                }

                else if (property.Name == "iDisplayStart")
                {
                    message.Start = property.Value.ToObject <int>();
                }

                else if (property.Name == "iDisplayLength")
                {
                    message.Length = property.Value.ToObject <int>();
                }

                else if (property.Name == "sSearch")
                {
                    message.Search.Value = property.Value.ToObject <string>();
                }

                else if (property.Name == "bEscapeRegex")
                {
                    message.Search.IsRegex = property.Value.ToObject <bool>();
                }

                else if (property.Name == "iSortingCols")
                {
                    message.Sort.Capacity = property.Value.ToObject <int>();
                }

                else if (property.Name == "iColumns")
                {
                    continue;
                }

                else if (property.Name.StartsWith("iSortCol") ||
                         property.Name.StartsWith("sSortDir"))
                {
                    ReadSortConfiguration(ref message, property);
                }

                else if (property.Name.StartsWith("mDataProp") ||
                         property.Name.StartsWith("bSearchable") ||
                         property.Name.StartsWith("bSortable") ||
                         property.Name.StartsWith("sSearch") ||
                         property.Name.StartsWith("bRegex"))
                {
                    ReadColumnConfiguration(ref message, property);
                }

                else
                {
                    otherProperties.Add(property.Name, property);
                }
            }

            JsonConvertHelper.ReadJson(message, otherProperties, serializer,
                                       prop => JsonConvertHelper.GetPropertiesFromType(typeof(IFilterRequest)).Select(x => x.Name).Contains(prop.Name));

            return(message);
        }