|
@@ -14,6 +14,11 @@ using Newtonsoft.Json.Linq;
|
|
|
|
|
|
namespace InABox.Core
|
|
|
{
|
|
|
+ public class SerialisationException : Exception
|
|
|
+ {
|
|
|
+ public SerialisationException(string message): base(message) { }
|
|
|
+ }
|
|
|
+
|
|
|
public interface ISerializeBinary
|
|
|
{
|
|
|
public void SerializeBinary(CoreBinaryWriter writer);
|
|
@@ -494,7 +499,7 @@ namespace InABox.Core
|
|
|
WriteBinaryValue(writer, userprop.Type, userprop.Value);
|
|
|
|
|
|
else
|
|
|
- throw new Exception($"Invalid type; Target DataType is {type} and value DataType is {value?.GetType().ToString() ?? "null"}");
|
|
|
+ throw new SerialisationException($"Invalid type; Target DataType is {type} and value DataType is {value?.GetType().ToString() ?? "null"}");
|
|
|
|
|
|
}
|
|
|
|
|
@@ -630,7 +635,7 @@ namespace InABox.Core
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- throw new Exception($"Invalid type; Target DataType is {type}");
|
|
|
+ throw new SerialisationException($"Invalid type; Target DataType is {type}");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -720,7 +725,7 @@ namespace InABox.Core
|
|
|
where TObject : BaseObject
|
|
|
{
|
|
|
if (!typeof(TObject).IsAssignableFrom(type))
|
|
|
- throw new Exception($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
|
|
|
+ throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
|
|
|
|
|
|
var properties = SerializableProperties(type).ToList();
|
|
|
|
|
@@ -749,7 +754,7 @@ namespace InABox.Core
|
|
|
where TObject : BaseObject
|
|
|
{
|
|
|
if (!typeof(TObject).IsAssignableFrom(type))
|
|
|
- throw new Exception($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
|
|
|
+ throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
|
|
|
|
|
|
var obj = (Activator.CreateInstance(type) as TObject)!;
|
|
|
obj.SetObserving(false);
|
|
@@ -758,8 +763,9 @@ namespace InABox.Core
|
|
|
for (int i = 0; i < nProps; ++i)
|
|
|
{
|
|
|
var propName = reader.ReadString();
|
|
|
- var property = DatabaseSchema.Property(type, propName);
|
|
|
- property?.Setter()(obj, reader.ReadBinaryValue(property.PropertyType));
|
|
|
+ var property = DatabaseSchema.Property(type, propName)
|
|
|
+ ?? throw new SerialisationException($"Property {propName} does not exist on {type.EntityName()}");
|
|
|
+ property.Setter()(obj, reader.ReadBinaryValue(property.PropertyType));
|
|
|
}
|
|
|
reader.ReadOriginalValues(obj);
|
|
|
|
|
@@ -794,7 +800,7 @@ namespace InABox.Core
|
|
|
where TObject : BaseObject
|
|
|
{
|
|
|
if (!typeof(TObject).IsAssignableFrom(type))
|
|
|
- throw new Exception($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
|
|
|
+ throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
|
|
|
|
|
|
var nObjs = objects?.Count ?? 0;
|
|
|
writer.Write(nObjs);
|
|
@@ -811,13 +817,16 @@ namespace InABox.Core
|
|
|
writer.Write(property.Name);
|
|
|
}
|
|
|
|
|
|
- foreach (var obj in objects)
|
|
|
+ if(objects != null)
|
|
|
{
|
|
|
- foreach (var property in properties)
|
|
|
+ foreach (var obj in objects)
|
|
|
{
|
|
|
- writer.WriteBinaryValue(property.PropertyType, property.Getter()(obj));
|
|
|
+ foreach (var property in properties)
|
|
|
+ {
|
|
|
+ writer.WriteBinaryValue(property.PropertyType, property.Getter()(obj));
|
|
|
+ }
|
|
|
+ writer.WriteOriginalValues(obj);
|
|
|
}
|
|
|
- writer.WriteOriginalValues(obj);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -836,7 +845,7 @@ namespace InABox.Core
|
|
|
public static List<TObject> ReadObjects<TObject>(this CoreBinaryReader reader, Type type) where TObject : BaseObject
|
|
|
{
|
|
|
if (!typeof(TObject).IsAssignableFrom(type))
|
|
|
- throw new Exception($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
|
|
|
+ throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
|
|
|
|
|
|
var objs = new List<TObject>();
|
|
|
var properties = new List<IProperty>();
|
|
@@ -850,8 +859,10 @@ namespace InABox.Core
|
|
|
var nProps = reader.ReadInt32();
|
|
|
for (int i = 0; i < nProps; ++i)
|
|
|
{
|
|
|
- var property = reader.ReadString();
|
|
|
- properties.Add(DatabaseSchema.Property(type, property));
|
|
|
+ var propertyName = reader.ReadString();
|
|
|
+ var property = DatabaseSchema.Property(type, propertyName)
|
|
|
+ ?? throw new SerialisationException($"Property {propertyName} does not exist on {type.EntityName()}");
|
|
|
+ properties.Add(property);
|
|
|
}
|
|
|
|
|
|
for (int i = 0; i < nObjs; ++i)
|