Configure JsonSerializationOptions for WASM Interop in Blazor?

I need to configure JSON serialization in Blazor wasm. I want to use/provide custom JsonContext.. specifically its type info
51 Replies
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
I thought it was clear. It's obvious that I'm asking some extension method on DI service collection to configure the way Blazor receives objects when invokes js functions. I'm asking if there's a way to provide JsonTypeInfo to use source generated version and optimize interop calls
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
why not?? Instead of controller, wouldn't be nicer to have some configuration for wasm di services? like:
var combinedResolver = new DefaultJsonTypeInfoResolver()
.WithAddedModifier(MyTypeJsonContext.Default.TypeInfoResolver)
.WithAddedModifier(ThirdPartyJsonContext.Default.TypeInfoResolver);

builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.TypeInfoResolver = combinedResolver;
});
var combinedResolver = new DefaultJsonTypeInfoResolver()
.WithAddedModifier(MyTypeJsonContext.Default.TypeInfoResolver)
.WithAddedModifier(ThirdPartyJsonContext.Default.TypeInfoResolver);

builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.TypeInfoResolver = combinedResolver;
});
Are you telling me that something like this doesn't exists?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
I know, I just asked for something similar, in order to optimize JSInterop in blazor using source generated and not runtime serializer Since it's something that I might do regular, I think it should be something provided by author I guess
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
JSInterop should use something generic to "pass" params to JS invokes
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
no man, I don't know how microsoft does to pass objects to JSRuntime.InvokeAsync
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
but I guess theres a serialization in the middle that's sounds awful
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
I though of JsonSerializationContext inheritance even thus, how would I use it in JSRuntime.InvokeAsync?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
I was requested to create a wrapper for a js datagrid component
No description
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
it's private
MODiX
MODiX3w ago
TeBeCo
like a dotnet new blazor-wasm + example of 3-4 calls that have the issue
React with ❌ to remove this embed.
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
this is the critical part
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
man, I was given a private repo, extract that would mean to provide license for example?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
the portion of code in the picture is all about
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
I'll take a look wait
SleepWellPupper
You can use the JsonConverter annotation
Pedro Gil Mora
Pedro Gil MoraOP3w ago
I see
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
it's fixed serialization by MS
SleepWellPupper
yes for back compat reasons but you can customize your own types No worries I'll gladly google the docs for you :)
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
mmmm... like the attribute?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
SleepWellPupper
yes
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
so, it would be over the collection type I'll try and see
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
you know what? I completely forgot that How didn't I thought of this?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Pedro Gil Mora
Pedro Gil MoraOP3w ago
Yeah, I've seen it, didn't remember since I was flawed to using source generators instead of the variants that already existed and I didn't know they could work. my bad thanks any other advises would be highly appreciated @SleepWellPupper @TeBeCo
SleepWellPupper
Is it possible to combine custom converter annotations with SG'd serialization?
Pedro Gil Mora
Pedro Gil MoraOP3w ago
I would love it if possible
SleepWellPupper
Is your only concern to avoid reflection when invoking functions you control? I encourage you to $tias as I will be unavailable for a bit. I'd be interested as well.
Pedro Gil Mora
Pedro Gil MoraOP3w ago
yeah. I'm thinking of: it's a datagrid control, I would love to be @foreach over rows, but not like that. JS takes all the configuration, even rows instead (maybe overthinking or overengineering) I prefer to reduce the amount of memory being used. Eg: a 10000 rows makes it slow to pass for jsInvoke. I suggested to allow HTML define table rows. but they don't use tables.. 😕 good part it's just first time until next time source gets refreshed. Meaning, manipulate data in the datagrid control is managed by a js lib. but refreshing that datasource with JSInterop I perceive a cost
SleepWellPupper
ASP.NET Core Razor component virtualization
Learn how to use component virtualization in ASP.NET Core Blazor apps.
ASP.NET Core Blazor QuickGrid component
The QuickGrid component is a Razor component for quickly and efficiently displaying data in tabular form.
Pedro Gil Mora
Pedro Gil MoraOP3w ago
virtualization good, quickgrid component..mmm client wants his own js lib wrapper First thing I proposed was to use QuickGrid he kicked it out hit it hard for homerun, like we Cubans say
SleepWellPupper
a shame oh well

Did you find this page helpful?