Skip to content

Commit

Permalink
Changed JSON config in InertiaOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mergehez committed Oct 13, 2024
1 parent fe3745a commit c9fb9f6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
49 changes: 34 additions & 15 deletions InertiaNetCore/Models/InertiaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SessionOptions> 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<object, InertiaOptions, string> JsonSerializeFn { get; set; } = (model, o) =>
private Func<object, object, string> 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>(T options, Func<object, T, string>? 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<SessionOptions> ConfigureSession { get; set; } = _ => { };

public bool EncryptHistory { get; set; }

public InertiaJsonOptions Json { get; set; } = InertiaJsonOptions.Default;
}
8 changes: 3 additions & 5 deletions InertiaNetCore/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -92,8 +92,7 @@ private Dictionary<string, List<string>> GetDeferredProps(ActionContext context)
}

// apply json serialization options to dictionary keys before grouping them
var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions;
tmp = JsonSerializer.Deserialize<Dictionary<string, string>>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions);
tmp = JsonSerializer.Deserialize<Dictionary<string, string>>(_options.Json.Serialize(tmp));

return tmp!
.GroupBy(prop => prop.Value)
Expand All @@ -116,8 +115,7 @@ private List<string> GetMergeProps(ActionContext context)
}

// apply json serialization options to dictionary keys before grouping them
var jsonOptions = _options.JsonSerializerOptions as JsonSerializerOptions;
tmp = JsonSerializer.Deserialize<Dictionary<string, string>>(JsonSerializer.Serialize(tmp, jsonOptions), jsonOptions);
tmp = JsonSerializer.Deserialize<Dictionary<string, string>>(_options.Json.Serialize(tmp));

return tmp!.Select(prop => prop.Key).ToList();
}
Expand Down
2 changes: 1 addition & 1 deletion InertiaNetCore/ResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<IHtmlContent> 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($"<div id=\"app\" data-page=\"{encoded}\"></div>");
Expand Down
2 changes: 1 addition & 1 deletion InertiaNetCore/Ssr/SsrGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class SsrGateway(IHttpClientFactory httpClientFactory, IOptions<Inertia
{
public async Task<SsrResponse?> 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");

Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
```

Expand Down

0 comments on commit c9fb9f6

Please sign in to comment.