MVC now serializes JSON with camel case names by default #194

Open
dougbu opened this Issue Jun 9, 2016 · 3 comments

Projects

None yet

1 participant

@dougbu
Member
dougbu commented Jun 9, 2016 edited

In previous milestones, MVC's JSON serialization used Json.NET's default naming convention. This maintained C# property names in the JSON.

In 1.0.0, MVC uses camel case names by default. This matches most JSON naming conventions.

Potential compatibility breaks

Applications which depend on the exact bytes sent over the wire or that include code such as

dynamic d = JObject.Parse(body);

may need to be adjusted.

To restore previous naming strategy

If you have case-sensitive clients that cannot be easily updated, change your Startup from

    services.AddMvc();

to

    services
        .AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
Example
Before
public class Person
{
    public int Id { get; set; }

    public string FullName { get; set; }
}

Would serialize to

{"Id":9000,"FullName":"John Smith"}
After

The same model will serialize to

{"id":9000,"fullName":"John Smith"}

Note the initial lowercase letters.

@dougbu dougbu added this to the 1.0.0 milestone Jun 9, 2016
@dougbu
Member
dougbu commented Jun 9, 2016

Please see aspnet/Mvc#4842 for discussion of this change.

@dougbu dougbu locked and limited conversation to collaborators Jun 9, 2016
@dougbu
Member
dougbu commented Jun 9, 2016

See also the original issue we fixed in 1.0.0: aspnet/Mvc#4283

@dougbu
Member
dougbu commented Jun 10, 2016

Updated description to show the configuration one-liner.

@dougbu dougbu referenced this issue in aspnet/Mvc Jul 17, 2016
@dougbu dougbu Remove extra options to manipulate `JsonSerializerSettings`
- #4339: remove non-recommended JSON formatter constructors
 - affects `JsonInputFormatter`, `JsonOutputFormatter`, `JsonPatchInputFormatter`
 - `JsonOutputFormatter` cleanup also impacts `JsonHelper`
 - rename and make `SerializerSettingsProvider` class public; use it as appropriate
- #4409: make `SerializerSetings` properties get-only and `protected`
 - affects `JsonInputFormatter`, `JsonOutputFormatter`

Recommended patterns:
- change `JsonSerializerSettings` values in `MvcJsonOptions` for almost all customizations
- find `JsonOutputFormatter` in `MvcOptions.OutputFormatters` when limiting per-result formatters
- start with `JsonSerializerSettingsProvider.CreateSerializerSettings()` when customizing a per-result formatter
d8d2e54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.