Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesse Andrews 191 posts 716 karma points c-trib
    Apr 06, 2022 @ 00:24
    Jesse Andrews
    1

    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.

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Apr 06, 2022 @ 07:27
    Kevin Jump
    100

    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/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));
            }
        }
    }
    
  • Jesse Andrews 191 posts 716 karma points c-trib
    Apr 06, 2022 @ 16:20
    Jesse Andrews
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft