olp-cpp-sdk  1.22.0
ParserWrapper.h
1 /*
2  * Copyright (C) 2019-2023 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include <rapidjson/rapidjson.h>
28 #include <boost/optional.hpp>
29 
30 namespace olp {
31 namespace parser {
32 
33 inline void from_json(const rapidjson::Value& value, std::string& x) {
34  x = value.GetString();
35 }
36 
37 inline void from_json(const rapidjson::Value& value, int32_t& x) {
38  x = value.GetInt();
39 }
40 
41 inline void from_json(const rapidjson::Value& value, int64_t& x) {
42  x = value.GetInt64();
43 }
44 
45 inline void from_json(const rapidjson::Value& value, double& x) {
46  x = value.GetDouble();
47 }
48 
49 inline void from_json(const rapidjson::Value& value, bool& x) {
50  x = value.GetBool();
51 }
52 
53 inline void from_json(const rapidjson::Value& value,
54  std::shared_ptr<std::vector<unsigned char>>& x) {
55  std::string s = value.GetString();
56  x = std::make_shared<std::vector<unsigned char>>(s.begin(), s.end());
57 }
58 
59 template <typename T>
60 inline void from_json(const rapidjson::Value& value, boost::optional<T>& x) {
61  T result = T();
62  from_json(value, result);
63  x = result;
64 }
65 
66 template <typename T>
67 inline void from_json(const rapidjson::Value& value,
68  std::map<std::string, T>& results) {
69  for (rapidjson::Value::ConstMemberIterator itr = value.MemberBegin();
70  itr != value.MemberEnd(); ++itr) {
71  std::string key;
72  from_json(itr->name, key);
73  from_json(itr->value, results[key]);
74  }
75 }
76 
77 template <typename T>
78 inline void from_json(const rapidjson::Value& value, std::vector<T>& results) {
79  for (rapidjson::Value::ConstValueIterator itr = value.Begin();
80  itr != value.End(); ++itr) {
81  T result;
82  from_json(*itr, result);
83  results.emplace_back(std::move(result));
84  }
85 }
86 
87 template <typename T>
88 inline T parse(const rapidjson::Value& value, const std::string& name) {
89  T result = T();
90  rapidjson::Value::ConstMemberIterator itr = value.FindMember(name.c_str());
91  if (itr != value.MemberEnd()) {
92  from_json(itr->value, result);
93  }
94  return result;
95 }
96 
97 } // namespace parser
98 } // namespace olp
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24