| 1 |
// Copyright 2019 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "nlohmann_json_serializer.h" |
| 16 |
|
| 17 |
#include "null_json_serializer.h" |
| 18 |
|
| 19 |
// Disable JSON exceptions. We should be guarding against any exceptions being |
| 20 |
// fired in this file. |
| 21 |
#define JSON_NOEXCEPTION 1 |
| 22 |
#include <nlohmann/json.hpp> |
| 23 |
|
| 24 |
namespace dap { |
| 25 |
namespace json { |
| 26 |
|
| 27 |
NlohmannDeserializer::NlohmannDeserializer(const std::string& str) |
| 28 |
: json(new nlohmann::json(nlohmann::json::parse(str, nullptr, false))), |
| 29 |
ownsJson(true) {} |
| 30 |
|
| 31 |
NlohmannDeserializer::NlohmannDeserializer(const nlohmann::json* json) |
| 32 |
: json(json), ownsJson(false) {} |
| 33 |
|
| 34 |
NlohmannDeserializer::~NlohmannDeserializer() { |
| 35 |
if (ownsJson) { |
| 36 |
delete json; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
bool NlohmannDeserializer::deserialize(dap::boolean* v) const { |
| 41 |
if (!json->is_boolean()) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
*v = json->get<bool>(); |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
bool NlohmannDeserializer::deserialize(dap::integer* v) const { |
| 49 |
if (!json->is_number_integer()) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
*v = json->get<int64_t>(); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
bool NlohmannDeserializer::deserialize(dap::number* v) const { |
| 57 |
if (!json->is_number()) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
*v = json->get<double>(); |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
bool NlohmannDeserializer::deserialize(dap::string* v) const { |
| 65 |
if (!json->is_string()) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
*v = json->get<std::string>(); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
bool NlohmannDeserializer::deserialize(dap::object* v) const { |
| 73 |
v->reserve(json->size()); |
| 74 |
for (auto& el : json->items()) { |
| 75 |
NlohmannDeserializer d(&el.value()); |
| 76 |
dap::any val; |
| 77 |
if (!d.deserialize(&val)) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
(*v)[el.key()] = val; |
| 81 |
} |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
bool NlohmannDeserializer::deserialize(dap::any* v) const { |
| 86 |
if (json->is_boolean()) { |
| 87 |
*v = dap::boolean(json->get<bool>()); |
| 88 |
} else if (json->is_number_float()) { |
| 89 |
*v = dap::number(json->get<double>()); |
| 90 |
} else if (json->is_number_integer()) { |
| 91 |
*v = dap::integer(json->get<int64_t>()); |
| 92 |
} else if (json->is_string()) { |
| 93 |
*v = json->get<std::string>(); |
| 94 |
} else if (json->is_object()) { |
| 95 |
dap::object obj; |
| 96 |
if (!deserialize(&obj)) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
*v = obj; |
| 100 |
} else if (json->is_array()) { |
| 101 |
dap::array<any> arr; |
| 102 |
if (!deserialize(&arr)) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
*v = arr; |
| 106 |
} else if (json->is_null()) { |
| 107 |
*v = null(); |
| 108 |
} else { |
| 109 |
return false; |
| 110 |
} |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
size_t NlohmannDeserializer::count() const { |
| 115 |
return json->size(); |
| 116 |
} |
| 117 |
|
| 118 |
bool NlohmannDeserializer::array( |
| 119 |
const std::function<bool(dap::Deserializer*)>& cb) const { |
| 120 |
if (!json->is_array()) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
for (size_t i = 0; i < json->size(); i++) { |
| 124 |
NlohmannDeserializer d(&(*json)[i]); |
| 125 |
if (!cb(&d)) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
} |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
bool NlohmannDeserializer::field( |
| 133 |
const std::string& name, |
| 134 |
const std::function<bool(dap::Deserializer*)>& cb) const { |
| 135 |
if (!json->is_structured()) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
auto it = json->find(name); |
| 139 |
if (it == json->end()) { |
| 140 |
return cb(&NullDeserializer::instance); |
| 141 |
} |
| 142 |
auto obj = *it; |
| 143 |
NlohmannDeserializer d(&obj); |
| 144 |
return cb(&d); |
| 145 |
} |
| 146 |
|
| 147 |
NlohmannSerializer::NlohmannSerializer() |
| 148 |
: json(new nlohmann::json()), ownsJson(true) {} |
| 149 |
|
| 150 |
NlohmannSerializer::NlohmannSerializer(nlohmann::json* json) |
| 151 |
: json(json), ownsJson(false) {} |
| 152 |
|
| 153 |
NlohmannSerializer::~NlohmannSerializer() { |
| 154 |
if (ownsJson) { |
| 155 |
delete json; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
std::string NlohmannSerializer::dump() const { |
| 160 |
return json->dump(); |
| 161 |
} |
| 162 |
|
| 163 |
bool NlohmannSerializer::serialize(dap::boolean v) { |
| 164 |
*json = (bool)v; |
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
bool NlohmannSerializer::serialize(dap::integer v) { |
| 169 |
*json = (int64_t)v; |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
bool NlohmannSerializer::serialize(dap::number v) { |
| 174 |
*json = (double)v; |
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
bool NlohmannSerializer::serialize(const dap::string& v) { |
| 179 |
*json = v; |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
bool NlohmannSerializer::serialize(const dap::object& v) { |
| 184 |
if (!json->is_object()) { |
| 185 |
*json = nlohmann::json::object(); |
| 186 |
} |
| 187 |
for (auto& it : v) { |
| 188 |
NlohmannSerializer s(&(*json)[it.first]); |
| 189 |
if (!s.serialize(it.second)) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
} |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
bool NlohmannSerializer::serialize(const dap::any& v) { |
| 197 |
if (v.is<dap::boolean>()) { |
| 198 |
*json = (bool)v.get<dap::boolean>(); |
| 199 |
} else if (v.is<dap::integer>()) { |
| 200 |
*json = (int64_t)v.get<dap::integer>(); |
| 201 |
} else if (v.is<dap::number>()) { |
| 202 |
*json = (double)v.get<dap::number>(); |
| 203 |
} else if (v.is<dap::string>()) { |
| 204 |
*json = v.get<dap::string>(); |
| 205 |
} else if (v.is<dap::object>()) { |
| 206 |
// reachable if dap::object nested is inside other dap::object |
| 207 |
return serialize(v.get<dap::object>()); |
| 208 |
} else if (v.is<dap::null>()) { |
| 209 |
} else { |
| 210 |
// reachable if array or custom serialized type is nested inside other |
| 211 |
auto type = get_any_type(v); |
| 212 |
auto value = get_any_val(v); |
| 213 |
if (type && value) { |
| 214 |
return type->serialize(this, value); |
| 215 |
} |
| 216 |
return false; |
| 217 |
} |
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
bool NlohmannSerializer::array( |
| 222 |
size_t count, |
| 223 |
const std::function<bool(dap::Serializer*)>& cb) { |
| 224 |
*json = std::vector<int>(); |
| 225 |
for (size_t i = 0; i < count; i++) { |
| 226 |
NlohmannSerializer s(&(*json)[i]); |
| 227 |
if (!cb(&s)) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
} |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
bool NlohmannSerializer::object( |
| 235 |
const std::function<bool(dap::FieldSerializer*)>& cb) { |
| 236 |
struct FS : public FieldSerializer { |
| 237 |
nlohmann::json* const json; |
| 238 |
|
| 239 |
FS(nlohmann::json* json) : json(json) {} |
| 240 |
bool field(const std::string& name, const SerializeFunc& cb) override { |
| 241 |
NlohmannSerializer s(&(*json)[name]); |
| 242 |
auto res = cb(&s); |
| 243 |
if (s.removed) { |
| 244 |
json->erase(name); |
| 245 |
} |
| 246 |
return res; |
| 247 |
} |
| 248 |
}; |
| 249 |
|
| 250 |
*json = nlohmann::json({}, false, nlohmann::json::value_t::object); |
| 251 |
FS fs{json}; |
| 252 |
return cb(&fs); |
| 253 |
} |
| 254 |
|
| 255 |
void NlohmannSerializer::remove() { |
| 256 |
removed = true; |
| 257 |
} |
| 258 |
|
| 259 |
} // namespace json |
| 260 |
} // namespace dap |