Bug 1467759 - Escape all the property names in JSONWriter. r=froydnj

Scalar property names (e.g. IntProperty) are already escaped correctly.
This patch escapes the collection property names (e.g. StartObjectProperty)
as well (and adds test coverage for it).

MozReview-Commit-ID: 68kkjPb2ZN7

--HG--
extra : rebase_source : b42939ccebb71fc73914ff595803b2d6c7df2715
This commit is contained in:
Alessio Placitelli 2018-06-08 19:50:25 +02:00
parent 38762b2fd4
commit 1ecfce2746
2 changed files with 43 additions and 3 deletions

View File

@ -313,9 +313,7 @@ protected:
{
Separator();
if (aMaybePropertyName) {
mWriter->Write("\"");
mWriter->Write(aMaybePropertyName);
mWriter->Write("\": ");
PropertyNameAndColon(aMaybePropertyName);
}
mWriter->Write(aStartChar);
mNeedComma[mDepth] = true;

View File

@ -527,6 +527,47 @@ void TestDeepNesting()
Check(w.WriteFunc(), expected);
}
void TestEscapedPropertyNames()
{
const char* expected = "\
{\"i\\t\": 1, \"array\\t\": [null, [{}], {\"o\\t\": {}}, \"s\"], \"d\\t\": 3.33}\n\
";
JSONWriter w(MakeUnique<StringWriteFunc>());
w.Start(w.SingleLineStyle);
w.IntProperty("i\t", 1);
w.StartArrayProperty("array\t");
{
w.NullElement();
w.StartArrayElement(w.MultiLineStyle); // style overridden from above
{
w.StartObjectElement();
w.EndObject();
}
w.EndArray();
w.StartObjectElement();
{
w.StartObjectProperty("o\t");
w.EndObject();
}
w.EndObject();
w.StringElement("s");
}
w.EndArray();
w.DoubleProperty("d\t", 3.33);
w.End();
Check(w.WriteFunc(), expected);
}
int main(void)
{
TestBasicProperties();
@ -534,6 +575,7 @@ int main(void)
TestOneLineObject();
TestStringEscaping();
TestDeepNesting();
TestEscapedPropertyNames();
return 0;
}