Browse Source

Fixed serialisation of entity links and enclosed entities by stuffing around with interceptors

Kenric Nugteren 1 week ago
parent
commit
666fe58518
2 changed files with 37 additions and 4 deletions
  1. 36 4
      InABox.Core/Objects/BaseObject.cs
  2. 1 0
      InABox.Core/Serialization.cs

+ 36 - 4
InABox.Core/Objects/BaseObject.cs

@@ -119,7 +119,7 @@ namespace InABox.Core
     /// <summary>
     ///     Observable object with INotifyPropertyChanged implemented
     /// </summary>
-    public abstract class BaseObject : INotifyPropertyChanged, IBaseObject, IJsonOnDeserializing, IJsonOnDeserialized
+    public abstract class BaseObject : INotifyPropertyChanged, IBaseObject, IJsonOnDeserializing, IJsonOnDeserialized, IJsonOnSerialized, IJsonOnSerializing
     {
         public BaseObject()
         {
@@ -129,6 +129,7 @@ namespace InABox.Core
         }
 
         internal bool _disabledInterceptor;
+        private bool _deserialising;
 
         protected T InitializeField<T>(ref T? field, [CallerMemberName] string name = "")
             where T : BaseObject, ISubObject, new()
@@ -147,7 +148,7 @@ namespace InABox.Core
         [GetInterceptor]
         protected T GetValue<T>(Type propertyType, ref T field, string name)
         {
-            if (_disabledInterceptor) return field;
+            if (_disabledInterceptor || _deserialising) return field;
 
             if(field is null && propertyType.HasInterface<ISubObject>() && !propertyType.IsAbstract)
             {
@@ -165,19 +166,50 @@ namespace InABox.Core
         }
 
         [SetInterceptor]
-        protected void SetValue<T>(ref T field, T newValue)
+        protected void SetValue<T>(Type propertyType, ref T field, string name, T newValue)
         {
-            field = newValue;
+            if (_disabledInterceptor || !_deserialising)
+            {
+                field = newValue;
+                return;
+            }
+
+            if(field is null && newValue is ISubObject subObj && !propertyType.IsAbstract)
+            {
+                subObj.SetLinkedParent(this);
+                subObj.SetLinkedPath(name);
+                if(subObj is BaseObject obj)
+                {
+                    obj.SetObserving(_observing);
+                }
+                field = newValue;
+            }
+            else
+            {
+                field = newValue;
+            }
+        }
+
+        public void OnSerializing()
+        {
+            _disabledInterceptor = true;
+        }
+
+        public void OnSerialized()
+        {
+            _disabledInterceptor = false;
         }
 
         public void OnDeserializing()
         {
+            _deserialising = true;
             if (_observing)
                 SetObserving(false);
         }
 
         public void OnDeserialized()
         {
+            _deserialising = false;
             if (!_observing)
                 SetObserving(true);
         }

+ 1 - 0
InABox.Core/Serialization.cs

@@ -760,6 +760,7 @@ namespace InABox.Core
                 writer.Write(property.Name);
                 writer.WriteBinaryValue(property.PropertyType, property.Getter()(entity));
             }
+
             writer.WriteOriginalValues(entity);
         }