Slicer 5.9
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
Loading...
Searching...
No Matches
vtkMRMLJsonElement.h
Go to the documentation of this file.
1/*==============================================================================
2
3 Program: 3D Slicer
4
5 Portions (c) Copyright Brigham and Women's Hospital (BWH) All Rights Reserved.
6
7 See COPYRIGHT.txt
8 or http://www.slicer.org/copyright/copyright.txt for details.
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16==============================================================================*/
17
18#ifndef vtkMRMLJsonElement_h
19#define vtkMRMLJsonElement_h
20
22#include "vtkSmartPointer.h"
23#include "vtkNew.h"
24
25#include <vector>
26
27class vtkCodedEntry;
28class vtkXMLDataElement;
29
32class VTK_MRML_EXPORT vtkMRMLJsonElement : public vtkObject
33{
34public:
36 vtkTypeMacro(vtkMRMLJsonElement, vtkObject);
37 void PrintSelf(ostream& os, vtkIndent indent) override;
38
39 // Separator characters for (de)serializing the UID and the attributes map
40 static const std::string XML_SEPARATOR;
41 static const std::string XML_NAME_VALUE_SEPARATOR;
42
44 std::string GetSchema();
45
47 bool HasMember(const char* propertyName);
48
49 enum Type
50 {
52 OBJECT = 1,
53 ARRAY = 2,
54 STRING = 3,
55 BOOL = 4,
56 INT = 5,
58 };
59 Type GetMemberType(const char* propertyName);
60
63 std::string GetStringProperty(const char* propertyName);
67 bool GetStringProperty(const char* propertyName, std::string& propertyValue);
68
70 bool GetBoolProperty(const char* propertyName);
74 bool GetBoolProperty(const char* propertyName, bool& propertyValue);
75
79 bool GetDoubleProperty(const char* propertyName, double& propertyValue);
80
83 double GetDoubleProperty(const char* propertyName);
84
88 bool GetIntProperty(const char* propertyName, int& propertyValue);
89
92 int GetIntProperty(const char* propertyName);
93
96 bool GetVectorProperty(const char* propertyName, double* v, int numberOfComponents = 3);
97
102 bool GetMatrix4x4Property(const char* propertyName, double v[16], bool flipRasLps);
103
106 bool GetStringVectorProperty(const char* propertyName, std::vector<std::string>& arrayValues);
107
111 VTK_NEWINSTANCE
112 vtkCodedEntry* GetCodedEntryProperty(const char* propertyName);
113
117 VTK_NEWINSTANCE
118 vtkDoubleArray* GetDoubleArrayProperty(const char* propertyName);
119
122 bool GetArrayItemsStringProperty(const char* arrayName, const char* propertyName, std::vector<std::string>& propertyValues);
123
127 VTK_NEWINSTANCE
128 vtkMRMLJsonElement* GetArrayProperty(const char* arrayName);
129
133 VTK_NEWINSTANCE
134 vtkMRMLJsonElement* GetObjectProperty(const char* objectName);
135
137 bool IsArray();
138
141
144 VTK_NEWINSTANCE
145 vtkMRMLJsonElement* GetArrayItem(int childItemIndex);
146
148 bool IsObject();
149
152
154 std::string GetObjectPropertyNameByIndex(int childItemIndex);
155
158 VTK_NEWINSTANCE
159 vtkMRMLJsonElement* GetObjectItem(int childItemIndex);
160
163
166 VTK_NEWINSTANCE
168
171
173 bool HasErrors();
174
175protected:
180
181 vtkNew<vtkMRMLMessageCollection> UserMessages;
182
183 class vtkInternal;
185 friend class vtkInternal;
186 friend class vtkMRMLJsonReader;
187};
188
189class VTK_MRML_EXPORT vtkMRMLJsonReader : public vtkObject
190{
191public:
193 vtkTypeMacro(vtkMRMLJsonReader, vtkObject);
194 void PrintSelf(ostream& os, vtkIndent indent) override;
195
199 VTK_NEWINSTANCE
200 vtkMRMLJsonElement* ReadFromFile(const char* filePath);
201
205 VTK_NEWINSTANCE
206 vtkMRMLJsonElement* ReadFromString(const std::string& jsonString);
207
210 std::string ConvertJsonToXML(const std::string& jsonString, const std::string& nodeTagName);
211
214
216 bool HasErrors();
217
218protected:
223
224 std::string processJsonElement(vtkMRMLJsonElement* jsonElement, const std::string& elementKey = "");
225
226 vtkNew<vtkMRMLMessageCollection> UserMessages;
227};
228
231class VTK_MRML_EXPORT vtkMRMLJsonWriter : public vtkObject
232{
233public:
235 vtkTypeMacro(vtkMRMLJsonWriter, vtkObject);
236 void PrintSelf(ostream& os, vtkIndent indent) override;
237
240 bool WriteToFileBegin(const char* filePath, const char* schema);
241
245
248 bool WriteToStringBegin(const char* nodeTagName);
249
252 std::string WriteToStringEnd();
253
255 void WriteArrayPropertyStart(const std::string& propertyName);
258
263
265 void WriteObjectPropertyStart(const std::string& propertyName);
268
271 void WriteStringProperty(const std::string& propertyName, const std::string& propertyValue);
272 void WriteStringVectorProperty(const std::string& propertyName, const std::vector<std::string>& arrayValues);
273 void WriteCodedEntryProperty(const std::string& propertyName, vtkCodedEntry* codedEntry);
274 void WriteStringPropertyIfNotEmpty(const std::string& propertyName, const std::string& propertyValue);
275 void WriteBoolProperty(const std::string& propertyName, bool propertyValue);
276 void WriteIntProperty(const std::string& propertyName, int propertyValue);
277 void WriteDoubleProperty(const std::string& propertyName, double propertyValue);
278 void WriteVectorProperty(const std::string& propertyName, double* v, int numberOfComponents = 3);
279 void WriteMatrix4x4Property(const std::string& propertyName, double v[16], bool flipRasLps);
280 void WriteDoubleArrayProperty(const char* propertyName, vtkDoubleArray* doubleArray);
282
284 std::string toLower(const std::string& str);
286 bool isBool(const std::string& str);
287 bool isInt(const std::string& str);
288 bool isDouble(const std::string& str);
289
292 std::string ConvertXMLToJson(vtkXMLDataElement* xmlElement, const std::string& nodeTagName);
293
296
298 bool HasErrors();
299
300protected:
305
306 void processXMLElement(vtkXMLDataElement* xmlElement);
307
308 vtkNew<vtkMRMLMessageCollection> UserMessages;
309
310 class vtkInternal;
312 friend class vtkInternal;
313};
314
315#endif
Simple class for storing standard coded entries (coding scheme, value, meaning triplets)
Represents a json object or list.
int GetObjectSize()
Returns the number of elements in this object.
bool GetStringProperty(const char *propertyName, std::string &propertyValue)
std::string GetSchema()
Get the JSON schema name.
bool GetArrayItemsStringProperty(const char *arrayName, const char *propertyName, std::vector< std::string > &propertyValues)
bool GetDoubleProperty(const char *propertyName, double &propertyValue)
double GetDoubleProperty(const char *propertyName)
VTK_NEWINSTANCE vtkMRMLJsonElement * GetArrayItem(int childItemIndex)
VTK_NEWINSTANCE vtkMRMLJsonElement * GetObjectProperty(const char *objectName)
static vtkMRMLJsonElement * New()
VTK_NEWINSTANCE vtkMRMLJsonElement * GetObjectItem(int childItemIndex)
std::string GetStringProperty(const char *propertyName)
VTK_NEWINSTANCE vtkMRMLJsonElement * GetChildMemberItem(int childItemIndex)
int GetArraySize()
Returns the number of elements in this array.
friend class vtkMRMLJsonReader
bool IsArray()
Returns true if this element is an array.
~vtkMRMLJsonElement() override
void PrintSelf(ostream &os, vtkIndent indent) override
std::string GetObjectPropertyNameByIndex(int childItemIndex)
Get object member name by index.
bool HasErrors()
Returns true if user messages contain error messages.
vtkNew< vtkMRMLMessageCollection > UserMessages
bool GetIntProperty(const char *propertyName, int &propertyValue)
bool GetVectorProperty(const char *propertyName, double *v, int numberOfComponents=3)
void operator=(const vtkMRMLJsonElement &)
VTK_NEWINSTANCE vtkDoubleArray * GetDoubleArrayProperty(const char *propertyName)
int GetIntProperty(const char *propertyName)
bool GetBoolProperty(const char *propertyName)
Get Boolean property value.
VTK_NEWINSTANCE vtkMRMLJsonElement * GetArrayProperty(const char *arrayName)
static const std::string XML_SEPARATOR
bool HasMember(const char *propertyName)
Returns true if the JSON object contains a member by this name.
bool GetStringVectorProperty(const char *propertyName, std::vector< std::string > &arrayValues)
Type GetMemberType(const char *propertyName)
bool IsObject()
Returns true if this element is an object.
vtkMRMLJsonElement(const vtkMRMLJsonElement &)
bool GetMatrix4x4Property(const char *propertyName, double v[16], bool flipRasLps)
VTK_NEWINSTANCE vtkCodedEntry * GetCodedEntryProperty(const char *propertyName)
static const std::string XML_NAME_VALUE_SEPARATOR
bool GetBoolProperty(const char *propertyName, bool &propertyValue)
Type GetType()
Returns this element type.
bool HasErrors()
Returns true if user messages contain error messages.
void PrintSelf(ostream &os, vtkIndent indent) override
VTK_NEWINSTANCE vtkMRMLJsonElement * ReadFromString(const std::string &jsonString)
std::string processJsonElement(vtkMRMLJsonElement *jsonElement, const std::string &elementKey="")
static vtkMRMLJsonReader * New()
vtkMRMLJsonReader(const vtkMRMLJsonReader &)
~vtkMRMLJsonReader() override
VTK_NEWINSTANCE vtkMRMLJsonElement * ReadFromFile(const char *filePath)
void operator=(const vtkMRMLJsonReader &)
std::string ConvertJsonToXML(const std::string &jsonString, const std::string &nodeTagName)
vtkNew< vtkMRMLMessageCollection > UserMessages
std::string ConvertXMLToJson(vtkXMLDataElement *xmlElement, const std::string &nodeTagName)
void WriteStringProperty(const std::string &propertyName, const std::string &propertyValue)
void PrintSelf(ostream &os, vtkIndent indent) override
void processXMLElement(vtkXMLDataElement *xmlElement)
void WriteIntProperty(const std::string &propertyName, int propertyValue)
void WriteBoolProperty(const std::string &propertyName, bool propertyValue)
std::string toLower(const std::string &str)
Utility function to convert string to lower case.
void WriteStringPropertyIfNotEmpty(const std::string &propertyName, const std::string &propertyValue)
vtkMRMLJsonWriter(const vtkMRMLJsonWriter &)
void WriteArrayPropertyEnd()
This method must be called when all array items are written.
~vtkMRMLJsonWriter() override
void WriteDoubleProperty(const std::string &propertyName, double propertyValue)
void WriteObjectPropertyStart(const std::string &propertyName)
This method creates a new object as a property.
void WriteObjectPropertyEnd()
This method must be called when all properties of the object are written.
void WriteCodedEntryProperty(const std::string &propertyName, vtkCodedEntry *codedEntry)
void WriteObjectStart()
This method creates a new object in an array.
bool isBool(const std::string &str)
Utility functions to check string type.
static vtkMRMLJsonWriter * New()
void WriteDoubleArrayProperty(const char *propertyName, vtkDoubleArray *doubleArray)
void WriteVectorProperty(const std::string &propertyName, double *v, int numberOfComponents=3)
void WriteArrayPropertyStart(const std::string &propertyName)
This method creates a new array as a property.
bool WriteToStringBegin(const char *nodeTagName)
bool WriteToFileBegin(const char *filePath, const char *schema)
bool isInt(const std::string &str)
void WriteStringVectorProperty(const std::string &propertyName, const std::vector< std::string > &arrayValues)
std::string WriteToStringEnd()
bool HasErrors()
Returns true if user messages contain error messages.
void WriteMatrix4x4Property(const std::string &propertyName, double v[16], bool flipRasLps)
void operator=(const vtkMRMLJsonWriter &)
vtkNew< vtkMRMLMessageCollection > UserMessages
void WriteObjectEnd()
This method must be called when all properties of the object are written.
bool isDouble(const std::string &str)