Skip to content

Commit 9cdb120

Browse files
committed
Fix vtkJSONDataSetWriter with invalid field name
1 parent efe773e commit 9cdb120

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

IO/Export/vtkJSONDataSetWriter.cxx

+16-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ vtkStandardNewMacro(vtkJSONDataSetWriter);
4444
vtkJSONDataSetWriter::vtkJSONDataSetWriter()
4545
{
4646
this->FileName = nullptr;
47+
this->ValidStringCount = 1;
4748
}
4849

4950
// ----------------------------------------------------------------------------
@@ -142,7 +143,7 @@ std::string vtkJSONDataSetWriter::WriteArray(
142143
std::stringstream ss;
143144
ss << "{\n"
144145
<< INDENT << " \"vtkClass\": \"" << className << "\",\n"
145-
<< INDENT << " \"name\": \"" << (arrayName == nullptr ? array->GetName() : arrayName) << "\",\n"
146+
<< INDENT << " \"name\": \"" << this->GetValidString(arrayName == nullptr ? array->GetName() : arrayName) << "\",\n"
146147
<< INDENT << " \"numberOfComponents\": " << array->GetNumberOfComponents() << ",\n"
147148
<< INDENT << " \"dataType\": \"" << vtkJSONDataSetWriter::GetShortType(array, needConvert) << "Array\",\n"
148149
<< INDENT << " \"ref\": {\n"
@@ -387,6 +388,20 @@ std::string vtkJSONDataSetWriter::GetUID(vtkDataArray* input, bool& needConversi
387388

388389
// ----------------------------------------------------------------------------
389390

391+
std::string vtkJSONDataSetWriter::GetValidString(const char* name)
392+
{
393+
if (name != nullptr && strlen(name))
394+
{
395+
return name;
396+
}
397+
std::stringstream ss;
398+
ss << "invalid_" << this->ValidStringCount++;
399+
400+
return ss.str();
401+
}
402+
403+
// ----------------------------------------------------------------------------
404+
390405
bool vtkJSONDataSetWriter::WriteArrayAsRAW(vtkDataArray* input, const char* filePath)
391406
{
392407
if (input->GetDataTypeSize() == 0)

IO/Export/vtkJSONDataSetWriter.h

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class VTKIOEXPORT_EXPORT vtkJSONDataSetWriter : public vtkWriter
7272
static std::string GetUID(vtkDataArray*, bool& needConversion);
7373
//@}
7474

75+
//@{
76+
/**
77+
* Return a Unique identifier for any invalid string
78+
*/
79+
std::string GetValidString(const char*);
80+
//@}
81+
7582
//@{
7683
/**
7784
* Write the content of the vtkDataArray to disk based on the filePath
@@ -117,6 +124,7 @@ class VTKIOEXPORT_EXPORT vtkJSONDataSetWriter : public vtkWriter
117124

118125
char* FileName;
119126
bool ValidDataSet;
127+
int ValidStringCount;
120128

121129
int FillInputPortInformation(int port, vtkInformation* info) override;
122130

0 commit comments

Comments
 (0)