|
@@ -2,6 +2,7 @@
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Runtime.InteropServices.ComTypes;
|
|
|
+using System.Text.Json;
|
|
|
using System.Text.Json.Serialization;
|
|
|
using InABox.Core;
|
|
|
|
|
@@ -552,6 +553,7 @@ namespace InABox.Clients
|
|
|
|
|
|
public interface IMultiQueryTableQuery
|
|
|
{
|
|
|
+ [JsonPropertyOrder(0)]
|
|
|
string Type { get; }
|
|
|
|
|
|
IFilter? Filter { get; set; }
|
|
@@ -652,6 +654,107 @@ namespace InABox.Clients
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public class MultiQueryRequestConverter : CustomJsonConverter<MultiQueryRequest>
|
|
|
+ {
|
|
|
+ private IMultiQueryTableQuery ReadQuery(ref Utf8JsonReader reader, JsonSerializerOptions options)
|
|
|
+ {
|
|
|
+ Type? type = null;
|
|
|
+ IFilter? filter = null;
|
|
|
+ IColumns? columns = null;
|
|
|
+ ISortOrder? sort = null;
|
|
|
+
|
|
|
+ ReadObject(ref reader, (ref Utf8JsonReader reader, string property) =>
|
|
|
+ {
|
|
|
+ if(property == "Type")
|
|
|
+ {
|
|
|
+ var typeName = reader.GetString() ?? "";
|
|
|
+ type = CoreUtils.GetEntity(typeName);
|
|
|
+ if(type is null)
|
|
|
+ {
|
|
|
+ throw new JsonException($"Invalid type name {typeName}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(type is null)
|
|
|
+ {
|
|
|
+ throw new JsonException($"'Type' expected as first field, got {property}");
|
|
|
+ }
|
|
|
+ else if(property == "Filter")
|
|
|
+ {
|
|
|
+ filter = JsonSerializer.Deserialize(ref reader, typeof(Filter<>).MakeGenericType(type), options) as IFilter;
|
|
|
+ }
|
|
|
+ else if(property == "Columns")
|
|
|
+ {
|
|
|
+ columns = JsonSerializer.Deserialize(ref reader, typeof(Columns<>).MakeGenericType(type), options) as IColumns;
|
|
|
+ }
|
|
|
+ else if(property == "Sort")
|
|
|
+ {
|
|
|
+ sort = JsonSerializer.Deserialize(ref reader, typeof(SortOrder<>).MakeGenericType(type), options) as ISortOrder;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ reader.Skip();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if(type is null)
|
|
|
+ {
|
|
|
+ throw new JsonException($"'Type' field expected");
|
|
|
+ }
|
|
|
+ var query = (Activator.CreateInstance(typeof(MultiQueryTableQuery<>).MakeGenericType(type)) as IMultiQueryTableQuery)!;
|
|
|
+ query.Filter = filter;
|
|
|
+ query.Columns = columns;
|
|
|
+ query.Sort = sort;
|
|
|
+ return query;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override MultiQueryRequest? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
|
+ {
|
|
|
+ if (reader.TokenType != JsonTokenType.StartObject)
|
|
|
+ {
|
|
|
+ throw new JsonException();
|
|
|
+ }
|
|
|
+
|
|
|
+ var multiQuery = new MultiQueryRequest();
|
|
|
+ ReadObject(ref reader, (ref Utf8JsonReader reader, string property) =>
|
|
|
+ {
|
|
|
+ if (property == "Queries")
|
|
|
+ {
|
|
|
+ ReadObject(ref reader, (ref Utf8JsonReader reader, string key) =>
|
|
|
+ {
|
|
|
+ multiQuery.Queries[key] = ReadQuery(ref reader, options);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ else if (property == "Credentials")
|
|
|
+ {
|
|
|
+ multiQuery.Credentials = JsonSerializer.Deserialize<Credentials>(ref reader, options) ?? new Credentials();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ reader.Skip();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return multiQuery;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void Write(Utf8JsonWriter writer, MultiQueryRequest value, JsonSerializerOptions options)
|
|
|
+ {
|
|
|
+ writer.WriteStartObject();
|
|
|
+
|
|
|
+ writer.WritePropertyName("Credentials");
|
|
|
+ JsonSerializer.Serialize(writer, value.Credentials, options);
|
|
|
+
|
|
|
+ writer.WriteStartObject("Queries");
|
|
|
+ foreach(var (key, query) in value.Queries)
|
|
|
+ {
|
|
|
+ writer.WritePropertyName(key);
|
|
|
+ JsonSerializer.Serialize(writer, query, options);
|
|
|
+ }
|
|
|
+ writer.WriteEndObject();
|
|
|
+
|
|
|
+ writer.WriteEndObject();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public class MultiQueryResponse : Response, ISerializeBinary
|
|
|
{
|
|
|
public MultiQueryResponse()
|