olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
SerializerWrapper.h
1/*
2 * Copyright (C) 2019-2021 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 <utility>
26#include <vector>
27
28#include <olp/core/porting/optional.h>
29#include <rapidjson/document.h>
30
31namespace olp {
32namespace serializer {
33
34inline void to_json(const std::string& x, rapidjson::Value& value,
35 rapidjson::Document::AllocatorType& allocator) {
36 value.SetString(rapidjson::StringRef(x.c_str(), x.size()), allocator);
37}
38
39inline void to_json(int32_t x, rapidjson::Value& value,
40 rapidjson::Document::AllocatorType&) {
41 value.SetInt(x);
42}
43
44inline void to_json(int64_t x, rapidjson::Value& value,
45 rapidjson::Document::AllocatorType&) {
46 value.SetInt64(x);
47}
48
49inline void to_json(double x, rapidjson::Value& value,
50 rapidjson::Document::AllocatorType&) {
51 value.SetDouble(x);
52}
53inline void to_json(bool x, rapidjson::Value& value,
54 rapidjson::Document::AllocatorType&) {
55 value.SetBool(x);
56}
57
58inline void to_json(const std::shared_ptr<std::vector<unsigned char>>& x,
59 rapidjson::Value& value,
60 rapidjson::Document::AllocatorType& allocator) {
61 value.SetString(reinterpret_cast<char*>(x->data()),
62 static_cast<rapidjson::SizeType>(x->size()), allocator);
63}
64
65template <typename T>
66inline void to_json(const porting::optional<T>& x, rapidjson::Value& value,
67 rapidjson::Document::AllocatorType& allocator) {
68 if (x) {
69 to_json(*x, value, allocator);
70 } else {
71 value.SetNull();
72 }
73}
74
75template <typename T>
76inline void to_json(const std::map<std::string, T>& x, rapidjson::Value& value,
77 rapidjson::Document::AllocatorType& allocator) {
78 value.SetObject();
79 for (auto itr = x.begin(); itr != x.end(); ++itr) {
80 const auto& key = itr->first;
81 rapidjson::Value item_value;
82 to_json(itr->second, item_value, allocator);
83 value.AddMember(rapidjson::StringRef(key.c_str(), key.size()),
84 std::move(item_value), allocator);
85 }
86}
87
88template <typename T>
89inline void to_json(const std::vector<T>& x, rapidjson::Value& value,
90 rapidjson::Document::AllocatorType& allocator) {
91 value.SetArray();
92 for (typename std::vector<T>::const_iterator itr = x.begin(); itr != x.end();
93 ++itr) {
94 rapidjson::Value item_value;
95 to_json(*itr, item_value, allocator);
96 value.PushBack(std::move(item_value), allocator);
97 }
98}
99
100template <typename T>
101inline void serialize(const std::string& key, const T& x,
102 rapidjson::Value& value,
103 rapidjson::Document::AllocatorType& allocator) {
104 rapidjson::Value key_value;
105 to_json(key, key_value, allocator);
106 rapidjson::Value item_value;
107 to_json(x, item_value, allocator);
108 if (!item_value.IsNull()) {
109 value.AddMember(std::move(key_value), std::move(item_value), allocator);
110 }
111}
112
113} // namespace serializer
114} // namespace olp
Rules all the other namespaces.
Definition AppleSignInProperties.h:24