cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Anton Deguet
6 Created on: 2011-06-27
7
8 (C) Copyright 2011-2025 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19
20#pragma once
21#ifndef _cmnDataFunctionsJSON_h
22#define _cmnDataFunctionsJSON_h
23
24#include <string.h> // for memcpy
25#include <iostream>
26#include <limits>
27#include <vector>
28#include <cisstConfig.h> // for CISST_HAS_JSON
31
32// always include last
34
35#if CISST_HAS_JSON
36
37#include <json/json.h>
38
39template <typename _elementType>
40class cmnDataJSON
41{
42public:
43 typedef _elementType DataType;
44 static void SerializeText(const DataType & data, Json::Value & jsonValue);
45 static void DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
46};
47
48template <>
49void CISST_EXPORT cmnDataJSON<double>::SerializeText(const DataType & data, Json::Value & jsonValue);
50template <>
51void CISST_EXPORT cmnDataJSON<double>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
52
53template <>
54void CISST_EXPORT cmnDataJSON<float>::SerializeText(const DataType & data, Json::Value & jsonValue);
55template <>
56void CISST_EXPORT cmnDataJSON<float>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
57
58template <>
59void CISST_EXPORT cmnDataJSON<char>::SerializeText(const DataType & data, Json::Value & jsonValue);
60template <>
61void CISST_EXPORT cmnDataJSON<char>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
62
63template <>
64void CISST_EXPORT cmnDataJSON<int>::SerializeText(const DataType & data, Json::Value & jsonValue);
65template <>
66void CISST_EXPORT cmnDataJSON<int>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
67
68template <>
69void CISST_EXPORT cmnDataJSON<unsigned int>::SerializeText(const DataType & data, Json::Value & jsonValue);
70template <>
71void CISST_EXPORT cmnDataJSON<unsigned int>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
72
73template <>
74void CISST_EXPORT cmnDataJSON<long int>::SerializeText(const DataType & data, Json::Value & jsonValue);
75template <>
76void CISST_EXPORT cmnDataJSON<long int>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
77
78template <>
79void CISST_EXPORT cmnDataJSON<unsigned long int>::SerializeText(const DataType & data, Json::Value & jsonValue);
80template <>
81void CISST_EXPORT cmnDataJSON<unsigned long int>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
82
83template <>
84void CISST_EXPORT cmnDataJSON<unsigned long long int>::SerializeText(const DataType & data, Json::Value & jsonValue);
85template <>
86void CISST_EXPORT cmnDataJSON<unsigned long long int>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
87
88template <>
89void CISST_EXPORT cmnDataJSON<bool>::SerializeText(const DataType & data, Json::Value & jsonValue);
90template <>
91void CISST_EXPORT cmnDataJSON<bool>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
92
93template <>
94void CISST_EXPORT cmnDataJSON<std::string>::SerializeText(const DataType & data, Json::Value & jsonValue);
95template <>
96void CISST_EXPORT cmnDataJSON<std::string>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
97
98// fake support for cmnData<Json::Value>
99template <>
100class cmnData<Json::Value> {
101public:
102 static std::string HumanReadable(Json::Value const&) { return ""; };
103
104 static void Copy(Json::Value & data, const Json::Value & source) { data = source; };
105
106 static std::string SerializeDescription(const Json::Value &, char, const std::string &) { return ""; };
107
108 static void SerializeText(const Json::Value &, std::ostream&, const char) {};
109
110 static void DeSerializeText(Json::Value &, std::istream &, const char) {};
111
112 static void SerializeBinary(const Json::Value &, std::ostream &) {};
113
114 static void DeSerializeBinary(Json::Value &, std::istream &, const cmnDataFormat &, const cmnDataFormat &) {};
115
116 static std::string ScalarDescription(const Json::Value &, size_t, const std::string &) { return ""; };
117
118 static double Scalar(const Json::Value &, size_t) { return 0.0; };
119
120 static size_t ScalarNumber(const Json::Value &) { return 0; };
121
122 static bool ScalarNumberIsFixed(const Json::Value &) { return true; };
123};
124
125template <>
126void CISST_EXPORT cmnDataJSON<Json::Value>::SerializeText(const DataType & data, Json::Value & jsonValue);
127template <>
128void CISST_EXPORT cmnDataJSON<Json::Value>::DeSerializeText(DataType & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error);
129
130
131template <class _elementType>
132class cmnDataJSON<std::vector<_elementType> >
133{
134public:
135 typedef std::vector<_elementType> DataType;
136
137 static void SerializeText(const DataType & data, Json::Value & jsonValue)
138 {
139 typedef typename DataType::const_iterator const_iterator;
140 const const_iterator end = data.end();
141 int jsonIndex = 0;
142 for (const_iterator iter = data.begin();
143 iter != end;
144 ++iter, ++jsonIndex) {
145 cmnDataJSON<_elementType>::SerializeText(*iter, jsonValue[jsonIndex]);
146 }
147 }
148
149 static void DeSerializeText(std::vector<_elementType> & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error)
150 {
151 // get the vector size from JSON and resize
152 data.resize(jsonValue.size());
153 typedef typename DataType::iterator iterator;
154 const iterator end = data.end();
155 int jsonIndex = 0;
156 for (iterator iter = data.begin();
157 iter != end;
158 ++iter, ++jsonIndex) {
159 cmnDataJSON<_elementType>::DeSerializeText(*iter, jsonValue[jsonIndex]);
160 }
161 }
162};
163
164
165template <class _elementType, int _size>
166class cmnDataJSON<_elementType[_size] >
167{
168public:
169 typedef _elementType * pointer;
170 typedef const _elementType * const_pointer;
171
172 static void SerializeText(const_pointer data, Json::Value & jsonValue)
173 {
174 const_pointer ptr = data;
175 for (int index = 0; index < _size; ++index, ++ptr) {
176 cmnDataJSON<_elementType>::SerializeText(*ptr, jsonValue[index]);
177 }
178 }
179
180 static void DeSerializeText(pointer data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error)
181 {
182 if (jsonValue.size() != _size) {
183 cmnThrow("cmnDataJSON<c-array>::DeSerializeText: vector sizes don't match");
184 }
185 pointer ptr = data;
186 for (int index = 0; index < _size; ++index, ++ptr) {
187 cmnDataJSON<_elementType>::DeSerializeText(*ptr, jsonValue[index]);
188 }
189 }
190};
191
192
193template <typename _elementType>
194void cmnDataSerializeTextJSON(const _elementType & data, Json::Value & jsonValue) {
195 cmnDataJSON<_elementType>::SerializeText(data, jsonValue);
196}
197
198template <typename _elementType>
199std::string cmnDataSerializeTextJSON(const _elementType & data) {
200 Json::Value jsonValue;
201 cmnDataJSON<_elementType>::SerializeText(data, jsonValue);
202 Json::StreamWriterBuilder builder;
203 return Json::writeString(builder, jsonValue);
204}
205
206template <typename _elementType>
207void cmnDataDeSerializeTextJSON(_elementType & data, const Json::Value & jsonValue) {
208 cmnDataJSON<_elementType>::DeSerializeText(data, jsonValue);
209}
210
211#endif // CISST_HAS_JSON
212
213#endif // _cmnDataFunctionsJSON_h
Definition cmnDataFunctions.h:51
static void Copy(DataType &data, const DataType &source)
static size_t DeSerializeBinary(DataType &data, const char *buffer, size_t bufferSize, const cmnDataFormat &CMN_UNUSED(localFormat), const cmnDataFormat &CMN_UNUSED(remoteFormat))
static std::string SerializeDescription(const DataType &data, const char delimiter, const std::string &userDescription="")
static std::string HumanReadable(const DataType &data)
static bool ScalarNumberIsFixed(const DataType &data)
static size_t SerializeBinary(const DataType &data, char *buffer, size_t bufferSize)
static std::string static ScalarDescription(const DataType &data, const size_t index, const std::string &userDescription="") CISST_THROW(std double static Scalar(const DataType &data, const size_t index) CISST_THROW(std size_t ScalarNumber(const DataType &data)
Definition cmnDataFunctions.h:162
Macros to export the symbols of cisstCommon (in a Dll).
#define CISST_EXPORT
Definition cmnExportMacros.h:50
#define CISST_THROW(exceptionParameter)
Somewhat portable compilation warning message. This works with very recent versions of gcc (4....
Definition cmnPortability.h:559
Declaration of the template function cmnThrow.
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
STL namespace.
const_iterator end(void) const
Definition vctDynamicConstMatrixBase.h:209
OwnerType::const_iterator const_iterator
Definition vctDynamicConstMatrixBase.h:95
OwnerType::iterator iterator
Definition vctDynamicConstMatrixBase.h:92