cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnDataFunctionsJSON.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet
7  Created on: 2011-06-27
8 
9  (C) Copyright 2011-2013 Johns Hopkins University (JHU), All Rights Reserved.
10 
11 --- begin cisst license - do not edit ---
12 
13 This software is provided "as is" under an open source license, with
14 no warranty. The complete license can be found in license.txt and
15 http://www.cisst.org/cisst/license.txt.
16 
17 --- end cisst license ---
18 
19 */
20 
21 
22 #pragma once
23 #ifndef _cmnDataFunctionsJSON_h
24 #define _cmnDataFunctionsJSON_h
25 
26 #include <string.h> // for memcpy
27 #include <iostream>
28 #include <limits>
29 #include <vector>
30 #include <cisstConfig.h> // for CISST_HAS_JSON
31 #include <cisstCommon/cmnThrow.h>
32 
33 // always include last
34 #include <cisstCommon/cmnExport.h>
35 
36 #if CISST_HAS_JSON
37 
38 #include <json/json.h>
39 
40 template <typename _elementType>
41 class cmnDataJSON
42 {
43 public:
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);
47 };
48 
49 template <>
50 void CISST_EXPORT cmnDataJSON<double>::SerializeText(const DataType & data, Json::Value & jsonValue);
51 template <>
52 void CISST_EXPORT cmnDataJSON<double>::DeSerializeText(DataType & data, const Json::Value & jsonValue) throw (std::runtime_error);
53 
54 template <>
55 void CISST_EXPORT cmnDataJSON<float>::SerializeText(const DataType & data, Json::Value & jsonValue);
56 template <>
57 void CISST_EXPORT cmnDataJSON<float>::DeSerializeText(DataType & data, const Json::Value & jsonValue) throw (std::runtime_error);
58 
59 template <>
60 void CISST_EXPORT cmnDataJSON<char>::SerializeText(const DataType & data, Json::Value & jsonValue);
61 template <>
62 void CISST_EXPORT cmnDataJSON<char>::DeSerializeText(DataType & data, const Json::Value & jsonValue) throw (std::runtime_error);
63 
64 template <>
65 void CISST_EXPORT cmnDataJSON<int>::SerializeText(const DataType & data, Json::Value & jsonValue);
66 template <>
67 void CISST_EXPORT cmnDataJSON<int>::DeSerializeText(DataType & data, const Json::Value & jsonValue) throw (std::runtime_error);
68 
69 template <>
70 void CISST_EXPORT cmnDataJSON<unsigned int>::SerializeText(const DataType & data, Json::Value & jsonValue);
71 template <>
72 void CISST_EXPORT cmnDataJSON<unsigned int>::DeSerializeText(DataType & data, const Json::Value & jsonValue) throw (std::runtime_error);
73 
74 template <>
75 void CISST_EXPORT cmnDataJSON<unsigned long int>::SerializeText(const DataType & data, Json::Value & jsonValue);
76 template <>
77 void CISST_EXPORT cmnDataJSON<unsigned long int>::DeSerializeText(DataType & data, const Json::Value & jsonValue) throw (std::runtime_error);
78 
79 template <>
80 void CISST_EXPORT cmnDataJSON<unsigned long long int>::SerializeText(const DataType & data, Json::Value & jsonValue);
81 template <>
82 void CISST_EXPORT cmnDataJSON<unsigned long long int>::DeSerializeText(DataType & data, const Json::Value & jsonValue) throw (std::runtime_error);
83 
84 template <>
85 void CISST_EXPORT cmnDataJSON<bool>::SerializeText(const DataType & data, Json::Value & jsonValue);
86 template <>
87 void CISST_EXPORT cmnDataJSON<bool>::DeSerializeText(DataType & data, const Json::Value & jsonValue) throw (std::runtime_error);
88 
89 template <>
90 void CISST_EXPORT cmnDataJSON<std::string>::SerializeText(const DataType & data, Json::Value & jsonValue);
91 template <>
92 void CISST_EXPORT cmnDataJSON<std::string>::DeSerializeText(DataType & data, const Json::Value & jsonValue) throw (std::runtime_error);
93 
94 
95 template <class _elementType>
96 class cmnDataJSON<std::vector<_elementType> >
97 {
98 public:
99  typedef std::vector<_elementType> DataType;
100 
101  static void SerializeText(const DataType & data, Json::Value & jsonValue)
102  {
103  typedef typename DataType::const_iterator const_iterator;
104  const const_iterator end = data.end();
105  int jsonIndex = 0;
106  for (const_iterator iter = data.begin();
107  iter != end;
108  ++iter, ++jsonIndex) {
109  cmnDataJSON<_elementType>::SerializeText(*iter, jsonValue[jsonIndex]);
110  }
111  }
112 
113  static void DeSerializeText(std::vector<_elementType> & data, const Json::Value & jsonValue) throw (std::runtime_error)
114  {
115  // get the vector size from JSON and resize
116  data.resize(jsonValue.size());
117  typedef typename DataType::iterator iterator;
118  const iterator end = data.end();
119  int jsonIndex = 0;
120  for (iterator iter = data.begin();
121  iter != end;
122  ++iter, ++jsonIndex) {
123  cmnDataJSON<_elementType>::DeSerializeText(*iter, jsonValue[jsonIndex]);
124  }
125  }
126 };
127 
128 
129 template <class _elementType, int _size>
130 class cmnDataJSON<_elementType[_size] >
131 {
132 public:
133  typedef _elementType * pointer;
134  typedef const _elementType * const_pointer;
135 
136  static void SerializeText(const_pointer data, Json::Value & jsonValue)
137  {
138  const_pointer ptr = data;
139  for (int index = 0; index < _size; ++index, ++ptr) {
140  cmnDataJSON<_elementType>::SerializeText(*ptr, jsonValue[index]);
141  }
142  }
143 
144  static void DeSerializeText(pointer data, const Json::Value & jsonValue) throw (std::runtime_error)
145  {
146  if (jsonValue.size() != _size) {
147  cmnThrow("cmnDataJSON<c-array>::DeSerializeText: vector sizes don't match");
148  }
149  pointer ptr = data;
150  for (int index = 0; index < _size; ++index, ++ptr) {
151  cmnDataJSON<_elementType>::DeSerializeText(*ptr, jsonValue[index]);
152  }
153  }
154 };
155 
156 #endif // CISST_HAS_JSON
157 
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.