Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeSpan to/from ISO8601 #370

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions GridBlazorOData.Server/Models/NorthwindDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GridBlazorOData.Shared.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace GridBlazorOData.Server.Models
{
Expand All @@ -13,6 +14,9 @@ public NorthwindDbContext(DbContextOptions<NorthwindDbContext> options)

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>()
.Property(s => s.Age)
.HasConversion(new TimeSpanToTicksConverter());
modelBuilder.Entity<OrderDetail>()
.HasKey(c => new { c.OrderID, c.ProductID });

Expand Down
3 changes: 3 additions & 0 deletions GridBlazorOData.Shared/Models/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public Order()
[NotMappedColumn]
[ForeignKey("ShipVia")]
public virtual Shipper Shipper { get; set; }

[GridColumn(Position = 18)]
public TimeSpan? Age { get; set; }
}

}
1 change: 1 addition & 0 deletions GridShared/Utility/JsonSerializerOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static JsonSerializerOptions AddOdataSupport(this JsonSerializerOptions j
}
// required for Blazor WA
jsonOptions.Converters.Add(new ODataDateTimeConverter());
jsonOptions.Converters.Add(new ODataTimeSpanConverter());

converters = jsonOptions.Converters.Where(r => r.CanConvert(typeof(Enum)));
if (converters != null)
Expand Down
25 changes: 25 additions & 0 deletions GridShared/Utility/ODataTimeSpanConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Xml;

namespace GridShared.Utility
{
/// <summary>
/// Converts with TimeSpan to/from ISO8601
/// </summary>
public class ODataTimeSpanConverter : JsonConverter<TimeSpan>
{
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Debug.Assert(typeToConvert == typeof(TimeSpan));
return XmlConvert.ToTimeSpan(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
{
writer.WriteStringValue(XmlConvert.ToString(value));
}
}
}