23 #ifndef _cmnDataFunctionsJSON_h
24 #define _cmnDataFunctionsJSON_h
38 #include <json/json.h>
40 template <
typename _elementType>
44 typedef _elementType DataType;
45 static void SerializeText(
const DataType & data, Json::Value & jsonValue);
46 static void DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
50 void CISST_EXPORT cmnDataJSON<double>::SerializeText(
const DataType & data, Json::Value & jsonValue);
52 void CISST_EXPORT cmnDataJSON<double>::DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
55 void CISST_EXPORT cmnDataJSON<float>::SerializeText(
const DataType & data, Json::Value & jsonValue);
57 void CISST_EXPORT cmnDataJSON<float>::DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
60 void CISST_EXPORT cmnDataJSON<char>::SerializeText(
const DataType & data, Json::Value & jsonValue);
62 void CISST_EXPORT cmnDataJSON<char>::DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
65 void CISST_EXPORT cmnDataJSON<int>::SerializeText(
const DataType & data, Json::Value & jsonValue);
67 void CISST_EXPORT cmnDataJSON<int>::DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
70 void CISST_EXPORT cmnDataJSON<unsigned int>::SerializeText(
const DataType & data, Json::Value & jsonValue);
72 void CISST_EXPORT cmnDataJSON<unsigned int>::DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
75 void CISST_EXPORT cmnDataJSON<unsigned long int>::SerializeText(
const DataType & data, Json::Value & jsonValue);
77 void CISST_EXPORT cmnDataJSON<unsigned long int>::DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
80 void CISST_EXPORT cmnDataJSON<unsigned long long int>::SerializeText(
const DataType & data, Json::Value & jsonValue);
82 void CISST_EXPORT cmnDataJSON<unsigned long long int>::DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
85 void CISST_EXPORT cmnDataJSON<bool>::SerializeText(
const DataType & data, Json::Value & jsonValue);
87 void CISST_EXPORT cmnDataJSON<bool>::DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
90 void CISST_EXPORT cmnDataJSON<std::string>::SerializeText(
const DataType & data, Json::Value & jsonValue);
92 void CISST_EXPORT cmnDataJSON<std::string>::DeSerializeText(DataType & data,
const Json::Value & jsonValue)
throw (std::runtime_error);
95 template <
class _elementType>
96 class cmnDataJSON<std::vector<_elementType> >
99 typedef std::vector<_elementType> DataType;
101 static void SerializeText(
const DataType & data, Json::Value & jsonValue)
103 typedef typename DataType::const_iterator const_iterator;
104 const const_iterator end = data.end();
106 for (const_iterator iter = data.begin();
108 ++iter, ++jsonIndex) {
109 cmnDataJSON<_elementType>::SerializeText(*iter, jsonValue[jsonIndex]);
113 static void DeSerializeText(std::vector<_elementType> & data,
const Json::Value & jsonValue)
throw (std::runtime_error)
116 data.resize(jsonValue.size());
117 typedef typename DataType::iterator iterator;
118 const iterator end = data.end();
120 for (iterator iter = data.begin();
122 ++iter, ++jsonIndex) {
123 cmnDataJSON<_elementType>::DeSerializeText(*iter, jsonValue[jsonIndex]);
129 template <
class _elementType,
int _size>
130 class cmnDataJSON<_elementType[_size] >
133 typedef _elementType * pointer;
134 typedef const _elementType * const_pointer;
136 static void SerializeText(const_pointer data, Json::Value & jsonValue)
138 const_pointer ptr = data;
139 for (
int index = 0; index < _size; ++index, ++ptr) {
140 cmnDataJSON<_elementType>::SerializeText(*ptr, jsonValue[index]);
144 static void DeSerializeText(pointer data,
const Json::Value & jsonValue)
throw (std::runtime_error)
146 if (jsonValue.size() != _size) {
147 cmnThrow(
"cmnDataJSON<c-array>::DeSerializeText: vector sizes don't match");
150 for (
int index = 0; index < _size; ++index, ++ptr) {
151 cmnDataJSON<_elementType>::DeSerializeText(*ptr, jsonValue[index]);
156 #endif // CISST_HAS_JSON
158 #endif // _cmnDataFunctionsJSON_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
Macros to export the symbols of cisstCommon (in a Dll).
#define cmnThrow(a)
Definition: MinimalCmn.h:4
Declaration of the template function cmnThrow.