olp-cpp-sdk  1.22.0
JsonParser.h
1 /*
2  * Copyright (C) 2019-2024 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 <memory>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 
27 #include <rapidjson/document.h>
28 #include <rapidjson/istreamwrapper.h>
29 
30 #include "ParserWrapper.h"
31 
32 namespace olp {
33 namespace parser {
34 template <typename T>
35 inline T parse(const std::string& json) {
36  rapidjson::Document doc;
37  doc.Parse(json.c_str());
38  T result{};
39  if (doc.IsObject() || doc.IsArray()) {
40  from_json(doc, result);
41  }
42  return result;
43 }
44 
45 template <typename T>
46 inline T parse(std::stringstream& json_stream, bool& res) {
47  res = false;
48  rapidjson::Document doc;
49  rapidjson::IStreamWrapper stream(json_stream);
50  doc.ParseStream(stream);
51  T result{};
52  if (doc.IsObject() || doc.IsArray()) {
53  from_json(doc, result);
54  res = true;
55  }
56  return result;
57 }
58 
59 template <typename T>
60 inline T parse(std::stringstream& json_stream) {
61  bool res = true;
62  return parse<T>(json_stream, res);
63 }
64 
65 template <typename T>
66 inline T parse(const std::shared_ptr<std::vector<unsigned char>>& json_bytes) {
67  rapidjson::Document doc;
68  doc.Parse(reinterpret_cast<char*>(json_bytes->data()), json_bytes->size());
69  T result{};
70  if (doc.IsObject() || doc.IsArray()) {
71  from_json(doc, result);
72  }
73  return result;
74 }
75 
76 } // namespace parser
77 } // namespace olp
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24