Exemplo n.º 1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            Type valueType = value.GetType();

            if (TypeCacheUtils.IsControlArray(valueType))
            {
                writer.WriteStartObject();
                var coll = (IObservableCollectionMarkerInterface)value;
                writer.WritePropertyName("UniqueID");
                writer.WriteValue(coll.UniqueID);
                writer.WritePropertyName("Count");
                writer.WriteValue(coll.Count);
                writer.WritePropertyName("@arr");
                writer.WriteValue("1");
                writer.WriteEndObject();
            }
            else
            {
                try
                {
                    base.WriteJson(writer, value, serializer);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.Fail("StateObjectSerializerIndex::WriteJSON error trying to seialize object " + ex.Message);
                    writer.WriteStartObject();
                    writer.WriteEndObject();
                }
            }
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject jobj = JObject.Load(reader);
            var     assemblyQualifiedType = jobj["__type"].Value <string>();
            Type    targetType            = Type.GetType(assemblyQualifiedType, false, false);

            if (TypeCacheUtils.IsControlArray(targetType))
            {
                var     size  = jobj["Count"].Value <int>();
                var     uid   = jobj["UniqueID"].Value <string>();
                dynamic value = IocContainerImplWithUnity.Current.ResolveUnPrepared(targetType);
                value.SetupLazy(size, uid);
                return(value);
            }
            object newInstance = IocContainerImplWithUnity.Current.ResolveUnPrepared(targetType);

            foreach (JProperty prop in jobj.Properties().ToList())
            {
                if (prop.Name == "__type")
                {
                    continue;
                }
                //WARNING Fasterflect result might vary frp, the reflection results
                PropertyInfo reflectedProperty = targetType.Property(prop.Name);
                object       adapterValue      = Convert.ChangeType(prop.Value, reflectedProperty.PropertyType);
                reflectedProperty.SetValue(newInstance, adapterValue, null);
            }
            return(newInstance);
        }