Skip to content

Commit

Permalink
Add QueryJsonConverter
Browse files Browse the repository at this point in the history
Fix #16372
  • Loading branch information
MikeAlhayek committed Jun 27, 2024
1 parent 51eb7b8 commit 6b33462
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Queries/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Deployment;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.Json;
using OrchardCore.Liquid;
using OrchardCore.Modules;
using OrchardCore.Navigation;
Expand Down Expand Up @@ -44,6 +45,11 @@ public override void ConfigureServices(IServiceCollection services)
});
})
.AddLiquidFilter<QueryFilter>("query");

services.Configure<DocumentJsonSerializerOptions>(options =>
{
options.SerializerOptions.Converters.Add(QueryJsonConverter.Instance);
});
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/OrchardCore/OrchardCore.Queries.Abstractions/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ protected Query(string source)
Source = source;
}

internal Query(string source, string name, string schema)
: this(source)
{
Name = name;
Schema = schema;
}

/// <summary>
/// Gets or sets the technical name of the query.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OrchardCore.Queries;

public sealed class QueryJsonConverter : JsonConverter<Query>
{
public static readonly QueryJsonConverter Instance = new();

public override Query Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string source = null;
string name = null;
string schema = null;

while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}

if (reader.TokenType == JsonTokenType.PropertyName)
{
var propertyName = reader.GetString();
reader.Read();

switch (propertyName)
{
case nameof(Query.Source):
source = reader.GetString();
break;
case nameof(Query.Name):
name = reader.GetString();
break;
case nameof(Query.Schema):
schema = reader.GetString();
break;
default:
break;
}
}
}

return new Query(source, name, schema);
}

public override void Write(Utf8JsonWriter writer, Query value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(nameof(Query.Source), value.Source);
writer.WriteString(nameof(Query.Name), value.Name);
writer.WriteString(nameof(Query.Schema), value.Schema);
writer.WriteEndObject();
}
}

0 comments on commit 6b33462

Please sign in to comment.