Disable read support (Invalid JSON) Separate quotation lines

Newtonsoft.Json for C # supports reading things like {'key':'value'} , but this is incorrect JSON. Is it possible to disable this so that it parses and reads more like PHP (where PHP does not support {'key': 'value'}, but {"key": "value"})

+7
source share
1 answer

You can write your own subclass of JsonReader to accomplish this, but the JsonTextReader class (which is most commonly used as far as I know) does not support this. From the ParseValue method, for example:

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

I have a strict JSON tokenizer on Google. Protobuf is internal, but should give you an idea that it is not terribly difficult to write a tokenizer yourself. This will not help you if you really want to use Json.NET, except for rigor, of course.

You might want to read and potentially vote / comment on issue 646 in the Json.NET repository, where I asked for "strict mode" as well. (It also offers an alternative approach - although it looks a bit like a hack.)

+16
source share

All Articles