Ouvert Fermé

"extraProperties" to camelCase in json response. #3645


User avatar
0
vuvanquyet17102@gmail.com créé

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


2 Réponse (s)
  • User Avatar
    0
    mahmut.gundogdu créé

    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 créé
    Équipe d'assistance 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 v8.2.0-preview Updated on mars 25, 2024, 15:11