Skip to content

Commit

Permalink
Fix MenuWidgetLiquidFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Feb 24, 2024
1 parent d9d5d9c commit 8186703
Showing 1 changed file with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OrchardCore.Liquid;
using OrchardCore.Navigation;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Lombiq.HelpfulExtensions.Extensions.Widgets.Liquid;
Expand Down Expand Up @@ -45,23 +46,18 @@ public ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments argu
localNav = arguments[nameof(localNav)].ToBooleanValue();
classes = arguments[nameof(classes)].ToStringValue();

var converter = new LocalizedStringJsonConverter(T);
var serializer = new JsonSerializer();
serializer.Converters.Add(converter);
var serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(converter);

var serializerOptions = LocalizedStringJsonConverter.Add(T);
var menuItems = input?.Type switch
{
FluidValues.String => JsonConvert.DeserializeObject<IList<MenuItem>>(
FluidValues.String => JsonSerializer.Deserialize<IList<MenuItem>>(
input!.ToStringValue(),
serializerSettings),
serializerOptions),
FluidValues.Object => input!.ToObjectValue() switch
{
IEnumerable<MenuItem> enumerable => enumerable.AsList(),
MenuItem single => new[] { single },
JArray jArray => jArray.ToObject<IList<MenuItem>>(serializer),
JObject jObject => new[] { jObject.ToObject<MenuItem>(serializer) },
JsonArray jsonArray => jsonArray.ToObject<IList<MenuItem>>(serializerOptions),
JsonObject jsonObject => new[] { jsonObject.ToObject<MenuItem>(serializerOptions) },
_ => null,
},
_ => null,
Expand Down Expand Up @@ -102,32 +98,36 @@ public class LocalizedStringJsonConverter : JsonConverter<LocalizedString>
{
private readonly IStringLocalizer T;

public LocalizedStringJsonConverter(IStringLocalizer stringLocalizer) =>
private LocalizedStringJsonConverter(IStringLocalizer stringLocalizer) =>
T = stringLocalizer;

public override void WriteJson(JsonWriter writer, LocalizedString value, JsonSerializer serializer) =>
writer.WriteValue(value?.Value);
public override void Write(Utf8JsonWriter writer, LocalizedString value, JsonSerializerOptions options) =>
writer.WriteStringValue(value?.Value);

[SuppressMessage("Style", "IDE0010:Add missing cases", Justification = "We don't want to handle other token types.")]
public override LocalizedString ReadJson(
JsonReader reader,
Type objectType,
LocalizedString existingValue,
bool hasExistingValue,
JsonSerializer serializer)
public override LocalizedString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonToken.String:
return JToken.ReadFrom(reader).ToObject<string>() is { } text ? T[text] : null;
case JsonToken.StartObject:
case JsonTokenType.String:
return JsonSerializer.Deserialize<string>(ref reader, options) is { } text ? T[text] : null;
case JsonTokenType.StartObject:
var data = new Dictionary<string, string>(
JToken.ReadFrom(reader).ToObject<Dictionary<string, string>>(),
JsonSerializer.Deserialize<Dictionary<string, string>>(ref reader, options),
StringComparer.OrdinalIgnoreCase);
return new LocalizedString(data[nameof(LocalizedString.Name)], data[nameof(LocalizedString.Value)]);
default:
throw new InvalidOperationException("Unable to parse JSON!");
}
}

public static JsonSerializerOptions Add(IStringLocalizer stringLocalizer, JsonSerializerOptions options = null)
{
options ??= new JsonSerializerOptions(JsonSerializerOptions.Default);
options.Converters.RemoveAll(converter => converter is JsonConverter<LocalizedString>);
options.Converters.Add(new LocalizedStringJsonConverter(stringLocalizer));

return options;
}
}
}

0 comments on commit 8186703

Please sign in to comment.