public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            JObject      jsonObject = new JObject();
            BindableData data       = (BindableData)value;

            foreach (var property in data.Values.Where(p => BindableData.IsSerializableValue(p.Value.Property)))
            {
                object v = property.Value.GetValue();
                if (v == null)
                {
                    if (property.Value.ValueType == typeof(string) || property.Value.ValueType.GetInterface(nameof(IEnumerable)) == null)
                    {
                        jsonObject.AddFirst(new JProperty(property.Key, new JValue("")));
                    }
                    else
                    {
                        jsonObject.AddFirst(new JProperty(property.Key, new JArray()));
                    }
                }
                else
                {
                    jsonObject.AddFirst(new JProperty(property.Key, JToken.FromObject(v)));
                }
            }

            jsonObject.WriteTo(writer);
        }
        private static JObject GenerateJsonObject(Type type)
        {
            JObject jsonObject = new JObject();

            foreach (var property in BindableData.GetSerializableValues(type))
            {
                Type valueType = BindableData.GetBindableValueValueType(property.PropertyType);

                if (valueType.IsValueType)
                {
                    jsonObject.Add(new JProperty(property.Name, Activator.CreateInstance(valueType)));
                }
                else if (valueType == typeof(string) || valueType.GetInterface(nameof(IEnumerable)) == null)
                {
                    jsonObject.Add(new JProperty(property.Name, new JValue("")));
                }
                else
                {
                    jsonObject.Add(new JProperty(property.Name, new JArray()));
                }
            }

            foreach (var property in BindableData.GetBindableDatas(type).Where(t => BindableData.HasSerializeableValuesInChilds(t.PropertyType)))
            {
                jsonObject.Add(new JProperty(property.Name, GenerateJsonObject(property.PropertyType)));
            }

            return(jsonObject);
        }
Пример #3
0
        public BindableValueInfo(PropertyInfo property, BindableData owner)
        {
            Property      = property;
            BindableValue = property.GetValue(owner);

            EventInfo = property.PropertyType.GetEvent("OnValueChanged");
            ValueType = property.PropertyType.GetGenericArguments()[0];
            getMethod = BindableValue.GetType().GetMethod("Get", new Type[0]);
            invokeOnValueChangedMethod = BindableValue.GetType().GetMethod("InvokeOnValueChanged", new Type[0]);
        }
Пример #4
0
        private void Deserialize(BindableData deserialized)
        {
            foreach (var property in deserialized.AssignableValues.Where(kvp => IsSerializableValue(kvp.Value.Property)))
            {
                AssignableValues[property.Key].SetValue(property.Value.GetValue());
            }

            foreach (var child in deserialized.Childs)
            {
                Childs[child.Key].Deserialize(child.Value);
            }
        }
Пример #5
0
        public void Deserialize(string serialized)
        {
            BindableData deserialized = GetDeserializedData(serialized, GetType());

            Deserialize(deserialized);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            BindableData instance = (BindableData)existingValue;

            if (instance == null)
            {
                instance = (BindableData)Activator.CreateInstance(objectType);
                instance.Initialize();
            }

            JObject jsonObject = JObject.Load(reader);

            foreach (var serialized in jsonObject.Properties())
            {
                if (instance.AssignableValues.ContainsKey(serialized.Name))
                {
                    AssignableValueInfo reflected = instance.AssignableValues[serialized.Name];
                    if (BindableData.IsSerializableValue(reflected.Property))
                    {
                        if (serialized.Value.Type == JTokenType.Array)
                        {
                            Type elementType = null;
                            if (reflected.ValueType.IsArray)
                            {
                                elementType = reflected.ValueType.GetElementType();
                            }
                            else if (reflected.ValueType.GetInterface(nameof(IDictionary)) != null)
                            {
                                elementType = typeof(KeyValuePair <,>).MakeGenericType(reflected.ValueType.GenericTypeArguments);
                            }
                            else if (reflected.ValueType.GetInterface(nameof(IEnumerable)) != null)
                            {
                                elementType = reflected.ValueType.GenericTypeArguments[0];
                            }

                            var elements = serialized.Values().Select(v => v.ToObject(elementType)).ToArray();
                            reflected.SetValue(elements);
                            continue;
                        }
                        reflected.SetValue(serialized.Value.ToObject(reflected.ValueType));
                        continue;
                    }
                    else
                    {
                        Debug.LogError($"Property {serialized.Name} is not marked as serializable.");
                        continue;
                    }
                }
                else
                {
                    Debug.LogError($"Property {serialized.Name} is not found.");
                    continue;
                }
            }

            return(instance);
        }
Пример #7
0
 public AssignableValueInfo(PropertyInfo property, BindableData owner) : base(property, owner)
 {
     setMethod = BindableValue.GetType().GetMethod("Set", new Type[1] {
         ValueType
     });
 }