From c9fb9f65f8c92fec9a2f9ea9f162985d92d0cda2 Mon Sep 17 00:00:00 2001 From: mergehez Date: Sun, 13 Oct 2024 16:56:45 +0200 Subject: [PATCH] Changed JSON config in InertiaOptions --- InertiaNetCore/Models/InertiaOptions.cs | 49 +++++++++++++++++-------- InertiaNetCore/Response.cs | 8 ++-- InertiaNetCore/ResponseFactory.cs | 2 +- InertiaNetCore/Ssr/SsrGateway.cs | 2 +- README.md | 5 +-- 5 files changed, 41 insertions(+), 25 deletions(-) diff --git a/InertiaNetCore/Models/InertiaOptions.cs b/InertiaNetCore/Models/InertiaOptions.cs index db7a14c..acb9d9d 100644 --- a/InertiaNetCore/Models/InertiaOptions.cs +++ b/InertiaNetCore/Models/InertiaOptions.cs @@ -4,28 +4,47 @@ namespace InertiaNetCore.Models; -public class InertiaOptions +public record InertiaJsonOptions { - public string RootView { get; set; } = "~/Views/App.cshtml"; + private InertiaJsonOptions() + { + } - public bool SsrEnabled { get; set; } = false; - public string SsrUrl { get; set; } = "http://127.0.0.1:13714/render"; - - public Action ConfigureSession { get; set; } = _ => { }; - - public bool EncryptHistory { get; set; } - - private static JsonSerializerOptions DefaultJsonSerializerOptions { get; } = new() + internal object Options { get; private init; } = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, ReferenceHandler = ReferenceHandler.IgnoreCycles, DictionaryKeyPolicy = JsonNamingPolicy.CamelCase }; - public object JsonSerializerOptions { get; set; } = DefaultJsonSerializerOptions; - - public Func JsonSerializeFn { get; set; } = (model, o) => + private Func SerializeFn { get; init; } = (model, options) + => JsonSerializer.Serialize(model, options as JsonSerializerOptions); + + internal string Serialize(object model) => SerializeFn(model, Options); + + public static InertiaJsonOptions Default { get; } = new(); + + public static InertiaJsonOptions Create(T options, Func? serialize = null) where T : class { - return JsonSerializer.Serialize(model, o.JsonSerializerOptions as JsonSerializerOptions); - }; + serialize ??= (model, jo) => JsonSerializer.Serialize(model, jo as JsonSerializerOptions); + return new InertiaJsonOptions + { + Options = options, + SerializeFn = (model, jo) => serialize(model, (jo as T)!) + }; + } } + +public class InertiaOptions +{ + public string RootView { get; set; } = "~/Views/App.cshtml"; + + public bool SsrEnabled { get; set; } = false; + public string SsrUrl { get; set; } = "http://127.0.0.1:13714/render"; + + public Action ConfigureSession { get; set; } = _ => { }; + + public bool EncryptHistory { get; set; } + + public InertiaJsonOptions Json { get; set; } = InertiaJsonOptions.Default; +} \ No newline at end of file diff --git a/InertiaNetCore/Response.cs b/InertiaNetCore/Response.cs index 1fa45cd..82b5849 100644 --- a/InertiaNetCore/Response.cs +++ b/InertiaNetCore/Response.cs @@ -50,7 +50,7 @@ public async Task ExecuteResultAsync(ActionContext context) context.HttpContext.Response.Headers.Append("Vary", "Accept"); context.HttpContext.Response.StatusCode = 200; - var jsonResult = new JsonResult(page, _options.JsonSerializerOptions); + var jsonResult = new JsonResult(page, _options.Json.Options); await jsonResult.ExecuteResultAsync(context); } } @@ -92,8 +92,7 @@ private Dictionary> GetDeferredProps(ActionContext context) } // apply json serialization options to dictionary keys before grouping them - var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions; - tmp = JsonSerializer.Deserialize>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions); + tmp = JsonSerializer.Deserialize>(_options.Json.Serialize(tmp)); return tmp! .GroupBy(prop => prop.Value) @@ -116,8 +115,7 @@ private List GetMergeProps(ActionContext context) } // apply json serialization options to dictionary keys before grouping them - var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions; - tmp = JsonSerializer.Deserialize>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions); + tmp = JsonSerializer.Deserialize>(_options.Json.Serialize(tmp)); return tmp!.Select(prop => prop.Key).ToList(); } diff --git a/InertiaNetCore/ResponseFactory.cs b/InertiaNetCore/ResponseFactory.cs index 60d6163..786fd5b 100644 --- a/InertiaNetCore/ResponseFactory.cs +++ b/InertiaNetCore/ResponseFactory.cs @@ -56,7 +56,7 @@ public async Task Html(dynamic model) } } - var data = options.Value.JsonSerializeFn(model, options.Value); + var data = options.Value.Json.Serialize(model); var encoded = WebUtility.HtmlEncode(data); return new HtmlString($"
"); diff --git a/InertiaNetCore/Ssr/SsrGateway.cs b/InertiaNetCore/Ssr/SsrGateway.cs index 4643763..b48a4a8 100644 --- a/InertiaNetCore/Ssr/SsrGateway.cs +++ b/InertiaNetCore/Ssr/SsrGateway.cs @@ -9,7 +9,7 @@ internal class SsrGateway(IHttpClientFactory httpClientFactory, IOptions Dispatch(dynamic model, string url) { - var json = options.Value.JsonSerializeFn(model, options.Value); + var json = options.Value.Json.Serialize(model); var content = new StringContent(json.ToString(), Encoding.UTF8, "application/json"); diff --git a/README.md b/README.md index a796d8e..391e652 100644 --- a/README.md +++ b/README.md @@ -70,13 +70,12 @@ For example, you can change JSON serializer settings to use `Newtonsoft.Json` in ```csharp builder.Services.AddInertia(options => { - options.JsonSerializerSettings = new JsonSerializerSettings + var options = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), ReferenceLoopHandling = ReferenceLoopHandling.Ignore, }; - - options.JsonSerializeFn = model => JsonConvert.SerializeObject(model, options.JsonSerializerSettings); + o.Json = InertiaJsonOptions.Create(options); // there is also an optional parameter to customize the "Serialize" method }); ```