コード例 #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject jObject = JObject.Load(reader);

            Object result = null;

            if (objectType == typeof(Collection))
            {
                result = new Collection();
            }
            else if (objectType == typeof(Resource))
            {

                if (jObject["type"].ToString() == "schema")
                {
                    result = new SchemaResource();
                    objectType = typeof(SchemaResource);
                }
                else
                {
                    result = new ItemResource();
                }

                foreach (JProperty property in jObject.Properties())
                {
                    if (objectType.GetProperty(property.Name) == null)
                    {
                        ((Resource)result).setProperty(property.Name, property.Value.ToString());
                    }
                }

            }

            foreach (PropertyInfo property in objectType.GetProperties())
            {
                if (jObject.Property(property.Name) != null)
                {
                    Type propertyType = property.PropertyType;
                    if (propertyType == ResourceConverter.RESOURCE_FILTER)
                    {
                        Dictionary<string, List<KeyValuePair<string, string>>> dFilters = new Dictionary<string, List<KeyValuePair<string, string>>>();
                        foreach (JProperty p in jObject.Property(property.Name).Value)
                        {
                            List<KeyValuePair<string, string>> lFilterValues = new List<KeyValuePair<string, string>>();

                            if (p.Value.HasValues)
                            {
                                foreach (JToken jToken in p.Values())
                                {
                                    lFilterValues.Add(new KeyValuePair<string, string>("value", jToken["value"].ToString()));
                                }
                            }

                            dFilters.Add(p.Name, lFilterValues);
                        }
                        property.SetValue(result, dFilters, null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_STRING_ARRAY)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToObject<String[]>(), null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_STRING)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToString(), null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_DICTIONARY)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToObject<Dictionary<string, string>>(), null);
                    }
                    else if(propertyType == ResourceConverter.RESOURCE_DICTIONARY_JTOKEN)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToObject<Dictionary<string,Dictionary<string,JToken>>>(),null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_DICTIONARY_DICTIONARY)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToObject<Dictionary<string, Dictionary<string, string>>>(), null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_LIST)
                    {

                        JArray aResources = (JArray)jObject.Property(property.Name).Value;
                        List<Resource> lResources = new List<Resource>();
                        foreach (JToken jToken in aResources)
                        {
                            lResources.Add((Resource)JsonConvert.DeserializeObject(jToken.ToString(), typeof(Resource), new ResourceConverter()));
                        }

                        property.SetValue(result, lResources, null);
                    }
                }
            }

            return result;
        }
コード例 #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject jObject = JObject.Load(reader);

            Object result = null;

            if (objectType == typeof(Collection))
            {
                result = new Collection();
            }
            else if (objectType == typeof(Resource))
            {
                if (jObject["type"].ToString() == "schema")
                {
                    result     = new SchemaResource();
                    objectType = typeof(SchemaResource);
                }
                else
                {
                    result = new ItemResource();
                }

                foreach (JProperty property in jObject.Properties())
                {
                    if (objectType.GetProperty(property.Name) == null)
                    {
                        ((Resource)result).setProperty(property.Name, property.Value.ToString());
                    }
                }
            }

            foreach (PropertyInfo property in objectType.GetProperties())
            {
                if (jObject.Property(property.Name) != null)
                {
                    Type propertyType = property.PropertyType;
                    if (propertyType == ResourceConverter.RESOURCE_FILTER)
                    {
                        Dictionary <string, List <KeyValuePair <string, string> > > dFilters = new Dictionary <string, List <KeyValuePair <string, string> > >();
                        foreach (JProperty p in jObject.Property(property.Name).Value)
                        {
                            List <KeyValuePair <string, string> > lFilterValues = new List <KeyValuePair <string, string> >();

                            if (p.Value.HasValues)
                            {
                                foreach (JToken jToken in p.Values())
                                {
                                    lFilterValues.Add(new KeyValuePair <string, string>("value", jToken["value"].ToString()));
                                }
                            }

                            dFilters.Add(p.Name, lFilterValues);
                        }
                        property.SetValue(result, dFilters, null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_STRING_ARRAY)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToObject <String[]>(), null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_STRING)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToString(), null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_DICTIONARY)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToObject <Dictionary <string, string> >(), null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_DICTIONARY_JTOKEN)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToObject <Dictionary <string, Dictionary <string, JToken> > >(), null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_DICTIONARY_DICTIONARY)
                    {
                        property.SetValue(result, jObject.Property(property.Name).Value.ToObject <Dictionary <string, Dictionary <string, string> > >(), null);
                    }
                    else if (propertyType == ResourceConverter.RESOURCE_LIST)
                    {
                        JArray          aResources = (JArray)jObject.Property(property.Name).Value;
                        List <Resource> lResources = new List <Resource>();
                        foreach (JToken jToken in aResources)
                        {
                            lResources.Add((Resource)JsonConvert.DeserializeObject(jToken.ToString(), typeof(Resource), new ResourceConverter()));
                        }

                        property.SetValue(result, lResources, null);
                    }
                }
            }

            return(result);
        }