SetCurrentPropertyRoute() public static méthode

public static SetCurrentPropertyRoute ( PropertyRoute route ) : IDisposable
route PropertyRoute
Résultat IDisposable
        static void WriteJsonInternal <T>(JsonWriter writer, MList <T> value, JsonSerializer serializer)
        {
            writer.WriteStartArray();
            var pr = JsonSerializerExtensions.CurrentPropertyRoute;

            var elementPr = pr.Add("Item");

            using (JsonSerializerExtensions.SetCurrentPropertyRoute(elementPr))
            {
                foreach (var item in ((IMListPrivate <T>)value).InnerList)
                {
                    writer.WriteStartObject();

                    writer.WritePropertyName("rowId");
                    writer.WriteValue(item.RowId?.Object);

                    writer.WritePropertyName("element");
                    serializer.Serialize(writer, item.Element);

                    writer.WriteEndObject();
                }
            }

            writer.WriteEndArray();
        }
Exemple #2
0
        public void WriteJsonProperty(JsonWriter writer, JsonSerializer serializer, ModifiableEntity mod, string lowerCaseName, PropertyConverter pc, PropertyRoute route)
        {
            if (pc.CustomWriteJsonProperty != null)
            {
                pc.CustomWriteJsonProperty(new WriteJsonPropertyContext
                {
                    JsonWriter          = writer,
                    JsonSerializer      = serializer,
                    LowerCaseName       = lowerCaseName,
                    Entity              = mod,
                    ParentPropertyRoute = route,
                    PropertyConverter   = pc
                });
            }
            else
            {
                var pr = route.Add(pc.PropertyValidator.PropertyInfo);

                string error = CanReadPropertyRoute?.Invoke(pr);

                if (error != null)
                {
                    return;
                }

                using (JsonSerializerExtensions.SetCurrentPropertyRoute(pr))
                {
                    writer.WritePropertyName(lowerCaseName);
                    serializer.Serialize(writer, pc.GetValue(mod));
                }
            }
        }
Exemple #3
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);
            }
        }
Exemple #4
0
        public void ReadJsonProperty(JsonReader reader, JsonSerializer serializer, ModifiableEntity entity, PropertyConverter pc, PropertyRoute parentRoute, bool markedAsModified)
        {
            if (pc.CustomReadJsonProperty != null)
            {
                pc.CustomReadJsonProperty(new ReadJsonPropertyContext(
                                              jsonReader: reader,
                                              jsonSerializer: serializer,
                                              entity: entity,
                                              parentPropertyRoute: parentRoute,
                                              propertyConverter: pc
                                              ));
            }
            else
            {
                object?oldValue = pc.GetValue !(entity);

                var pi = pc.PropertyValidator !.PropertyInfo;

                var pr = parentRoute.Add(pi);

                using (JsonSerializerExtensions.SetCurrentPropertyRoute(pr))
                {
                    object newValue = serializer.DeserializeValue(reader, pi.PropertyType.Nullify(), oldValue);

                    if (!IsEquals(newValue, oldValue))
                    {
                        if (!markedAsModified && parentRoute.RootType.IsEntity())
                        {
                            if (!pi.HasAttribute <IgnoreAttribute>())
                            {
                                try
                                {
                                    //Call attention of developer
                                    throw new InvalidOperationException($"'modified' is not set but '{pi.Name}' is modified");
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                        else
                        {
                            AssertCanWrite(pr);
                            if (newValue == null && pc.IsNotNull())
                            {
                                entity.SetTemporalError(pi, ValidationMessage._0IsNotSet.NiceToString(pi.NiceName()));
                                return;
                            }

                            pc.SetValue?.Invoke(entity, newValue);
                        }
                    }
                }
            }
        }
Exemple #5
0
        public void ReadJsonProperty(JsonReader reader, JsonSerializer serializer, ModifiableEntity entity, PropertyConverter pc, PropertyRoute parentRoute, bool markedAsModified)
        {
            if (pc.CustomReadJsonProperty != null)
            {
                pc.CustomReadJsonProperty(new ReadJsonPropertyContext
                {
                    JsonReader          = reader,
                    JsonSerializer      = serializer,
                    Entity              = entity,
                    ParentPropertyRoute = parentRoute,
                    PropertyConverter   = pc,
                });
            }
            else
            {
                object oldValue = pc.GetValue(entity);

                var pi = pc.PropertyValidator.PropertyInfo;

                var pr = parentRoute.Add(pi);

                using (JsonSerializerExtensions.SetCurrentPropertyRoute(pr))
                {
                    object newValue = serializer.DeserializeValue(reader, pi.PropertyType, oldValue);

                    if (!IsEquals(newValue, oldValue))
                    {
                        if (!markedAsModified && parentRoute.RootType.IsEntity())
                        {
                            try
                            {
                                //Call attention of developer
                                throw new InvalidOperationException($"'modified' is not set but '{pi.Name}' is modified");
                            }
                            catch (Exception)
                            {
                            }
                        }
                        else
                        {
                            AssertCanWrite(pr);
                            if (newValue == null && pc.IsNotNull()) //JSON.Net already complaining
                            {
                                return;
                            }

                            pc.SetValue?.Invoke(entity, newValue);
                        }
                    }
                }
            }
        }
Exemple #6
0
        public override void WriteJson(JsonWriter writer, object?value, JsonSerializer serializer)
        {
            using (HeavyProfiler.LogNoStackTrace("ReadJson", () => typeof(ResultTable).Name))
            {
                var rt = (ResultTable)value !;

                writer.WriteStartObject();

                writer.WritePropertyName("entityColumn");
                writer.WriteValue(rt.EntityColumn?.Name);

                writer.WritePropertyName("columns");
                serializer.Serialize(writer, rt.Columns.Select(c => c.Column.Token.FullKey()).ToList());

                writer.WritePropertyName("pagination");
                serializer.Serialize(writer, new PaginationTS(rt.Pagination));

                writer.WritePropertyName("totalElements");
                writer.WriteValue(rt.TotalElements);


                writer.WritePropertyName("rows");
                writer.WriteStartArray();
                foreach (var row in rt.Rows)
                {
                    writer.WriteStartObject();
                    if (rt.EntityColumn != null)
                    {
                        writer.WritePropertyName("entity");
                        serializer.Serialize(writer, row.Entity);
                    }

                    writer.WritePropertyName("columns");
                    writer.WriteStartArray();
                    foreach (var column in rt.Columns)
                    {
                        using (JsonSerializerExtensions.SetCurrentPropertyRoute(column.Column.Token.GetPropertyRoute()))
                        {
                            serializer.Serialize(writer, row[column]);
                        }
                    }
                    writer.WriteEndArray();


                    writer.WriteEndObject();
                }
                writer.WriteEndArray();


                writer.WriteEndObject();
            }
        }
Exemple #7
0
        public void WriteJsonProperty(JsonWriter writer, JsonSerializer serializer, ModifiableEntity mod, string lowerCaseName, PropertyConverter pc, PropertyRoute route)
        {
            if (pc.CustomWriteJsonProperty != null)
            {
                pc.CustomWriteJsonProperty(new WriteJsonPropertyContext
                {
                    JsonWriter          = writer,
                    JsonSerializer      = serializer,
                    LowerCaseName       = lowerCaseName,
                    Entity              = mod,
                    ParentPropertyRoute = route,
                    PropertyConverter   = pc
                });
            }
            else
            {
                var pr = route.Add(pc.PropertyValidator.PropertyInfo);

                string error = CanReadPropertyRoute?.Invoke(pr);

                if (error != null)
                {
                    return;
                }

                using (JsonSerializerExtensions.SetCurrentPropertyRoute(pr))
                {
                    writer.WritePropertyName(lowerCaseName);
                    var val = pc.GetValue(mod);
                    if (val is Lite <Entity> lite)
                    {
                        new LiteJsonConverter().WriteJson(writer, lite, serializer);
                    }
                    else
                    {
                        serializer.Serialize(writer, pc.GetValue(mod));
                    }

                    if (writer.WriteState == WriteState.Property)
                    {
                        throw new InvalidOperationException($"Impossible to serialize '{mod}' to JSON. Maybe there is a cycle?");
                    }
                }
            }
        }
Exemple #8
0
        static void WriteJsonInternal <T>(JsonWriter writer, MList <T> value, JsonSerializer serializer)
        {
            var errors = new List <string>();

            serializer.Error += delegate(object?sender, ErrorEventArgs args)
            {
                // only log an error once
                if (args.CurrentObject == args.ErrorContext.OriginalObject)
                {
                    errors.Add(args.ErrorContext.Error.Message);
                }
            };

            writer.WriteStartArray();
            var pr = JsonSerializerExtensions.CurrentPropertyRoute !;

            var elementPr = pr.Add("Item");

            using (JsonSerializerExtensions.SetCurrentPropertyRoute(elementPr))
            {
                foreach (var item in ((IMListPrivate <T>)value).InnerList)
                {
                    writer.WriteStartObject();

                    writer.WritePropertyName("rowId");
                    writer.WriteValue(item.RowId?.Object);

                    writer.WritePropertyName("element");
                    serializer.Serialize(writer, item.Element);

                    writer.WriteEndObject();
                }
            }

            if (errors.Any())
            {
                throw new JsonSerializationException(errors.ToString("\r\n"));
            }

            writer.WriteEndArray();
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            Lite <Entity> lite = (Lite <Entity>)value;

            writer.WriteStartObject();

            writer.WritePropertyName("EntityType");
            serializer.Serialize(writer, TypeLogic.GetCleanName(lite.EntityType));

            writer.WritePropertyName("id");
            serializer.Serialize(writer, lite.IdOrNull == null ? null : lite.Id.Object);

            writer.WritePropertyName("toStr");
            serializer.Serialize(writer, lite.ToString());

            if (lite.EntityOrNull != null)
            {
                writer.WritePropertyName("entity");
                using (JsonSerializerExtensions.SetCurrentPropertyRoute(PropertyRoute.Root(lite.Entity.GetType())))
                    serializer.Serialize(writer, lite.Entity);
            }

            writer.WriteEndObject();
        }
        public override void WriteJson(JsonWriter writer, object?value, JsonSerializer serializer)
        {
            using (HeavyProfiler.LogNoStackTrace("WriteJson", () => value !.GetType().Name))
            {
                PropertyRoute pr = GetCurrentPropertyRoute(value !);

                ModifiableEntity mod = (ModifiableEntity)value !;

                writer.WriteStartObject();

                if (mod is Entity entity)
                {
                    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 (mod.Mixins.Any())
                {
                    writer.WritePropertyName("mixins");
                    writer.WriteStartObject();

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

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

                    writer.WriteEndObject();
                }

                writer.WriteEndObject();
            }
        }
Exemple #11
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);
            }
        }
Exemple #12
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();
        }
        static MList <T> ReadJsonInternal <T>(JsonReader reader, IMListPrivate <T> existingValue, JsonSerializer serializer)
        {
            var dic = existingValue == null ? new Dictionary <PrimaryKey, MList <T> .RowIdElement>() :
                      existingValue.InnerList.Where(a => a.RowId.HasValue).ToDictionary(a => a.RowId.Value, a => a);

            var newList = new List <MList <T> .RowIdElement>();

            var pr = JsonSerializerExtensions.CurrentPropertyRoute;

            var elementPr = pr.Add("Item");

            var rowIdType = GetRowIdTypeFromAttribute(pr);


            reader.Assert(JsonToken.StartArray);

            reader.Read();

            using (JsonSerializerExtensions.SetCurrentPropertyRoute(elementPr))
            {
                while (reader.TokenType == JsonToken.StartObject)
                {
                    reader.Read();
                    reader.Assert(JsonToken.PropertyName);
                    if (((string)reader.Value) != "rowId")
                    {
                        throw new JsonSerializationException($"member 'rowId' expected in {reader.Path}");
                    }

                    reader.Read();
                    var rowIdValue = reader.Value;

                    reader.Read();
                    reader.Assert(JsonToken.PropertyName);
                    if (((string)reader.Value) != "element")
                    {
                        throw new JsonSerializationException($"member 'element' expected in {reader.Path}");
                    }

                    reader.Read();
                    if (rowIdValue != null && !rowIdValue.Equals(GraphExplorer.DummyRowId.Object))
                    {
                        var rowId = new PrimaryKey((IComparable)ReflectionTools.ChangeType(rowIdValue, rowIdType));

                        var oldValue = dic.TryGetS(rowId);

                        if (oldValue == null)
                        {
                            T newValue = (T)serializer.DeserializeValue(reader, typeof(T), null);

                            newList.Add(new MList <T> .RowIdElement(newValue, rowId, null));
                        }
                        else
                        {
                            T newValue = (T)serializer.DeserializeValue(reader, typeof(T), oldValue.Value.Element);

                            if (oldValue.Value.Element.Equals(newValue))
                            {
                                newList.Add(new MList <T> .RowIdElement(newValue, rowId, oldValue.Value.OldIndex));
                            }
                            else
                            {
                                newList.Add(new MList <T> .RowIdElement(newValue));
                            }
                        }
                    }
                    else
                    {
                        var newValue = (T)serializer.DeserializeValue(reader, typeof(T), null);
                        newList.Add(new MList <T> .RowIdElement(newValue));
                    }

                    reader.Read();

                    reader.Assert(JsonToken.EndObject);
                    reader.Read();
                }
            }

            reader.Assert(JsonToken.EndArray);

            if (existingValue == null) //Strange case...
            {
                if (newList.IsEmpty())
                {
                    return(null);
                }
                else
                {
                    existingValue = new MList <T>();
                }
            }

            bool orderMatters = GetPreserveOrderFromAttribute(pr);

            if (!existingValue.IsEqualTo(newList, orderMatters))
            {
                EntityJsonConverter.AssertCanWrite(pr);

                existingValue.AssignMList(newList);
            }

            return((MList <T>)existingValue);
        }
Exemple #14
0
        static MList <T> ReadJsonInternal <T>(JsonReader reader, IMListPrivate <T> existingValue, JsonSerializer serializer)
        {
            var errors = new List <string>();

            serializer.Error += delegate(object?sender, ErrorEventArgs args)
            {
                // only log an error once
                if (args.CurrentObject == args.ErrorContext.OriginalObject)
                {
                    errors.Add(args.ErrorContext.Error.Message);
                }
            };

            var dic = existingValue == null ? new Dictionary <PrimaryKey, MList <T> .RowIdElement>() :
                      existingValue.InnerList.Where(a => a.RowId.HasValue).ToDictionary(a => a.RowId !.Value, a => a);

            var newList = new List <MList <T> .RowIdElement>();

            var pr = JsonSerializerExtensions.CurrentPropertyRoute !;

            var elementPr = pr.Add("Item");

            var rowIdType = GetRowIdTypeFromAttribute(pr);


            reader.Assert(JsonToken.StartArray);

            reader.Read();

            using (JsonSerializerExtensions.SetCurrentPropertyRoute(elementPr))
            {
                while (reader.TokenType == JsonToken.StartObject)
                {
                    reader.Read();
                    reader.Assert(JsonToken.PropertyName);
                    if (((string)reader.Value !) != "rowId")
                    {
                        throw new JsonSerializationException($"member 'rowId' expected in {reader.Path}");
                    }

                    reader.Read();
                    var rowIdValue = reader.Value;

                    reader.Read();
                    reader.Assert(JsonToken.PropertyName);
                    if (((string)reader.Value !) != "element")
                    {
                        throw new JsonSerializationException($"member 'element' expected in {reader.Path}");
                    }

                    reader.Read();
                    if (rowIdValue != null && !rowIdValue.Equals(GraphExplorer.DummyRowId.Object))
                    {
                        var rowId = new PrimaryKey((IComparable)ReflectionTools.ChangeType(rowIdValue, rowIdType) !);

                        var oldValue = dic.TryGetS(rowId);

                        if (oldValue == null)
                        {
                            T newValue = (T)serializer.DeserializeValue(reader, typeof(T), null) !;

                            newList.Add(new MList <T> .RowIdElement(newValue, rowId, null));
                        }
                        else
                        {
                            T newValue = (T)serializer.DeserializeValue(reader, typeof(T), oldValue.Value.Element) !;

                            if (oldValue.Value.Element !.Equals(newValue))
                            {
                                newList.Add(new MList <T> .RowIdElement(newValue, rowId, oldValue.Value.OldIndex));
                            }
                            else
                            {
                                newList.Add(new MList <T> .RowIdElement(newValue));
                            }
                        }
                    }