Try our conversational search powered by Generative AI!

Json serialization error when saving block with enum property and integer backing type

Vote:
 

Hi

We have recently upgraded from CMS 11 to 12.

We're getting serialization error when trying to save a block containing an enum.

The backing type is integer.

The easy fix is adding preferredUiJsonSerializerType="Newtonsoft" to module.config.

But the question is: how would a fix this using the Core built-in serializer? I have tried adding a converter in JsonOptions, as a converter and as a converter factory, as well as a service. But the converter is never called? Obviously without the above change to module.config, or set to "Net".

#319256
Edited, Mar 20, 2024 14:21
Vote:
 

Can you share the class defintion for SectionBackground for context?

Possibly a type conversion issue if the enum was originally serialized as a JSON string.

Trying enabling the behaviour in the .NET Core serializer - https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/migrate-from-newtonsoft?pivots=dotnet-9-0#allow-or-write-numbers-in-quotes

services
 .AddMvc()
 .AddJsonOptions(o => o.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString);
#319293
Mar 21, 2024 2:46
Vote:
 

Added the definition in the original post (at the top). It's just a simple enum with two values (None/0, Filled/1).

Changing the option didn't help.

Another thing i forgot to mention: it works when setting the enum field to Filled/1. If set to None/0, it errors. Is it some wierd null-handling/default-value issue?

This is where it errors out, even before I changed the option:

Either the option is on by default or it's ignoring these options at this stage in the UI (it looks like they're adding a bunch of their own converters as services in AddCmsUI()?)

#319302
Edited, Mar 21, 2024 8:01
Vote:
 

What does your editor descriptor do? Enums should work OOTB, no need for custom serialization or editor descriptors.

#319308
Mar 21, 2024 8:55
Vote:
 

Much of this is orignially developed by various agencies over the years, from CMS 6 and on.

But I guess it is there to get a proper selection editor?

If I remove the attribute I get a regular numeric textbox (+/-).

#319312
Mar 21, 2024 10:10
Vote:
 

Likely some custom code which is causing this weird behaviour.

Try changing the code where it errors to use the TryGetInt32 method instead and return default value of 0 if the method returns false. As you say if it only fails when set to None (0), this may be the quickest fix.

#319323
Mar 21, 2024 21:56
Vote:
 

Sorry I wasn't clear, but that bit of code is part of System.Text.Json.Serialization.Converters.Int32Converter, so a bit hard to change. :-)

But I think I have found why this is happening.

It seems the custom enum SelectionFactory renders the options in a somewhat hacky fashion.
As in, if the first value has the name "None", it gets an empty string value.

I guess the Newtonsoft variant just assigns it the default value either way.

#319363
Mar 22, 2024 8:26
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.