Open Closed

"extraProperties" to camelCase in json response. #3645


User avatar
0
vuvanquyet17102@gmail.com created

How can I set name of property in "extraProperties" to camelCase:


2 Answer(s)
  • User Avatar
    0
    mahmut.gundogdu created

    The Extra properties serialised 'as-is'. if you want to return 'socialSecurityNumber' for a now you should write camelCase. I am looking the better solution. Maybe we will add Newtonsoft.Json.JsonProprertyAttribute support.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Add the below code to your web project.

    This may cause issues, because by design the key name in the dictionary is not camelCase

    
    Configure<JsonOptions>(options =>
    {
        options.JsonSerializerOptions.Converters.Add(new MyJsonConverter());
    });
    
    public class MyJsonConverter : JsonConverter<IdentityUserDto>
    {
        private JsonSerializerOptions _readJsonSerializerOptions;
        private JsonSerializerOptions _writeJsonSerializerOptions;
    
        public override IdentityUserDto Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            _readJsonSerializerOptions ??= new JsonSerializerOptions(options)
            {
                DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
            };
            _readJsonSerializerOptions.Converters.RemoveAll(x => x.GetType() == typeof(MyJsonConverter));
    
            return JsonSerializer.Deserialize(ref reader, typeToConvert, _readJsonSerializerOptions).As<IdentityUserDto>();
        }
    
        public override void Write(Utf8JsonWriter writer, IdentityUserDto value, JsonSerializerOptions options)
        {
            _writeJsonSerializerOptions ??= new JsonSerializerOptions(options)
            {
                DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
            };
            _writeJsonSerializerOptions.Converters.RemoveAll(x => x.GetType() == typeof(MyJsonConverter));
            
            JsonSerializer.Serialize(writer, value, _writeJsonSerializerOptions);
        }
    }
    
    
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38