summaryrefslogtreecommitdiffstats
path: root/Utilities/cmcppdap/src/nlohmann_json_serializer.h
blob: 38e47c9b766f68514dcf4a463716031708ae681a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef dap_nlohmann_json_serializer_h
#define dap_nlohmann_json_serializer_h

#include "dap/protocol.h"
#include "dap/serialization.h"
#include "dap/types.h"

#include <nlohmann/json_fwd.hpp>

namespace dap {
namespace json {

struct NlohmannDeserializer : public dap::Deserializer {
  explicit NlohmannDeserializer(const std::string&);
  ~NlohmannDeserializer();

  // dap::Deserializer compliance
  bool deserialize(boolean* v) const override;
  bool deserialize(integer* v) const override;
  bool deserialize(number* v) const override;
  bool deserialize(string* v) const override;
  bool deserialize(object* v) const override;
  bool deserialize(any* v) const override;
  size_t count() const override;
  bool array(const std::function<bool(dap::Deserializer*)>&) const override;
  bool field(const std::string& name,
             const std::function<bool(dap::Deserializer*)>&) const override;

  // Unhide base overloads
  template <typename T>
  inline bool field(const std::string& name, T* v) {
    return dap::Deserializer::field(name, v);
  }

  template <typename T,
            typename = std::enable_if<TypeOf<T>::has_custom_serialization>>
  inline bool deserialize(T* v) const {
    return dap::Deserializer::deserialize(v);
  }

  template <typename T>
  inline bool deserialize(dap::array<T>* v) const {
    return dap::Deserializer::deserialize(v);
  }

  template <typename T>
  inline bool deserialize(dap::optional<T>* v) const {
    return dap::Deserializer::deserialize(v);
  }

  template <typename T0, typename... Types>
  inline bool deserialize(dap::variant<T0, Types...>* v) const {
    return dap::Deserializer::deserialize(v);
  }

  template <typename T>
  inline bool field(const std::string& name, T* v) const {
    return dap::Deserializer::deserialize(name, v);
  }

 private:
  NlohmannDeserializer(const nlohmann::json*);
  const nlohmann::json* const json;
  const bool ownsJson;
};

struct NlohmannSerializer : public dap::Serializer {
  NlohmannSerializer();
  ~NlohmannSerializer();

  std::string dump() const;

  // dap::Serializer compliance
  bool serialize(boolean v) override;
  bool serialize(integer v) override;
  bool serialize(number v) override;
  bool serialize(const string& v) override;
  bool serialize(const dap::object& v) override;
  bool serialize(const any& v) override;
  bool array(size_t count,
             const std::function<bool(dap::Serializer*)>&) override;
  bool object(const std::function<bool(dap::FieldSerializer*)>&) override;
  void remove() override;

  // Unhide base overloads
  template <typename T,
            typename = std::enable_if<TypeOf<T>::has_custom_serialization>>
  inline bool serialize(const T& v) {
    return dap::Serializer::serialize(v);
  }

  template <typename T>
  inline bool serialize(const dap::array<T>& v) {
    return dap::Serializer::serialize(v);
  }

  template <typename T>
  inline bool serialize(const dap::optional<T>& v) {
    return dap::Serializer::serialize(v);
  }

  template <typename T0, typename... Types>
  inline bool serialize(const dap::variant<T0, Types...>& v) {
    return dap::Serializer::serialize(v);
  }

  inline bool serialize(const char* v) { return dap::Serializer::serialize(v); }

 private:
  NlohmannSerializer(nlohmann::json*);
  nlohmann::json* const json;
  const bool ownsJson;
  bool removed = false;
};

}  // namespace json
}  // namespace dap

#endif  // dap_nlohmann_json_serializer_h