GetPropertyConverters() public static method

public static GetPropertyConverters ( Type type ) : PropertyConverter>.Dictionary
type System.Type
return PropertyConverter>.Dictionary
Esempio n. 1
0
        public override object?ReadJson(JsonReader reader, Type objectType, object?existingValue, JsonSerializer serializer)
        {
            using (HeavyProfiler.LogNoStackTrace("ReadJson", () => objectType.Name))
            {
                if (reader.TokenType == JsonToken.Null)
                {
                    return(null);
                }

                using (EntityCache ec = new EntityCache())
                {
                    reader.Assert(JsonToken.StartObject);

                    ModifiableEntity mod = GetEntity(reader, objectType, existingValue, out bool markedAsModified);

                    var pr = GetCurrentPropertyRoute(mod);

                    var dic = PropertyConverter.GetPropertyConverters(mod.GetType());
                    using (JsonSerializerExtensions.SetAllowDirectMListChanges(markedAsModified))
                        while (reader.TokenType == JsonToken.PropertyName)
                        {
                            if ((string)reader.Value ! == "mixins")
                            {
                                var entity = (Entity)mod;
                                reader.Read();
                                reader.Assert(JsonToken.StartObject);

                                reader.Read();
                                while (reader.TokenType == JsonToken.PropertyName)
                                {
                                    var mixin = entity[(string)reader.Value !];
Esempio n. 2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            using (EntityCache ec = new EntityCache())
            {
                reader.Assert(JsonToken.StartObject);

                ModifiableEntity mod = GetEntity(reader, objectType, existingValue, serializer, out bool markedAsModified);

                var pr = GetCurrentPropertyRoute(mod);

                var dic = PropertyConverter.GetPropertyConverters(mod.GetType());

                while (reader.TokenType == JsonToken.PropertyName)
                {
                    if ((string)reader.Value == "mixins")
                    {
                        var entity = (Entity)mod;
                        reader.Read();
                        reader.Assert(JsonToken.StartObject);

                        reader.Read();
                        while (reader.TokenType == JsonToken.PropertyName)
                        {
                            var mixin = entity[(string)reader.Value];

                            reader.Read();

                            using (JsonSerializerExtensions.SetCurrentPropertyRoute(pr.Add(mixin.GetType())))
                                serializer.DeserializeValue(reader, mixin.GetType(), mixin);

                            reader.Read();
                        }

                        reader.Assert(JsonToken.EndObject);
                        reader.Read();
                    }
                    else
                    {
                        PropertyConverter pc = dic.GetOrThrow((string)reader.Value);

                        reader.Read();
                        ReadJsonProperty(reader, serializer, mod, pc, pr, markedAsModified);

                        reader.Read();
                    }
                }

                reader.Assert(JsonToken.EndObject);

                AfterDeserilization.Invoke(mod);

                return(mod);
            }
        }
Esempio n. 3
0
        private bool ValidateModifiableEntity(ValidationContext validationContext, ModifiableEntity mod)
        {
            bool          isValid       = true;
            PropertyScope propertyScope = new PropertyScope();

            validationContext.KeyBuilders.Push(propertyScope);

            var entity = mod as Entity;

            using (entity == null ? null : entity.Mixins.OfType <CorruptMixin>().Any(c => c.Corrupt) ? Corruption.AllowScope() : Corruption.DenyScope())
            {
                foreach (var kvp in PropertyConverter.GetPropertyConverters(mod.GetType()))
                {
                    if (kvp.Value.AvoidValidate)
                    {
                        continue;
                    }

                    propertyScope.PropertyName = kvp.Key;
                    if (SignumValidate(validationContext, kvp.Value.GetValue(mod)) ?? true)
                    {
                        isValid = false;
                    }

                    string error = kvp.Value.PropertyValidator.PropertyCheck(mod);

                    if (error != null)
                    {
                        string key = CalculateKey(validationContext);
                        if (validationContext.ModelState.IsValidField(key))
                        {
                            isValid = false;
                            validationContext.ModelState.AddModelError(key, error);
                        }
                    }
                }
            }

            if (entity != null && entity.Mixins.Any())
            {
                propertyScope.PropertyName = "mixins";
                PropertyScope mixinScope = new PropertyScope();
                validationContext.KeyBuilders.Push(mixinScope);
                foreach (var mixin in entity.Mixins)
                {
                    mixinScope.PropertyName = mixin.GetType().Name;
                    if (!ValidateModifiableEntity(validationContext, mixin))
                    {
                        isValid = false;
                    }
                }
                validationContext.KeyBuilders.Pop();
            }

            validationContext.KeyBuilders.Pop();
            return(isValid);
        }
Esempio n. 4
0
        private bool ValidateModifiableEntity(ModifiableEntity mod)
        {
            using (Validator.ModelBinderScope())
            {
                bool isValid = true;

                var entity = mod as Entity;
                using (entity == null ? null : entity.Mixins.OfType <CorruptMixin>().Any(c => c.Corrupt) ? Corruption.AllowScope() : Corruption.DenyScope())
                {
                    foreach (var kvp in PropertyConverter.GetPropertyConverters(mod.GetType()))
                    {
                        if (kvp.Value.AvoidValidate)
                        {
                            continue;
                        }

                        string error = kvp.Value.PropertyValidator.PropertyCheck(mod);

                        if (error != null)
                        {
                            isValid = false;
                            ModelState.AddModelError(this.Key + "." + kvp.Key, error);
                        }

                        var val = kvp.Value.GetValue(mod);
                        if (this.CurrentPath.Push(val))
                        {
                            using (StateManager.Recurse(this, this.Key + "." + kvp.Key, null, val, null))
                            {
                                if (this.SignumValidate() == false)
                                {
                                    isValid = false;
                                }
                            }
                        }
                    }
                }

                if (entity != null && entity.Mixins.Any())
                {
                    foreach (var mixin in entity.Mixins)
                    {
                        if (this.CurrentPath.Push(mixin))
                        {
                            using (StateManager.Recurse(this, "mixins[" + mixin.GetType().Name + "].element", null, mixin, null))
                            {
                                isValid &= ValidateModifiableEntity(mixin);
                            }
                        }
                    }
                }

                return(isValid);
            }
        }
Esempio n. 5
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            using (EntityCache ec = new EntityCache())
            {
                reader.Assert(JsonToken.StartObject);

                bool             markedAsModified;
                ModifiableEntity mod = GetEntity(reader, objectType, existingValue, serializer, out markedAsModified);

                var pr = JsonSerializerExtensions.CurrentPropertyRoute;
                if (pr == null || mod is IRootEntity)
                {
                    pr = PropertyRoute.Root(mod.GetType());
                }
                else if (pr.Type.ElementType() == objectType)
                {
                    pr = pr.Add("Item"); //Because we have a custom MListJsonConverter but not for other simpler collections
                }
                var dic = PropertyConverter.GetPropertyConverters(mod.GetType());

                while (reader.TokenType == JsonToken.PropertyName)
                {
                    if ((string)reader.Value == "mixins")
                    {
                        var entity = (Entity)mod;
                        reader.Read();
                        reader.Assert(JsonToken.StartObject);

                        reader.Read();
                        while (reader.TokenType == JsonToken.PropertyName)
                        {
                            var mixin = entity[(string)reader.Value];

                            reader.Read();

                            using (JsonSerializerExtensions.SetCurrentPropertyRoute(pr.Add(mixin.GetType())))
                                serializer.DeserializeValue(reader, mixin.GetType(), mixin);

                            reader.Read();
                        }

                        reader.Assert(JsonToken.EndObject);
                        reader.Read();
                    }
                    else
                    {
                        PropertyConverter pc = dic.GetOrThrow((string)reader.Value);

                        reader.Read();
                        ReadJsonProperty(reader, serializer, mod, pc, pr, markedAsModified);

                        reader.Read();
                    }
                }

                reader.Assert(JsonToken.EndObject);

                AfterDeserilization.Invoke(mod);

                return(mod);
            }
        }
Esempio n. 6
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var pr = JsonSerializerExtensions.CurrentPropertyRoute;

            if (pr == null || typeof(IRootEntity).IsAssignableFrom(pr.Type))
            {
                pr = PropertyRoute.Root(value.GetType());
            }
            else if (pr.Type.ElementType() == value.GetType())
            {
                pr = pr.Add("Item");
            }

            ModifiableEntity mod = (ModifiableEntity)value;

            writer.WriteStartObject();

            var entity = mod as Entity;

            if (entity != null)
            {
                writer.WritePropertyName("Type");
                writer.WriteValue(TypeLogic.TryGetCleanName(mod.GetType()));

                writer.WritePropertyName("id");
                writer.WriteValue(entity.IdOrNull == null ? null : entity.Id.Object);

                if (entity.IsNew)
                {
                    writer.WritePropertyName("isNew");
                    writer.WriteValue(true);
                }

                if (Schema.Current.Table(entity.GetType()).Ticks != null)
                {
                    writer.WritePropertyName("ticks");
                    writer.WriteValue(entity.Ticks.ToString());
                }
            }
            else
            {
                writer.WritePropertyName("Type");
                writer.WriteValue(mod.GetType().Name);
            }

            if (!(mod is MixinEntity))
            {
                writer.WritePropertyName("toStr");
                writer.WriteValue(mod.ToString());
            }

            writer.WritePropertyName("modified");
            writer.WriteValue(mod.Modified == ModifiedState.Modified || mod.Modified == ModifiedState.SelfModified);

            foreach (var kvp in PropertyConverter.GetPropertyConverters(value.GetType()))
            {
                WriteJsonProperty(writer, serializer, mod, kvp.Key, kvp.Value, pr);
            }

            if (entity != null && entity.Mixins.Any())
            {
                writer.WritePropertyName("mixins");
                writer.WriteStartObject();

                foreach (var m in entity.Mixins)
                {
                    var prm = pr.Add(m.GetType());

                    using (JsonSerializerExtensions.SetCurrentPropertyRoute(prm))
                    {
                        writer.WritePropertyName(m.GetType().Name);
                        serializer.Serialize(writer, m);
                    }
                }

                writer.WriteEndObject();
            }

            writer.WriteEndObject();
        }