Slicer 5.13
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#include "vtkVariant.h"
25
26#include <vector>
27
28class vtkCodedEntry;
29class vtkXMLDataElement;
30
33class VTK_MRML_EXPORT vtkMRMLJsonElement : public vtkObject
34{
35public:
37 vtkTypeMacro(vtkMRMLJsonElement, vtkObject);
38 void PrintSelf(ostream& os, vtkIndent indent) override;
39
40 // Separator characters for (de)serializing the UID and the attributes map
41 static const std::string XML_SEPARATOR;
42 static const std::string XML_NAME_VALUE_SEPARATOR;
43
45 std::string GetSchema();
46
48 bool HasMember(const char* propertyName);
49
50 enum Type
51 {
53 OBJECT = 1,
54 ARRAY = 2,
55 STRING = 3,
56 BOOL = 4,
57 INT = 5,
59 };
60 Type GetMemberType(const char* propertyName);
61
64 std::string GetStringProperty(const char* propertyName);
68 bool GetStringProperty(const char* propertyName, std::string& propertyValue);
69
71 bool GetBoolProperty(const char* propertyName);
75 bool GetBoolProperty(const char* propertyName, bool& propertyValue);
76
80 bool GetDoubleProperty(const char* propertyName, double& propertyValue);
81
84 double GetDoubleProperty(const char* propertyName);
85
89 bool GetIntProperty(const char* propertyName, int& propertyValue);
90
93 int GetIntProperty(const char* propertyName);
94
97 bool GetVectorProperty(const char* propertyName, double* v, int numberOfComponents = 3);
98
103 bool GetMatrix4x4Property(const char* propertyName, double v[16], bool flipRasLps);
104
107 bool GetStringVectorProperty(const char* propertyName, std::vector<std::string>& arrayValues);
108
112 VTK_NEWINSTANCE
113 vtkCodedEntry* GetCodedEntryProperty(const char* propertyName);
114
118 VTK_NEWINSTANCE
119 vtkDoubleArray* GetDoubleArrayProperty(const char* propertyName);
120
123 bool GetArrayItemsStringProperty(const char* arrayName, const char* propertyName, std::vector<std::string>& propertyValues);
124
128 VTK_NEWINSTANCE
129 vtkMRMLJsonElement* GetArrayProperty(const char* arrayName);
130
134 VTK_NEWINSTANCE
135 vtkMRMLJsonElement* GetObjectProperty(const char* objectName);
136
138 bool IsArray();
139
142
145 VTK_NEWINSTANCE
146 vtkMRMLJsonElement* GetArrayItem(int childItemIndex);
147
149 bool IsObject();
150
153
155 std::string GetObjectPropertyNameByIndex(int childItemIndex);
156
159 VTK_NEWINSTANCE
160 vtkMRMLJsonElement* GetObjectItem(int childItemIndex);
161
164
167 VTK_NEWINSTANCE
169
172
174 bool HasErrors();
175
176protected:
181
182 vtkNew<vtkMRMLMessageCollection> UserMessages;
183
184 class vtkInternal;
186 friend class vtkInternal;
187 friend class vtkMRMLJsonReader;
188};
189
190class VTK_MRML_EXPORT vtkMRMLJsonReader : public vtkObject
191{
192public:
194 vtkTypeMacro(vtkMRMLJsonReader, vtkObject);
195 void PrintSelf(ostream& os, vtkIndent indent) override;
196
200 VTK_NEWINSTANCE
201 vtkMRMLJsonElement* ReadFromFile(const char* filePath);
202
206 VTK_NEWINSTANCE
207 vtkMRMLJsonElement* ReadFromString(const std::string& jsonString);
208
211 std::string ConvertJsonToXML(const std::string& jsonString, const std::string& nodeTagName);
212
215
217 bool HasErrors();
218
219protected:
224
225 std::string processJsonElement(vtkMRMLJsonElement* jsonElement, const std::string& elementKey = "");
226
227 vtkNew<vtkMRMLMessageCollection> UserMessages;
228};
229
232class VTK_MRML_EXPORT vtkMRMLJsonWriter : public vtkObject
233{
234public:
236 vtkTypeMacro(vtkMRMLJsonWriter, vtkObject);
237 void PrintSelf(ostream& os, vtkIndent indent) override;
238
241 bool WriteToFileBegin(const char* filePath, const char* schema);
242
246
251 bool WriteToStringBegin(const char* nodeTagName);
252
258
261 std::string WriteToStringEnd();
262
264 void WriteArrayPropertyStart(const std::string& propertyName);
267
272
274 void WriteObjectPropertyStart(const std::string& propertyName);
277
280 void WriteStringProperty(const std::string& propertyName, const std::string& propertyValue);
281 void WriteStringVectorProperty(const std::string& propertyName, const std::vector<std::string>& arrayValues);
282 void WriteCodedEntryProperty(const std::string& propertyName, vtkCodedEntry* codedEntry);
283 void WriteStringPropertyIfNotEmpty(const std::string& propertyName, const std::string& propertyValue);
284 void WriteBoolProperty(const std::string& propertyName, bool propertyValue);
285 void WriteIntProperty(const std::string& propertyName, int propertyValue);
286 void WriteDoubleProperty(const std::string& propertyName, double propertyValue);
287 void WriteVectorProperty(const std::string& propertyName, double* v, int numberOfComponents = 3);
288 void WriteMatrix4x4Property(const std::string& propertyName, double v[16], bool flipRasLps);
289 void WriteDoubleArrayProperty(const char* propertyName, vtkDoubleArray* doubleArray);
291
294 void WriteVariantProperty(const std::string& propertyName, const vtkVariant& propertyValue);
295
297 void WriteNullProperty(const std::string& propertyName);
298
300 std::string toLower(const std::string& str);
302 bool isBool(const std::string& str);
303 bool isInt(const std::string& str);
304 bool isDouble(const std::string& str);
305
308 std::string ConvertXMLToJson(vtkXMLDataElement* xmlElement, const std::string& nodeTagName);
309
312
314 bool HasErrors();
315
316protected:
321
322 void processXMLElement(vtkXMLDataElement* xmlElement);
323
324 vtkNew<vtkMRMLMessageCollection> UserMessages;
325
326 class vtkInternal;
328 friend class vtkInternal;
329};
330
331#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
bool WriteToStringBegin()
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.
void WriteNullProperty(const std::string &propertyName)
Write null as a property value.
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 WriteVariantProperty(const std::string &propertyName, const vtkVariant &propertyValue)
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)