How do I make the web api return camel case in Umbraco 9?
I'm trying to port the umbraco 8 solution for this to umbraco 9, but I haven't had any luck finding out what I need to do to apply the serialization update. IControllerConfiguration doesn't seem to exist anymore and all the serialization examples I've found are for changing it globally or directly on the returned data via
return new JsonResult(data, new JsonSerializerOptions {
PropertyNamingPolicy = ...
});
Any direction on how to go about this would be much appreciated.
.. was it turns out about to write the same attribute the class answer as lasttime (its still true)
I am not sure if you can use it direct, but Umbraco has a [JsonCamelCaseFormatter] attribute which you can place on the methods/class
e.g
[JsonCamelCaseFormatter]
public IActionResult MyMethodThatReturnsSomething()
if for some reason you need to do that yourself (the Umbraco one does do something Angulary with its own version - not sure if that will effect things)
public class MyCamelCaseJsonFormatterAttribute : TypeFilterAttribute
{
public MyCamelCaseJsonFormatterAttribute () :
base(typeof(MyCamelCaseJsonFormatterFilter))
{
Order = 2;
}
}
public class MyCamelCaseJsonFormatterFilter : IResultFilter
{
private readonly ArrayPool<char> _arrayPool;
private readonly IOptions<MvcOptions> _options;
public MyCamelCaseJsonFormatterFilter (ArrayPool<char> arrayPool, IOptions<MvcOptions> options)
{
_arrayPool = arrayPool;
_options = options;
}
public void OnResultExecuted(ResultExecutedContext context)
{ }
public void OnResultExecuting(ResultExecutingContext context)
{
if (context.Result is ObjectResult objectResult)
{
var serializerSettings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
objectResult.Formatters.Clear();
objectResult.Formatters.Add(new NewtonsoftJsonOutputFormatter(serializerSettings, _arrayPool, _options.Value));
}
}
}
Looks like JsonCamelCaseFormatter does what I want, but good to know how to do it from scratch if needed. I had found the IResultFilter interface yesterday, but didn't know how to attach the JsonSerializerSettings data to the context.
How do I make the web api return camel case in Umbraco 9?
I'm trying to port the umbraco 8 solution for this to umbraco 9, but I haven't had any luck finding out what I need to do to apply the serialization update. IControllerConfiguration doesn't seem to exist anymore and all the serialization examples I've found are for changing it globally or directly on the returned data via
Any direction on how to go about this would be much appreciated.
Hi
.. was it turns out about to write the same attribute the class answer as lasttime (its still true)
I am not sure if you can use it direct, but Umbraco has a
[JsonCamelCaseFormatter]
attribute which you can place on the methods/classe.g
if for some reason you need to do that yourself (the Umbraco one does do something Angulary with its own version - not sure if that will effect things)
Thanks for the information. It was very helpful.
Looks like JsonCamelCaseFormatter does what I want, but good to know how to do it from scratch if needed. I had found the IResultFilter interface yesterday, but didn't know how to attach the JsonSerializerSettings data to the context.
is working on a reply...