Skip to content

Commit

Permalink
v3.8.1 (#86)
Browse files Browse the repository at this point in the history
* Fix ReferenceData serialization.

* Correct test.
  • Loading branch information
chullybun authored Jan 7, 2024
1 parent 5eeb7ac commit 127bfa3
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Represents the **NuGet** versions.

## v3.8.1
- *Fixed*: The `CoreEx.Text.JsonSerializer` has been updated to cache the _indented_ option correctly.
- *Fixed*: The `ReferenceDataOrchestator` updated to use the correct serializer for `ETag` generation.

## v3.8.0
- *Enhancement*: The `ValueContentResult.CreateResult` has been updated to return the resulting value as-is where is an instance of `IActionResult`; otherwise, converts `value` to a `ValueContentResult` (previous behavior).
- *Enhancement*: The `PagingArgs` has been extended to support `Token`; being a continuation token to enable paging to be performed where the underlying data source does not support skip/take-style paging.
Expand Down
2 changes: 1 addition & 1 deletion Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>3.8.0</Version>
<Version>3.8.1</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
3 changes: 3 additions & 0 deletions src/CoreEx/Entities/IIdentifier.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx

using System;
using System.Text.Json.Serialization;

namespace CoreEx.Entities
{
Expand All @@ -17,9 +18,11 @@ public interface IIdentifier : IEntityKey
/// <summary>
/// Gets the <see cref="Id"/> <see cref="Type"/>.
/// </summary>
[JsonIgnore]
Type IdType { get; }

/// <inheritdoc/>
[JsonIgnore]
CompositeKey IEntityKey.EntityKey => new(Id);
}
}
2 changes: 2 additions & 0 deletions src/CoreEx/Entities/IIdentifierT.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx

using System;
using System.Text.Json.Serialization;

namespace CoreEx.Entities
{
Expand All @@ -21,6 +22,7 @@ public interface IIdentifier<TId> : IIdentifier where TId : IComparable<TId>, IE
new TId? Id { get; set; }

/// <inheritdoc/>
[JsonIgnore]
Type IIdentifier.IdType => typeof(TId);
}
}
3 changes: 0 additions & 3 deletions src/CoreEx/RefData/Extended/ReferenceDataBaseEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public ReferenceDataBaseEx()
throw new InvalidOperationException($"A Reference Data {nameof(Id)} can only be of type {nameof(Int32)}, {nameof(Int64)}, {nameof(String)} or {nameof(Guid)}.");
}

/// <inheritdoc/>
Type IIdentifier.IdType => typeof(TId);

/// <inheritdoc/>
object? IIdentifier.Id { get => Id; set => Id = (TId)value!; }

Expand Down
2 changes: 1 addition & 1 deletion src/CoreEx/RefData/ReferenceDataOrchestrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private async Task<IReferenceDataCollection> GetByTypeInternalAsync(ReferenceDat
var sw = Stopwatch.StartNew();
var provider = (IReferenceDataProvider)scope.ServiceProvider.GetRequiredService(providerType);
var coll = (await provider.GetAsync(type, cancellationToken).ConfigureAwait(false)).Value;
coll.ETag = ETagGenerator.Generate(ServiceProvider.GetRequiredService<IJsonSerializer>(), coll)!;
coll.ETag = ETagGenerator.Generate(ServiceProvider.GetRequiredService<IReferenceDataContentJsonSerializer>(), coll)!;
sw.Stop();

Logger.LogInformation("Reference data type {RefDataType} cache load finish: {ItemCount} items cached [{Elapsed}ms]", type.FullName, coll.Count, sw.Elapsed.TotalMilliseconds);
Expand Down
24 changes: 18 additions & 6 deletions src/CoreEx/Text/Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ namespace CoreEx.Text.Json
/// <summary>
/// Provides the <see cref="Stj.JsonSerializer"/> encapsulated implementation.
/// </summary>
/// <param name="options">The <see cref="Stj.JsonSerializerOptions"/>. Defaults to <see cref="DefaultOptions"/>.</param>
public class JsonSerializer(Stj.JsonSerializerOptions? options = null) : IJsonSerializer
public class JsonSerializer : IJsonSerializer
{
private Stj.JsonSerializerOptions? _indentedOptions;

/// <summary>
/// Gets or sets the default <see cref="Stj.JsonSerializerOptions"/>.
/// </summary>
Expand All @@ -40,6 +37,16 @@ public class JsonSerializer(Stj.JsonSerializerOptions? options = null) : IJsonSe
Converters = { new JsonStringEnumConverter(), new ExceptionConverterFactory(), new ReferenceDataConverterFactory(), new CollectionResultConverterFactory(), new ResultConverterFactory() }
};

/// <summary>
/// Initializes a new instance of the <see cref="JsonSerializer"/> class.
/// </summary>
/// <param name="options">The <see cref="Stj.JsonSerializerOptions"/>. Defaults to <see cref="DefaultOptions"/>.</param>
public JsonSerializer(Stj.JsonSerializerOptions? options = null)
{
Options = options ?? DefaultOptions;
IndentedOptions = new Stj.JsonSerializerOptions(Options) { WriteIndented = true };
}

/// <summary>
/// Gets the underlying serializer configuration settings/options.
/// </summary>
Expand All @@ -48,14 +55,19 @@ public class JsonSerializer(Stj.JsonSerializerOptions? options = null) : IJsonSe
/// <summary>
/// Gets the <see cref="Stj.JsonSerializerOptions"/>.
/// </summary>
public Stj.JsonSerializerOptions Options { get; } = options ?? DefaultOptions;
public Stj.JsonSerializerOptions Options { get; }

/// <summary>
/// Gets or sets the <see cref="Stj.JsonSerializerOptions"/> with <see cref="Stj.JsonSerializerOptions.WriteIndented"/> = <c>true</c>.
/// </summary>
public Stj.JsonSerializerOptions? IndentedOptions { get; }

/// <inheritdoc/>
public string Serialize<T>(T value, JsonWriteFormat? format = null) => SerializeToBinaryData(value, format).ToString();

/// <inheritdoc/>
public BinaryData SerializeToBinaryData<T>(T value, JsonWriteFormat? format = null)
=> new(Stj.JsonSerializer.SerializeToUtf8Bytes(value, format == null ? Options : (_indentedOptions ??= new Stj.JsonSerializerOptions(Options) { WriteIndented = format.Value == JsonWriteFormat.Indented })));
=> new(Stj.JsonSerializer.SerializeToUtf8Bytes(value, format == null || format.Value == JsonWriteFormat.None ? Options : IndentedOptions));

/// <inheritdoc/>
public object? Deserialize(string json) => Stj.JsonSerializer.Deserialize<dynamic>(json, Options);
Expand Down

0 comments on commit 127bfa3

Please sign in to comment.