$darkmode
json.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
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  */
24 #pragma once
25 
26 #include <string> // for string
27 #include <utility> // for move
28 
29 #include <fable/schema/interface.hpp> // for Base<>
30 
31 namespace fable::schema {
32 
45 template <typename T>
46 class FromJson : public Base<FromJson<T>> {
47  public: // Types and Constructors
48  using Type = T;
49 
50  FromJson(Type* ptr, JsonType t, std::string desc)
51  : Base<FromJson<T>>(t, std::move(desc)), ptr_(ptr) {}
52 
53  public: // Overrides
54  [[nodiscard]] Json json_schema() const override {
55  Json j{
56  {"type", this->type_string()},
57  };
58  this->augment_schema(j);
59  return j;
60  }
61 
62  bool validate(const Conf& c, std::optional<SchemaError>& err) const override {
63  return this->validate_type(c, err);
64  }
65 
66  using Interface::to_json;
67  void to_json(Json& j) const override {
68  assert(ptr_ != nullptr);
69  serialize_into(j, *ptr_);
70  }
71 
72  void from_conf(const Conf& c) override {
73  assert(ptr_ != nullptr);
74  deserialize_into(c, *ptr_);
75  }
76 
77  void reset_ptr() override { ptr_ = nullptr; }
78 
79  [[nodiscard]] Json serialize(const Type& x) const {
80  return Json(x);
81  }
82 
83  void serialize_into(Json& j, const Type& x) const {
84  ::nlohmann::adl_serializer<Type>::to_json(j, x);
85  }
86 
87  template<typename = std::enable_if<std::is_default_constructible_v<Type>>>
88  [[nodiscard]] Type deserialize(const Conf& c) const {
89  return c->get<Type>();
90  }
91 
92  void deserialize_into(const Conf& c, Type& x) const {
93  c->get_to(x);
94  }
95 
96  private:
97  Type* ptr_{nullptr};
98 };
99 
100 template <typename T>
101 inline FromJson<T> make_schema(T* ptr, JsonType t, std::string desc) {
102  return FromJson<T>(ptr, t, std::move(desc));
103 }
104 
105 } // namespace fable::schema
Definition: conf.hpp:76
Definition: interface.hpp:398
bool validate_type(const Conf &c, std::optional< SchemaError > &err) const
Definition: interface.hpp:459
std::string type_string() const override
Definition: interface.hpp:419
Definition: json.hpp:46
void reset_ptr() override
Definition: json.hpp:77
void from_conf(const Conf &c) override
Definition: json.hpp:72
void to_json(Json &j) const override
Definition: json.hpp:67
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: json.hpp:62
Json json_schema() const override
Definition: json.hpp:54
virtual Json to_json() const
Definition: interface.hpp:254
nlohmann::json Json
Definition: fable_fwd.hpp:35
nlohmann::detail::value_t JsonType
Definition: fable_fwd.hpp:37