SaaS CMS has officially launched! Learn more now.

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



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".

Edited, Mar 20, 2024 14:21

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 -

 .AddJsonOptions(o => o.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString);
Mar 21, 2024 2:46

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()?)

Edited, Mar 21, 2024 8:01

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

Mar 21, 2024 8:55

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 (+/-).

Mar 21, 2024 10:10

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.

Mar 21, 2024 21:56

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.

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.