UseNewtonsoftSerialization formats other responses as well

Vote:
 

I want to use UseNewtonsoftSerialization for my custom Api Controllers.

// Startup.cs
services.UseNewtonsoftSerialization(typeof(BaseJsonApiController), settings =>
{
	settings.Converters.Add(new StringEnumConverter(new DefaultNamingStrategy()));
	settings.ContractResolver = new DefaultContractResolver
	{
		NamingStrategy = new CamelCaseNamingStrategy(true, true, true)
	};
	settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml;
	settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

This setting some how also affects loading of form data in backend when viewing form submissions.

/epiui/EPiServer.Forms.UI/Stores/formsdata/?parent=xxx_yyy


If I remove the UseNewtonsoftSerialization the form submissions data starts working correctly again.

Any ideas?

#310684
Oct 12, 2023 6:48
Vote:
 

We had similar issues at our end too when we added some serializer settings to startup, it was interfering with Optimizely forms. To address this we have removed serializer settings from startup which fixes the Optimizely forms.

We then have a custom result object class inheriting JsonResult where we specify the serializer settings and then use that as part of action method. 

You can customize this class per your need. Then use CustomJsonSerializerResult  instead of JsonResult as a return type.

    public class CustomJsonSerializerResult : JsonResult
    {
        public JsonNetResult(object? value) : base(value)
        {
            Settings = new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };
        }        

        public JsonSerializerSettings Settings { get; private set; }

        public override void ExecuteResult(ActionContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");          

            HttpResponse response = context.HttpContext.Response;
            response.ContentType =
                string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
                      
            if (this.Value == null)
                return;

            var scriptSerializer = JsonSerializer.Create(this.Settings);

            using (var sw = new StringWriter())
            {
                scriptSerializer.Serialize(sw, this.Value);
                response.WriteAsync(sw.ToString());
            }
        }
    }

#310706
Oct 12, 2023 13:30
Vote:
 

I ended up removing the 'UseNewtonsoftSerialization' and just changing our 'react'-frontend to handle enums as ints instead.

That was the only main change that affected us.

I still needed to use the [JsonFormatter] attribute and the 'JsonFormattedResult' to format the json input/output properly.

#310766
Oct 13, 2023 6:17
Vote:
 

Do note that creating a new JsonSerializerSettings object every time might affect performance, so better create it statically once 👍

#312524
Edited, Nov 14, 2023 10:28
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.