1

I'm using ASP.NET Boilerplate for one of my application. The application was on 3.5.0 version of Abp and the template was ASP.NET Core with MVC. I recently upgraded it to 3.6.2 the upgrade was fine but when I run it all AJAX calls are returning JSON data in CamelCase now, previously it was in PascalCase. I'm using DefaultContractResolver for JSON configuration

I tried everything that was available in Google and StackOverflow, but no luck. I downloaded the latest ABP template and added configuration there as well but it is not working there as well. Seems like serializer settings are not applying.

Is anybody knows how to fix it?

My code configuration is:

services.AddMvc().AddJsonOptions(x =>
            x.SerializerSettings.ContractResolver = new DefaultContractResolver());

Please let me know if you need more information.

Thank you in advance.

Ram Koti
  • 2,203
  • 7
  • 26
  • 36

2 Answers2

3

Set it in Startup.cs > ConfigureServices() method

services.PostConfigure<MvcJsonOptions>(options =>
{
    options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
  • Thanks @Alper. Worked like a charm. But I don't understand why we need to use post configure. Do you know what they changed? Because it was working fine in previous version with same configuration. – Muhammad Bilal Jun 25 '18 at 04:39
  • By writing it in PostConfigure we ensure that it's not being overridden by the framework. ( I'm not sure but this might have been changed recently ) – Alper Ebicoglu Jun 25 '18 at 08:14
0

This answer is for this question. That question is marked of a duplicate of this question. It cannot accept accept answers. Therefore I am posting the answer here.

Here is code to deserialize JSON in C# if the JSON is in camelCase and the property names of the type you're deserializing to is in PascalCase:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace j.Tests
{
    [TestClass]
    public class MyStaticClassTests
    {
        [TestMethod]
        public void Sandbox()
        {
            var value = "My Value";
            var json = $"{{ \"myProperty\": \"{value}\" }}";
            var deserialized = json
                .FromJsonPascalCase<MyClass>();
            Assert.IsNotNull(deserialized);
            Assert.AreEqual(deserialized.MyProperty, value);
        }
    }

    public static class StringExtensions
    {
        private static JsonSerializerSettings pascalCaseSettings =
            new JsonSerializerSettings
            {
                ContractResolver = new PascalCasePropertyNamesContractResolver()
            };

        public static T FromJsonPascalCase<T>(this string json)
        {
            var deserialized = JsonConvert
                .DeserializeObject<T>(
                json,
                pascalCaseSettings);
            return deserialized;
        }

        /// <summary>
        /// Converts a string from camelCase PascalCase
        /// Adapted from: https://andrewlock.net/serializing-a-pascalcase-newtonsoft-json-jobject-to-camelcase/
        /// </summary>
        public static string ToPascalCase(this string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return char.ToUpperInvariant(str[0]) + str.Substring(1);
            }

            return str;
        }
    }

    /// <summary>
    /// Adapted from: https://stackoverflow.com/a/55499707/569302
    /// </summary>
    public class PascalCasePropertyNamesContractResolver : DefaultContractResolver
    {
        protected override string ResolvePropertyName(string propertyName)
        {
            return base
                .ResolvePropertyName(propertyName)
                .ToPascalCase();
        }
    }

    public class MyClass
    {
        public string MyProperty { get; set; }
    }
}
Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97