Newtonsoft.Json for C# supports reading stuff like {'key':'value'} but thats improper JSON. Is it possible to disable that so it parses and reads more like PHP (Where'as PHP doesnt support {'key':'value'} but {"key":"value"})

upvote
  flag
With software design you really want to be as flexible as possible with your inputs and rigorous in your outputs. So, what's the purpose of constraining the input in this manner? – Sam Axe
upvote
  flag
@SamAxe: I strongly disagree with that philosophy. I'm very much in favour of only supporting valid inputs. It's the "let's be lax" attitude that leads to so much broken HTML etc. Most XML parsers are pretty strict. – Jon Skeet
upvote
  flag
@JonSkeet: I suppose there's an argument to be made either way.. and like most things what is acceptable will depend on a number of factors. I very much respect your point of view on this. – Sam Axe
upvote
  flag
See Support "strict mode" for RFC7159 parsing #646 where James Newton-King has several suggestions for implementing strict JSON checking including, What you could do is inherit from JsonTextReader, override Read and check the incoming data after each base.Read(). If QuoteChar is ' then throw an error,... However the issue got closed with no plans to implement yet. – dbc
upvote
  flag
@dbc: Funny - I just linked to that issue within my own answer, too :) – Jon Skeet
upvote
  flag
@JonSkeet - well you did open it after all :) – dbc

1 Answers 11

You could write your own JsonReader subclass to perform this, but the JsonTextReader class (which is the most commonly used one, as far as I'm aware) doesn't support this. From the ParseValue method, for example:

case '"':
case '\'':
    ParseString(currentChar, ReadType.Read);
    return true;

I have a strict JSON tokenizer in Google.Protobuf - it's internal, but should give you some idea that it's not terribly tricky to write such a tokenizer yourself. That doesn't help you if you really want to use Json.NET other than the strictness, of course.

You might want to read and potentially vote/comment on issue 646 in the Json.NET repo, where I requested a "strict mode" as well. (There's a suggested alternative approach there, too - although it feels like a bit of a hack.)

upvote
  flag
How exactly can I do this? I use JToken.Parse() to read the JSON strings, not entirely sure what im meant to do here. – user8549339
upvote
  flag
@user8549339: And this is why it's always useful to provide a Minimal, Complete, and Verifiable example - we'd have known which API you were using. It sounds like you want JToken.Load, passing in the appropriate reader. – Jon Skeet
upvote
  flag
Im literally using Newtonsoft.Json, just the regular old thing and then use JToken.Parse(string json) – user8549339
upvote
  flag
@user8549339: But calling JToken.Parse is only one way of using Json.NET - and your question didn't explain what you've told us now. However, I've now explained what you'd do if you wanted to follow the advice in issue 646: you'd create the subclass of JsonTextReader, then pass it to JToken.Load. But as I say, it's a bit of a hack IMO. – Jon Skeet

Not the answer you're looking for? Browse other questions tagged or ask your own question.