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 !];
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); } }
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); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } reader.Assert(JsonToken.StartObject); string toString = null; string idObj = null; string typeStr = null; Entity entity = null; reader.Read(); while (reader.TokenType == JsonToken.PropertyName) { switch ((string)reader.Value) { case "toStr": toString = reader.ReadAsString(); break; case "id": idObj = reader.ReadAsString(); break; case "EntityType": typeStr = reader.ReadAsString(); break; case "entity": reader.Read(); entity = (Entity)serializer.Deserialize(reader, typeof(Entity)); break; default: throw new InvalidOperationException("unexpected property " + (string)reader.Value); } reader.Read(); } reader.Assert(JsonToken.EndObject); Type type = TypeLogic.GetType(typeStr); PrimaryKey?idOrNull = idObj == null ? (PrimaryKey?)null : PrimaryKey.Parse(idObj, type); if (entity == null) { return(Lite.Create(type, idOrNull.Value, toString)); } var result = entity.ToLiteFat(toString); if (result.EntityType != type) { throw new InvalidOperationException("Types don't match"); } if (result.IdOrNull != idOrNull) { throw new InvalidOperationException("Id's don't match"); } var existing = existingValue as Lite <Entity>; if (existing.Is(result) && existing.EntityOrNull == null && result.EntityOrNull != null) { existing.SetEntity(result.EntityOrNull); return(existing); } return(result); }
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); }
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)); } } }