navule
navule

Reputation: 3624

Invalidate JSON object for keys without quotes in c#

In javascript this question JSON.parse(json) will validate to fail Is there any alternative in c# when quotes are not provided for names in json object. (names indicate keys in keyvalue pairs)

{
    name: "s"
}

should fail validation where as

{
    "name": "s"
}

should pass validation

Tried with Newtonsoft.Json's JObject.Parse(body); but it automatically adds quotes and passes validation. I'm trying to validate according to JSON Standard RFC 4627 in c#. Wondering if there a facility to fail validation in case of not providing quotes for keys in c#

Upvotes: 3

Views: 788

Answers (1)

navule
navule

Reputation: 3624

Finally figured out the solution on my own.

    public bool ValidateMissingDoubleQuotes(string json)
    {
        using (var reader = new JsonTextReader(new StringReader(json)))
        {
            while (reader.Read())
            {
                return !(reader.TokenType == JsonToken.PropertyName && reader.QuoteChar != '\"');
            }
        }
        return true;
    }

Upvotes: 3

Related Questions