Google APIs Client Library for C++
jsoncpp_data_helpers.h
Go to the documentation of this file.
00001 /*
00002  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
00003  * \license @{
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  * @}
00018  */
00019 
00020 //
00021 // The Json discovery documents specify 64 bit integers as strings, so we
00022 // need to serialize/deserialize them as strings.
00023 
00024 #ifndef APISERVING_CLIENTS_CPP_DATA_JSONCPP_DATA_HELPERS_H_
00025 #define APISERVING_CLIENTS_CPP_DATA_JSONCPP_DATA_HELPERS_H_
00026 
00027 #include <string>
00028 using std::string;
00029 #include "googleapis/client/util/date_time.h"
00030 #include "googleapis/base/integral_types.h"
00031 #include "googleapis/strings/numbers.h"
00032 #include <json/json.h>
00033 namespace googleapis {
00034 
00035 namespace client {
00036 
00037 // Converts a Json::Value into C++ value.
00038 // This just normalizes the third party api so that we dont have to
00039 // special case the name of the method for each different template type.
00040 template<typename T> void ClearCppValueHelper(T* value);
00041 template<typename T>
00042 const T JsonValueToCppValueHelper(const Json::Value& value);
00043 template<typename T>
00044 T JsonValueToMutableCppValueHelper(Json::Value* value);
00045 template<typename T>
00046 void SetJsonValueFromCppValueHelper(const T& value, Json::Value* storage);
00047 template<typename T>
00048 void SetCppValueFromJsonValueHelper(const Json::Value& storage, T* value);
00049 
00050 // Explicit templated get implementation specific to int.
00051 template<>
00052 inline void ClearCppValueHelper<int16>(int16* value) { *value = 0; }
00053 template<>
00054 inline const int16 JsonValueToCppValueHelper<int16>(const Json::Value& value) {
00055   return value.asInt();
00056 }
00057 template<>
00058 inline int16 JsonValueToMutableCppValueHelper<int16>(Json::Value* value) {
00059   return value->asInt();
00060 }
00061 template<>
00062 inline void SetJsonValueFromCppValueHelper<int16>(
00063     const int16& val, Json::Value* storage) {
00064   *storage = val;
00065 }
00066 template<>
00067 inline void SetCppValueFromJsonValueHelper<int16>(
00068     const Json::Value& storage, int16* value) {
00069   *value = storage.asInt();
00070 }
00071 
00072 template<>
00073 inline void ClearCppValueHelper<int32>(int32* value) { *value = 0; }
00074 template<>
00075 inline const int32 JsonValueToCppValueHelper<int32>(const Json::Value& value) {
00076   return value.asInt();
00077 }
00078 template<>
00079 inline int32 JsonValueToMutableCppValueHelper<int32>(Json::Value* value) {
00080   return value->asInt();
00081 }
00082 template<>
00083 inline void SetJsonValueFromCppValueHelper<int32>(
00084      const int32& val, Json::Value* storage) {
00085   *storage = val;
00086 }
00087 template<>
00088 inline void SetCppValueFromJsonValueHelper<int32>(
00089     const Json::Value& storage, int32* value) {
00090   *value = storage.asInt();
00091 }
00092 
00093 template<>
00094 inline void ClearCppValueHelper<int64>(int64* value) { *value = 0L; }
00095 template<>
00096 inline const int64 JsonValueToCppValueHelper<int64>(const Json::Value& value) {
00097   if (value.isNull()) return 0L;
00098   return ParseLeadingInt64Value(value.asCString(), 0L);
00099 }
00100 template<>
00101 inline int64 JsonValueToMutableCppValueHelper<int64>(Json::Value* value) {
00102   return JsonValueToCppValueHelper<int64>(*value);
00103 }
00104 template<>
00105 inline void SetJsonValueFromCppValueHelper<int64>(
00106      const int64& val, Json::Value* storage) {
00107   *storage = SimpleItoa(val).c_str();
00108 }
00109 template<>
00110 inline void SetCppValueFromJsonValueHelper<int64>(
00111     const Json::Value& storage, int64* value) {
00112   *value = JsonValueToCppValueHelper<int64>(storage);
00113 }
00114 
00115 template<>
00116 inline void ClearCppValueHelper<uint16>(uint16* value) { *value = 0; }
00117 template<>
00118 inline const uint16 JsonValueToCppValueHelper<uint16>(
00119     const Json::Value& value) {
00120   return value.asUInt();
00121 }
00122 template<>
00123 inline uint16 JsonValueToMutableCppValueHelper<uint16>(Json::Value* value) {
00124   return value->asUInt();
00125 }
00126 template<>
00127 inline void SetJsonValueFromCppValueHelper<uint16>(
00128      const uint16& val, Json::Value* storage) {
00129   *storage = val;
00130 }
00131 template<>
00132 inline void SetCppValueFromJsonValueHelper<uint16>(
00133     const Json::Value& storage, uint16* value) {
00134   *value = storage.asUInt();
00135 }
00136 
00137 template<>
00138 inline void ClearCppValueHelper<uint32>(uint32* value) { *value = 0; }
00139 template<>
00140 inline const uint32 JsonValueToCppValueHelper<uint32>(
00141     const Json::Value& value) {
00142   return value.asUInt();
00143 }
00144 template<>
00145 inline uint32 JsonValueToMutableCppValueHelper<uint32>(Json::Value* value) {
00146   return value->asUInt();
00147 }
00148 template<>
00149 inline void SetJsonValueFromCppValueHelper<uint32>(
00150      const uint32& val, Json::Value* storage) {
00151   *storage = val;
00152 }
00153 template<>
00154 inline void SetCppValueFromJsonValueHelper<uint32>(
00155     const Json::Value& storage, uint32* value) {
00156   *value = storage.asUInt();
00157 }
00158 
00159 template<>
00160 inline void ClearCppValueHelper<uint64>(uint64* value) { *value = 0L; }
00161 template<>
00162 inline const uint64 JsonValueToCppValueHelper<uint64>(
00163     const Json::Value& value) {
00164   if (value.isNull()) return 0L;
00165   return ParseLeadingUInt64Value(value.asCString(), 0L);
00166 }
00167 template<>
00168 inline uint64 JsonValueToMutableCppValueHelper<uint64>(Json::Value* value) {
00169   return JsonValueToCppValueHelper<uint64>(*value);
00170 }
00171 template<>
00172 inline void SetJsonValueFromCppValueHelper<uint64>(
00173      const uint64& val, Json::Value* storage) {
00174   *storage = SimpleItoa(val).c_str();
00175 }
00176 template<>
00177 inline void SetCppValueFromJsonValueHelper<uint64>(
00178     const Json::Value& storage, uint64* value) {
00179   *value = JsonValueToCppValueHelper<uint64>(storage);
00180 }
00181 
00182 template<>
00183 inline void ClearCppValueHelper<bool>(bool* value) { *value = false; }
00184 template<>
00185 inline const bool JsonValueToCppValueHelper<bool>(const Json::Value& value) {
00186   return value.asBool();
00187 }
00188 template<>
00189 inline bool JsonValueToMutableCppValueHelper<bool>(Json::Value* value) {
00190   return value->asBool();
00191 }
00192 template<>
00193 inline void SetJsonValueFromCppValueHelper<bool>(
00194      const bool& val, Json::Value* storage) {
00195   *storage = val;
00196 }
00197 template<>
00198 inline void SetCppValueFromJsonValueHelper<bool>(
00199     const Json::Value& storage, bool* value) {
00200   *value = storage.asBool();
00201 }
00202 
00203 template<>
00204 inline void ClearCppValueHelper<float>(float* value) { *value = 0; }
00205 template<>
00206 inline const float JsonValueToCppValueHelper<float>(const Json::Value& value) {
00207   return static_cast<float>(value.asDouble());
00208 }
00209 template<>
00210 inline float JsonValueToMutableCppValueHelper<float>(Json::Value* value) {
00211   return static_cast<double>(value->asDouble());
00212 }
00213 template<>
00214 inline void SetJsonValueFromCppValueHelper<float>(
00215      const float& val, Json::Value* storage) {
00216   *storage = val;
00217 }
00218 template<>
00219 inline void SetCppValueFromJsonValueHelper<float>(
00220     const Json::Value& storage, float* value) {
00221   *value = static_cast<float>(storage.asDouble());
00222 }
00223 
00224 template<>
00225 inline void ClearCppValueHelper<double>(double* value) { *value = 0; }
00226 template<>
00227 inline const double JsonValueToCppValueHelper<double>(
00228     const Json::Value& value) {
00229   return value.asDouble();
00230 }
00231 template<>
00232 inline double JsonValueToMutableCppValueHelper<double>(Json::Value* value) {
00233   return value->asDouble();
00234 }
00235 template<>
00236 inline void SetJsonValueFromCppValueHelper<double>(
00237      const double& val, Json::Value* storage) {
00238   *storage = val;
00239 }
00240 template<>
00241 inline void SetCppValueFromJsonValueHelper<double>(
00242     const Json::Value& storage, double* value) {
00243   *value = storage.asDouble();
00244 }
00245 
00246 template<>
00247 inline void ClearCppValueHelper<string>(string* value) { value->clear(); }
00248 template<>
00249 inline const string JsonValueToCppValueHelper<string>(
00250     const Json::Value& value) {
00251   return value.asString();
00252 }
00253 template<>
00254 inline string JsonValueToMutableCppValueHelper<string>(Json::Value* value) {
00255   return value->asString();
00256 }
00257 template<>
00258 inline void SetJsonValueFromCppValueHelper<string>(
00259      const string& val, Json::Value* storage) {
00260   *storage = val.c_str();
00261 }
00262 template<>
00263 inline void SetCppValueFromJsonValueHelper<string>(
00264     const Json::Value& storage, string* value) {
00265   *value = storage.asString();
00266 }
00267 
00268 template<>
00269 inline void ClearCppValueHelper<DateTime>(DateTime* value) {
00270   *value = DateTime();
00271 }
00272 template<>
00273 inline const DateTime JsonValueToCppValueHelper<DateTime>(
00274     const Json::Value& value) {
00275   return DateTime(value.asString());
00276 }
00277 template<>
00278 inline DateTime JsonValueToMutableCppValueHelper<DateTime>(Json::Value* value) {
00279   return DateTime(value->asString());
00280 }
00281 template<>
00282 inline void SetJsonValueFromCppValueHelper<DateTime>(
00283      const DateTime& val, Json::Value* storage) {
00284   *storage = val.ToString().c_str();
00285 }
00286 template<>
00287 inline void SetCppValueFromJsonValueHelper<DateTime>(
00288     const Json::Value& storage, DateTime* value) {
00289   *value = DateTime(storage.asString());
00290 }
00291 
00292 // generic is for objects
00293 template<typename T>
00294 inline void ClearCppValueHelper(T* value) {
00295   *value->MutableStorage() = Json::Value::null;
00296 }
00297 template<typename T>
00298 inline const T JsonValueToCppValueHelper(const Json::Value& value) {
00299   return T(value);
00300 }
00301 template<typename T>
00302 inline T JsonValueToMutableCppValueHelper(Json::Value* value) {
00303   return T(value);
00304 }
00305 template<typename T>
00306 inline void SetJsonValueFromCppValueHelper(
00307      const T& val, Json::Value* storage) {
00308   *storage = val.Storage();
00309 }
00310 template<typename T>
00311 inline void SetCppValueFromJsonValueHelper(
00312     const Json::Value& storage, T* value) {
00313   *value->MutableStorage() = storage;
00314 }
00315 
00316 }  // namespace client
00317 
00318 } // namespace googleapis
00319 #endif  // APISERVING_CLIENTS_CPP_DATA_JSONCPP_DATA_HELPERS_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines