Skip to content

Commit

Permalink
Merge pull request #159 from Lombiq/issue/OSOE-795
Browse files Browse the repository at this point in the history
OSOE-795: Upgrade to latest OC preview to test System.Text.Json
  • Loading branch information
dministro authored May 15, 2024
2 parents 4a503d8 + 9384469 commit 32f9db9
Show file tree
Hide file tree
Showing 29 changed files with 431 additions and 302 deletions.
4 changes: 2 additions & 2 deletions Lombiq.DataTables.Samples/Lombiq.DataTables.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.ContentFields" Version="1.8.2" />
<PackageReference Include="OrchardCore.Module.Targets" Version="1.8.2" />
<PackageReference Include="OrchardCore.ContentFields" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18200" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 5 additions & 7 deletions Lombiq.DataTables/Controllers/Api/RowsController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Lombiq.DataTables.Models;
using Lombiq.DataTables.Services;
using Lombiq.HelpfulLibraries.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -32,7 +32,7 @@ public RowsController(
/// <summary>
/// Gets the current table view's rows.
/// </summary>
/// <param name="requestJson">The request to fulfill serialized as JSON.</param>
/// <param name="request">The request to fulfill serialized as JSON.</param>
/// <returns>The response for this API call.</returns>
/// <remarks>
/// <list type="bullet">
Expand All @@ -53,11 +53,10 @@ public RowsController(
/// </remarks>
[IgnoreAntiforgeryToken]
[HttpGet]
public async Task<ActionResult<DataTableDataResponse>> Get(string requestJson)
public async Task<ActionResult<DataTableDataResponse>> Get([FromJsonQueryString(Name = "requestJson")] DataTableDataRequest request)
{
if (string.IsNullOrEmpty(requestJson)) return BadRequest();
if (request == null) return BadRequest();

var request = JsonConvert.DeserializeObject<DataTableDataRequest>(requestJson);
var dataProvider = _dataTableDataProviderAccessor.GetDataProvider(request.DataProvider);
if (dataProvider == null)
{
Expand Down Expand Up @@ -86,11 +85,10 @@ public async Task<ActionResult<DataTableDataResponse>> Get(string requestJson)
}

public async Task<ActionResult<DataTableDataResponse>> Export(
string requestJson,
[FromJsonQueryString(Name = "requestJson")] DataTableDataRequest request,
string name = null,
bool exportAll = true)
{
var request = JsonConvert.DeserializeObject<DataTableDataRequest>(requestJson);
if (exportAll)
{
request.Start = 0;
Expand Down
9 changes: 5 additions & 4 deletions Lombiq.DataTables/Controllers/TableController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Lombiq.DataTables.Services;
using Lombiq.DataTables.ViewModels;
using Lombiq.HelpfulLibraries.OrchardCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using OrchardCore.Admin;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace Lombiq.DataTables.Controllers;
Expand All @@ -18,12 +17,14 @@ public class TableController : Controller
public TableController(IEnumerable<IDataTableDataProvider> dataTableDataProviders) =>
_dataTableDataProviders = dataTableDataProviders;

[AdminRoute("DataTable/{providerName}/{queryId?}")]
[Admin("DataTable/{providerName}/{queryId?}")]
public async Task<IActionResult> Get(string providerName, string queryId = null, bool paging = true, bool viewAction = false)
{
var provider = _dataTableDataProviders.Single(provider => provider.Name == providerName);
if (string.IsNullOrEmpty(queryId)) queryId = providerName;
var definition = new DataTableDefinitionViewModel(JObject.FromObject(new { paging, viewAction }))

var additionalDatatableOptions = JObject.FromObject(new { paging, viewAction })!.AsObject();
var definition = new DataTableDefinitionViewModel(additionalDatatableOptions)
{
DataProvider = providerName,
QueryId = queryId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using OrchardCore.Mvc.Core.Utilities;
using OrchardCore.Security.Permissions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using static Lombiq.DataTables.Constants.SortingDirection;

Expand Down Expand Up @@ -167,7 +167,7 @@ public static string GetCustomActionsJson(
IHttpContextAccessor hca,
LinkGenerator linkGenerator,
IStringLocalizer<ActionsDescriptor> actionsStringLocalizer) =>
JsonConvert.SerializeObject(GetCustomActions(
JsonSerializer.Serialize(GetCustomActions(
dataProvider,
contentItemId,
canDelete,
Expand Down
11 changes: 11 additions & 0 deletions Lombiq.DataTables/Extensions/JsonNodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace System.Text.Json.Nodes;

public static class JsonNodeExtensions
{
/// <summary>
/// Checks if the provided <paramref name="node"/> is object and has a <c>Type</c> property, and if its value
/// matches <typeparamref name="T"/>.
/// </summary>
public static bool HasMatchingTypeProperty<T>(this JsonNode node) =>
node is JsonObject jsonObject && jsonObject["Type"]?.ToString() == typeof(T).Name;
}
4 changes: 2 additions & 2 deletions Lombiq.DataTables/Liquid/ActionsLiquidFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentManagement;
using OrchardCore.DisplayManagement;
using OrchardCore.Liquid;
using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace Lombiq.DataTables.Liquid;
Expand Down Expand Up @@ -71,7 +71,7 @@ public ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments argu
FluidValues.Object => input!.ToObjectValue() switch
{
ActionsDescriptor model => FromObjectAsync(model, title, returnUrl),
JToken jToken => FromObjectAsync(jToken.ToObject<ActionsDescriptor>(), title, returnUrl),
JsonNode jsonNode => FromObjectAsync(jsonNode.ToObject<ActionsDescriptor>(), title, returnUrl),
{ } unknown => throw GetException(unknown),
_ => throw new ArgumentNullException(nameof(input)),
},
Expand Down
7 changes: 3 additions & 4 deletions Lombiq.DataTables/Lombiq.DataTables.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@

<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.102.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Nito.AsyncEx.Tasks" Version="5.1.2" />
<PackageReference Include="OrchardCore.Contents" Version="1.8.2" />
<PackageReference Include="OrchardCore.Module.Targets" Version="1.8.2" />
<PackageReference Include="OrchardCore.Search.Lucene" Version="1.8.2" />
<PackageReference Include="OrchardCore.Contents" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.Search.Lucene" Version="2.0.0-preview-18200" />
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' != 'true'">
Expand Down
7 changes: 4 additions & 3 deletions Lombiq.DataTables/Models/DataTableChildRowResponse.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableChildRowResponse
{
[JsonPropertyName("error")]
public string Error { get; set; }

[JsonPropertyName("content")]
public string Content { get; set; }

public static DataTableChildRowResponse ErrorResult(string errorText) => new() { Error = errorText };
Expand Down
13 changes: 10 additions & 3 deletions Lombiq.DataTables/Models/DataTableColumn.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableColumn
{
[JsonPropertyName("data")]
public string Data { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("searchable")]
public bool Searchable { get; set; }

[JsonPropertyName("orderable")]
public bool Orderable { get; set; }

[JsonPropertyName("search")]
public DataTableSearchParameters Search { get; set; }
}
3 changes: 0 additions & 3 deletions Lombiq.DataTables/Models/DataTableDataRequest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.Linq;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableDataRequest
{
public string QueryId { get; set; }
Expand Down
11 changes: 7 additions & 4 deletions Lombiq.DataTables/Models/DataTableDataResponse.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableDataResponse
{
/// <summary>
Expand All @@ -15,28 +13,33 @@ public class DataTableDataResponse
/// <remarks>
/// <para>For internal use only. It's overwritten during normal use.</para>
/// </remarks>
[JsonProperty]
[JsonPropertyName("draw")]
[JsonInclude]
internal int Draw { get; set; }

/// <summary>
/// Gets or sets the extra informational field that shows the actual total if filtering (such as keyword
/// search) is used. When not filtering it must be the same as <see cref="RecordsFiltered"/>.
/// </summary>
[JsonPropertyName("recordsTotal")]
public int RecordsTotal { get; set; }

/// <summary>
/// Gets or sets the total number of results; used for paging.
/// </summary>
[JsonPropertyName("recordsFiltered")]
public int RecordsFiltered { get; set; }

/// <summary>
/// Gets or sets the table contents of the current page.
/// </summary>
[JsonPropertyName("data")]
public IEnumerable<DataTableRow> Data { get; set; }

/// <summary>
/// Gets or sets the user-facing error message in case something went wrong.
/// </summary>
[JsonPropertyName("error")]
public string Error { get; set; }

/// <summary>
Expand Down
16 changes: 13 additions & 3 deletions Lombiq.DataTables/Models/DataTableOrder.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
using Lombiq.DataTables.Constants;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableOrder
{
public string Column { get; set; }

[JsonIgnore]
public SortingDirection Direction { get; set; }

[JsonPropertyName("direction")]
[JsonInclude]
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "It's used for JSON conversion.")]
private string DirectionString
{
get => IsAscending ? "ascending" : "descending";
set => Direction = value == "descending" ? SortingDirection.Descending : SortingDirection.Ascending;
}

[JsonIgnore]
public bool IsAscending => Direction == SortingDirection.Ascending;
}
36 changes: 26 additions & 10 deletions Lombiq.DataTables/Models/DataTableRow.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableRow
{
[JsonExtensionData]
internal IDictionary<string, JToken> ValuesDictionary { get; set; }
[JsonInclude]
[JsonPropertyName("valuesDictionary")]
internal IDictionary<string, object> ValuesDictionary { get; set; } = new Dictionary<string, object>();

[JsonPropertyName("id")]
public int Id { get; set; }

public string this[string name]
{
get => ValuesDictionary.TryGetValue(name, out var value) ? value.Value<string>() : null;
get => ValuesDictionary.GetMaybe(name)?.ToString();
set => ValuesDictionary[name] = value;
}

public DataTableRow() => ValuesDictionary = new Dictionary<string, JToken>();
public DataTableRow() { }

public DataTableRow(int id, IDictionary<string, JToken> valuesDictionary)
public DataTableRow(int id, IDictionary<string, JsonNode> valuesDictionary)
{
Id = id;
ValuesDictionary = valuesDictionary;

if (valuesDictionary != null)
{
foreach (var (key, value) in valuesDictionary)
{
ValuesDictionary[key] = value;
}
}
}

public IEnumerable<string> GetValues() =>
ValuesDictionary.Values.Select(value => value.Value<string>());
ValuesDictionary.Values.Select(value => value.ToString());

public IEnumerable<string> GetValuesOrderedByColumns(IEnumerable<DataTableColumnDefinition> columnDefinitions) =>
columnDefinitions.Select(columnDefinition => this[columnDefinition.Name] ?? string.Empty);

internal JsonNode GetValueAsJsonNode(string name) =>
ValuesDictionary.GetMaybe(name) switch
{
JsonNode node => node,
{ } otherValue => JObject.FromObject(otherValue),
null => null,
};
}
7 changes: 3 additions & 4 deletions Lombiq.DataTables/Models/DataTableSearchParameters.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json.Serialization;

namespace Lombiq.DataTables.Models;

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class DataTableSearchParameters
{
[JsonPropertyName("value")]
public string Value { get; set; }

[JsonProperty(PropertyName = "regex")]
[JsonPropertyName("regex")]
public bool IsRegex { get; set; }
}
9 changes: 5 additions & 4 deletions Lombiq.DataTables/Models/ExportDate.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json.Linq;
using NodaTime;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;

namespace Lombiq.DataTables.Models;

Expand All @@ -21,10 +21,11 @@ public class ExportDate
public int Day { get; set; }
public string ExcelFormat { get; set; }

public static bool IsInstance(JObject jObject) =>
jObject[nameof(Type)]?.ToString() == nameof(ExportDate);
public static bool IsInstance(JsonObject jsonObject) =>
jsonObject.HasMatchingTypeProperty<ExportDate>();

public static string GetText(JObject jObject) => ((LocalDate)jObject.ToObject<ExportDate>()).ToShortDateString();
public static string GetText(JsonObject jsonObject) =>
((LocalDate)jsonObject.ToObject<ExportDate>()).ToShortDateString();

public static implicit operator ExportDate(LocalDate localDate) =>
new()
Expand Down
Loading

0 comments on commit 32f9db9

Please sign in to comment.