Skip to content

Commit 768beb7

Browse files
ENH: ParameterSerializer: Improve escaping of the JSONModuleDescription
The JSONModuleDescription did not escape the already escaped quote marks correctly, causing build error: error: expected ';' after top level declarator " \"Default\" : \"\\"1\\"\",\n" Escaping the escape character fixes this. A parameter with a string default with quotes is also added to testing to make sure that this works. Reported-by: Andrey Fedorov <fedorov@bwh.harvard.edu> Tested-by: Andrey Fedorov <fedorov@bwh.harvard.edu> Thanks: Andrey Fedorov <fedorov@bwh.harvard.edu>
1 parent 69ed4de commit 768beb7

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

GenerateCLP/GenerateCLP.cxx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,16 @@ void GeneratePre(std::ostream &sout, ModuleDescription &, int argc, char *argv[]
468468
}
469469

470470
#ifdef GenerateCLP_USE_SERIALIZER
471+
void ReplaceCharacter(std::string& line, char character, const std::string replacement)
472+
{
473+
size_t position = line.find( character );
474+
while( position != std::string::npos )
475+
{
476+
line = line.replace( position, 1, replacement );
477+
position = line.find( character, position + 2 );
478+
}
479+
}
480+
471481
void GenerateJson( std::ostream & sout,
472482
itk::SEMModuleDescriptionSerializer::Pointer & serializer )
473483
{
@@ -482,12 +492,8 @@ void GenerateJson( std::ostream & sout,
482492
{
483493
std::string line;
484494
std::getline( iStream, line );
485-
size_t quotePosition = line.find( '"' );
486-
while( quotePosition != std::string::npos )
487-
{
488-
line = line.replace( quotePosition, 1, "\\\"" );
489-
quotePosition = line.find( '"', quotePosition + 2 );
490-
}
495+
ReplaceCharacter(line, '\\', "\\\\");
496+
ReplaceCharacter(line, '"', "\\\"");
491497
sout << "\"" << line << "\\n\"\n";
492498
}
493499
sout << ";\n";

GenerateCLP/Testing/PointExample1/PointExample1.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,19 @@
4141
<description>Multi Float</description>
4242
<label>MultiFloat</label>
4343
</float>
44+
<string>
45+
<name>SimpleString</name>
46+
<longflag>--string</longflag>
47+
<description>Simple string to test that the escaping by Generate CLP</description>
48+
<label>Simple String</label>
49+
<default>This doublequote " and this quoted "word" should be escaped</default>
50+
</string>
51+
<string>
52+
<name>CDataSimpleString</name>
53+
<longflag>--cdataString</longflag>
54+
<description>Simple string to test that the escaping by Generate CLP while using CDATA</description>
55+
<label>CDATA Simple String</label>
56+
<default><![CDATA[This doublequote " and this quoted "word" should be escaped]]></default>
57+
</string>
4458
</parameters>
4559
</executable>

0 commit comments

Comments
 (0)