Repository navigation
http: serialize event details like ASP.NET Core JSON responses - #111
Merged
Merged
Conversation
The object overload of TriggerEvent still used the 1.x options: it camel-cased dictionary keys and dropped null properties. Keys are data, such as field names, so Errors["Email"] reached the browser as "email" and "Address.City" as "address.City". The JSON also differed from the JsonTypeInfo example in the docs, which says the payload stays the same. Use JsonSerializerDefaults.Web, which ASP.NET Core uses for JSON responses by default: property names are camel-cased, while dictionary keys and null values are written as is.
Add TriggerEvent(string, object, JsonSerializerOptions, timing). All overloads now write the detail through the same writer, so the header stays compact and escapes non-ASCII characters in string values and dictionary keys whatever the options say.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Event details passed to
TriggerEventas an object were serialized with the 1.x options:dictionary keys were camel-cased and
nullproperties were dropped. Dictionary keys are data,so
Errors["Email"]reached the browser asemail, and the JSON differed from theJsonTypeInfoexample in the docs.JsonSerializerDefaults.Web, as ASP.NET Core does for JSONresponses: property names are camel-cased, while dictionary keys and
nullvalues are writtenas is. The migration guide lists this as a silent change.
TriggerEvent(string, object, JsonSerializerOptions, HtmxTriggerTiming)overload, similar toResults.Json(object, JsonSerializerOptions), for details that must follow the application's JSONoptions. On .NET 6 it is the only way to do that, because
JsonSerializerOptions.GetTypeInfois not public there.
characters in string values and dictionary keys are escaped whatever the options say.