MihailPw
MihailPw

Reputation: 549

Return json with lower case first letter of property names

I have LoginModel:

public class LoginModel : IData
{
    public string Email { get; set; }
    public string Password { get; set; }
}

and I have the Web api method

public IHttpActionResult Login([FromBody] LoginModel model)
{
    return this.Ok(model);
}

And it's return 200 and body:

{
    Email: "dfdf",
    Password: "dsfsdf"
}

But I want to get with lower first letter in property like

{
    email: "dfdf",
    password: "dsfsdf"
}

And I have Json contract resolver for correcting

public class FirstLowerContractResolver : DefaultContractResolver
{
    protected override string ResolvePropertyName(string propertyName)
    {
        if (string.IsNullOrWhiteSpace(propertyName))
            return string.Empty;

        return $"{char.ToLower(propertyName[0])}{propertyName.Substring(1)}";
    }
}

How I can apply this?

Upvotes: 36

Views: 56815

Answers (5)

tar
tar

Reputation: 326

You can do this with System.Text.Json since .NET 8:

JsonSerializerOptions jsonOptions = new() {
  // dont mind upper/lower case
  PropertyNameCaseInsensitive = true,
  // use camel case with first lower letter
  PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

string json = JsonSerializer.Serialize(myObject, jsonOptions);

Upvotes: 1

Grengas
Grengas

Reputation: 1054

If you need it only in some certain place and not throughout whole application, then you can do following:

var objectToSerialize = new {Property1 = "value1", SubOjbect = new { SubObjectId = 1 }};
var json = Newtonsoft.Json.JsonConvert.SerializeObject(data, new JsonSerializerSettings { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() });

It should result in {"property1":"value1","subOjbect":{"subObjectId":1}} (note that nested properties also start from lowercase)

Upvotes: 7

Hakan Fıstık
Hakan Fıstık

Reputation: 19421

You can add the two following statement in the configuration of the web API or to the startup file

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;

But it is very important to use the return Ok() method instead of return Json() otherwise; this will not work.

if you have to use Json method (and have no other choice) then see this answer https://stackoverflow.com/a/28960505/4390133

Upvotes: 13

Luca Ghersi
Luca Ghersi

Reputation: 3321

To force all json data returned from api to camel case it's easier to use Newtonsoft Json with the default camel case contract resolver.

Create a class like this one:

using Newtonsoft.Json.Serialization;

internal class JsonContentNegotiator : IContentNegotiator
{
    private readonly JsonMediaTypeFormatter _jsonFormatter;

    public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
    {
        _jsonFormatter = formatter;          
        _jsonFormatter.SerializerSettings.ContractResolver =
            new CamelCasePropertyNamesContractResolver();
    }

    public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
    {
        return new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
    }
}

and set this during api configuration (at startup):

var jsonFormatter = new JsonMediaTypeFormatter();
httpConfiguration.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));

Upvotes: 23

AdrienTorris
AdrienTorris

Reputation: 9391

If your are using Newtonsoft.Json, you can add JsonProperties to your view model :

public class LoginModel : IData
{
     [JsonProperty(PropertyName = "email")]
     public string Email {get;set;}

     [JsonProperty(PropertyName = "password")]
     public string Password {get;set;}
}

Upvotes: 41

Related Questions