Esempio n. 1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var dictionary = serializer.Deserialize <Dictionary <string, object> >(reader);
            var name       = dictionary.GetValueOrDefault(_nameProperty) as string;
            var typeName   = dictionary.GetValueOrDefault(_typeProperty) as string;
            var targetType = typeName != null ? _controlTypes[typeName] : typeof(GuiControl);
            var properties = targetType
                             .GetRuntimeProperties()
                             .ToDictionary(p => p.Name);
            var style = new GuiControlStyle(name, targetType);

            foreach (var keyValuePair in dictionary.Where(i => i.Key != _typeProperty))
            {
                var propertyName = keyValuePair.Key;
                var rawValue     = keyValuePair.Value;

                PropertyInfo propertyInfo;
                var          value = properties.TryGetValue(propertyName, out propertyInfo)
                    ? DeserializeValueAs(serializer, rawValue, propertyInfo.PropertyType)
                    : DeserializeValueAs(serializer, rawValue, typeof(object));

                style.Add(propertyName, value);
            }

            return(style);
        }