7

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"})

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • 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 Jan 13 '18 at 02:17
  • @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 Jan 13 '18 at 02:19
  • @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 Jan 13 '18 at 02:22
  • See [Support "strict mode" for RFC7159 parsing #646](https://github.com/JamesNK/Newtonsoft.Json/issues/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 Jan 13 '18 at 02:23
  • @dbc: Funny - I just linked to that issue within my own answer, too :) – Jon Skeet Jan 13 '18 at 02:24
  • @JonSkeet - well you did open it after all :) – dbc Jan 13 '18 at 02:24

1 Answers1

14

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • How exactly can I do this? I use JToken.Parse() to read the JSON strings, not entirely sure what im meant to do here. –  Jan 16 '18 at 21:25
  • @user8549339: And this is why it's always useful to provide a [mcve] - we'd have known which API you were using. It sounds like you want `JToken.Load`, passing in the appropriate reader. – Jon Skeet Jan 16 '18 at 22:00
  • Im literally using Newtonsoft.Json, just the regular old thing and then use JToken.Parse(string json) –  Jan 16 '18 at 22:47
  • @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 Jan 16 '18 at 22:49