Try our conversational search powered by Generative AI!

ContentApiModel json formatted incorrectly

Vote:
 

We have a weird issue with formatting of ContentApiModel to JSON.

// Startup.cs
services.UseNewtonsoftSerialization(assembly, settings =>
{
	settings.Converters.Add(new StringEnumConverter(new DefaultNamingStrategy()));
	settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
	settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml;
	settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
...
services.AddCms();
...
// api controller, _contentApiOptions is injected in constructor

[HttpGet]
[Route("{contentLinkId:int?}")]
public IActionResult GetAll([FromRoute] int? contentLinkId = null)
{
	var content = new List<ContentApiModel>();
	var language = LanguageSelector.AutoDetect(true).Language;
	foreach (var page in GetAllResellers(contentLinkId))
	{
		var model = _contentConverter.Convert(page, new ConverterContext(page.ContentLink, language, _contentApiOptions, 
			ContextMode.Default, null, "*", false));
		content.Add(model);
	}

	return new JsonFormattedResult(content);
}

Resulting JSON, some properties are propper CamelCase - some are not.

Any ideas?

#306871
Aug 16, 2023 7:57
Carl S - Aug 16, 2023 8:05
All the custom added page properties is not proper camelcase.
Carl S - Aug 16, 2023 12:20
Are you supposed to use something else than 'DefaultContentConverter' or 'JsonFormattedResult' to get it to format correctly.
Vote:
 

Try setting this way, I think content delivery uses its own settings.

 services.ConfigureContentDeliveryApiSerializer(settings =>
            {
                settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            });
#306895
Aug 16, 2023 14:33
Vote:
 

Interesting. Did not know about that configuration option - the content delivery api response is however is correct.

This is a custom api controller. - The answer after a long search was this.

Use the DefaultContractResolver, with a CamelCaseNamingStrategy and all ctor parameters to 'true'.

#306941
Edited, Aug 17, 2023 4:23
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.