Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.NET case-sensitive deserialization

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"
  ]
}
like image 869
brutallord Avatar asked Jan 02 '16 16:01

brutallord


People also ask

Is JSON Net case sensitive?

Json does case sensitive JSON deserialization. Case sensitivity comes into play when a JSON string is being deserialized into an object.

Is Jobject case sensitive?

Refer to this answer that this is wanted according the JSON-RPC spec (keys are case sensitive!).

Should JSON be case insensitive?

SQL, by default, is case insensitive to identifiers and keywords, but case sensitive to data. JSON is case sensitive to both field names and data.

Is StringEnumConverter case sensitive?

Jericho commented on Nov 25, 2017Up until Json.net v9, the StringEnumConverter class was case insensitive and it was changed in v10. Here's the discussion explaining why it was changed and here's the commit released in v10 that changed the behavior to case-sensitive.


1 Answers

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.

like image 126
Jeff Mercado Avatar answered Oct 12 '22 14:10

Jeff Mercado