7

Is there a deserialization option to perform case-sensitive deserialization with Json.NET?

Suggest:

public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

must fail, when deserialized from:

{
  "email": "[email protected]",
  "active": true,
  "createdDate": "2013-01-20T00:00:00Z",
  "roles": [
    "User",
    "Admin"
  ]
}
1
  • 1
    Why do you want the failure? Mixing cases in variable names leads to maintenance nightmares months down the road Commented Jun 17, 2018 at 1:06

1 Answer 1

18

Not likely unfortunately. It seems to be hardcoded to try case-sensitive then case-insensitive.

/// <summary>
/// Gets the closest matching <see cref="JsonProperty"/> object.
/// First attempts to get an exact case match of propertyName and then
/// a case insensitive match.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns>A matching property if found.</returns>
public JsonProperty GetClosestMatchProperty(string propertyName)
{
    JsonProperty property = GetProperty(propertyName, StringComparison.Ordinal);
    if (property == null)
    {
        property = GetProperty(propertyName, StringComparison.OrdinalIgnoreCase);
    }

    return property;
}

JsonPropertyCollection

This is invoked by the internal reader so there's no simple switch that you could just flip. It should be possible, but you would have to write the converter yourself to reject case-insensitive matches.

Sign up to request clarification or add additional context in comments.

1 Comment

See also Issue #815: Provide a way to do case-sensitive property deserialization on github which requests the ability to opt-in to case sensitive deserialization with Json.NET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.