Browse Source

Fixed FilterConstants in filter deserialisation

Kenric Nugteren 4 days ago
parent
commit
97e7e6ca1e
3 changed files with 27 additions and 5 deletions
  1. 8 2
      InABox.Core/Query/Filter.cs
  2. 1 1
      InABox.Core/Query/SortOrder.cs
  3. 18 2
      InABox.Core/Serialization.cs

+ 8 - 2
InABox.Core/Query/Filter.cs

@@ -2152,11 +2152,13 @@ namespace InABox.Core
                 else if (string.Equals(tag, "Expression"))
                     propertyName = reader.GetString() ?? "";
                 else if (string.Equals(tag, "Operator"))
-                    op = Enum.Parse<Operator>(reader.GetString());
+                {
+                    op = ReadEnum<Operator>(ref reader);
+                }
                 else if (string.Equals(tag, "IsNot"))
                     isNot = reader.GetBoolean();
                 else if (string.Equals(tag, "FilterConstant"))
-                    value = Enum.Parse<FilterConstant>(reader.GetString());
+                    value = ReadEnum<FilterConstant>(ref reader);
                 else if (string.Equals(tag, "CustomValue"))
                     value = new CustomFilterValue(Convert.FromBase64String(reader.GetString()));
                 else if (string.Equals(tag, "Value"))
@@ -2202,6 +2204,10 @@ namespace InABox.Core
             {
                 result.Value = CoreUtils.ChangeType(value, result.Type.MakeArrayType());
             }
+            else if(value is FilterConstant || value is CustomFilterValue)
+            {
+                result.Value = value;
+            }
             else
             {
                 result.Value = CoreUtils.ChangeType(value, result.Type);

+ 1 - 1
InABox.Core/Query/SortOrder.cs

@@ -302,7 +302,7 @@ namespace InABox.Core
                 else if (string.Equals(tag, "Expression"))
                     sExpr = reader.GetString();
                 else if (string.Equals(tag, "Direction"))
-                    dir = Enum.Parse<SortDirection>(reader.GetString());
+                    dir = ReadEnum<SortDirection>(ref reader);
                 else if (string.Equals(tag, "Thens"))
                 {
                     while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)

+ 18 - 2
InABox.Core/Serialization.cs

@@ -51,7 +51,7 @@ namespace InABox.Core
 
         private static JsonConverter[] GetConverters()
         {
-            _converters ??= CoreUtils.Entities.Where(x => typeof(JsonConverter).IsAssignableFrom(x))
+            _converters ??= CoreUtils.Entities.Where(x => typeof(JsonConverter).IsAssignableFrom(x) && x.HasInterface<IGlobalJsonConverter>())
                 .Select(x => Activator.CreateInstance(x) as JsonConverter)
                 .NotNull()
                 .ToArray();
@@ -884,7 +884,10 @@ namespace InABox.Core
         }
     }
 
-    public abstract class CustomJsonConverter<T> : JsonConverter<T>
+    public interface IGlobalJsonConverter
+    {
+    }
+    public abstract class CustomJsonConverter<T> : JsonConverter<T>, IGlobalJsonConverter
     {
         protected object? ReadJson(ref Utf8JsonReader reader)
         {
@@ -917,6 +920,19 @@ namespace InABox.Core
                     return null;
             }
         }
+
+        protected T ReadEnum<T>(ref Utf8JsonReader reader)
+            where T : struct
+        {
+            if(reader.TokenType == JsonTokenType.Number)
+            {
+                return (T)Enum.ToObject(typeof(T), reader.GetInt32());
+            }
+            else
+            {
+                return Enum.Parse<T>(reader.GetString());
+            }
+        }
         
         /// <summary>
         /// Write a value as a JSON object; note that some data types, like