In order to install an external extension into Google Chrome browser, I try to update a Chrome external extension JSON file. Using Json.NET
it seems to be easy:
If you are stuck with JavaScriptSerializer (from the System.Web.Script.Serialization namespace), I have found that this works good enough...
private static string StripComments(string input)
{
// JavaScriptSerializer doesn't accept commented-out JSON,
// so we'll strip them out ourselves;
// NOTE: for safety and simplicity, we only support comments on their own lines,
// not sharing lines with real JSON
input = Regex.Replace(input, @"^\s*//.*$", "", RegexOptions.Multiline); // removes comments like this
input = Regex.Replace(input, @"^\s*/\*(\s|\S)*?\*/\s*$", "", RegexOptions.Multiline); /* comments like this */
return input;
}
Json.NET only supports reading multi-line JavaScript comments, i.e. /* commment */
Update: Json.NET 6.0 supports single line comments
You could always convert single-line comments to multi-line comment syntax before parsing...
Something like replace...
.*//.*\n
with
$1/*$2*/
...
Regex.Replace(subjectString, ".*//.*$", "$1/*$2*/");