Disable Support for Reading (Invalid JSON) Single Quote Strings

橙三吉。 提交于 2020-01-03 09:00:11

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/48236247/disable-support-for-reading-invalid-json-single-quote-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!