AnCH Framework 0.1
Another C++ Hack Framework
Loading...
Searching...
No Matches
mapper.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3 Copyright (C) 2020 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <string>
23#include <vector>
24#include <map>
25#include <ostream>
26#include <istream>
27#include <functional>
28#include <list>
29#include <set>
30#include <map>
31
32#include "json/genericMapper.hpp"
33#include "json/constants.hpp"
34#include "json/mappingOptions.hpp"
35#include "json/mappingFunctions.hpp"
36#include "json/readerContext.hpp"
37
38
39namespace anch::json {
40
41 // JSON mapper early declaration
42 template<typename T>
43 class ObjectMapper;
44
45 // JSON mapper early declaration
46 template<typename T>
47 class Factory;
48
57 template<typename T>
58 void registerObject(ObjectMapper<T>& mapper);
59
72 template<typename T>
73 class ObjectMapper: public anch::json::GenericMapper<ObjectMapper<T>, T> {
75 template<typename U>
76 friend void anch::json::registerObject(ObjectMapper<U>&);
77
78 // Attributes +
79 private:
81 std::vector<std::function<bool((const T&, std::ostream&, const anch::json::MappingOptions&))>> _writers;
82
84 std::map<std::string, anch::json::DeserializeFn<T>> _readers;
85 // Attributes -
86
87 // Constructors +
88 private:
92 ObjectMapper();
93
94 public:
98 ObjectMapper(const ObjectMapper& other) = delete;
99
103 ObjectMapper(ObjectMapper&& other) = delete;
104 // Constructors -
105
106 private:
107 // Destructors +
111 virtual ~ObjectMapper();
112 // Destructors -
113
114 // Methods +
115 private:
126 template<typename P>
127 ObjectMapper<T>& registerField(const std::string& key, P T::* value);
128
140 template<typename MT, typename P>
141 ObjectMapper<T>& registerField(const std::string& key, P T::* value);
142
154 template<typename P, typename MT = P>
155 ObjectMapper<T>& registerField(const std::string& key, std::function<P(const T&)> getter);
156
168 template<typename P, typename MT = P>
169 ObjectMapper<T>& registerField(const std::string& key, std::function<void(T&, const P&)> setter);
170
183 template<typename P, typename MT = P>
184 ObjectMapper<T>& registerField(const std::string& key, std::function<P(const T&)> getter, std::function<void(T&, const P&)> setter);
185
186 public:
193
204 bool serialize(const T& value,
205 std::ostream& out,
206 const anch::json::MappingOptions& options,
207 const std::optional<std::string>& field = EMPTY_FIELD);
208
219 bool serialize(const T* const value,
220 std::ostream& out,
221 const anch::json::MappingOptions& options,
222 const std::optional<std::string>& field = EMPTY_FIELD);
223
234 bool serialize(const std::optional<T>& value,
235 std::ostream& out,
236 const anch::json::MappingOptions& options,
237 const std::optional<std::string>& field = EMPTY_FIELD);
238
249 bool serialize(const std::vector<T>& value,
250 std::ostream& out,
251 const anch::json::MappingOptions& options,
252 const std::optional<std::string>& field = EMPTY_FIELD);
253
264 bool serialize(const std::list<T>& value,
265 std::ostream& out,
266 const anch::json::MappingOptions& options,
267 const std::optional<std::string>& field = EMPTY_FIELD);
268
279 bool serialize(const std::set<T>& value,
280 std::ostream& out,
281 const anch::json::MappingOptions& options,
282 const std::optional<std::string>& field = EMPTY_FIELD);
283
294 bool serialize(const std::map<std::string,T>& value,
295 std::ostream& out,
296 const anch::json::MappingOptions& options,
297 const std::optional<std::string>& field = EMPTY_FIELD);
298
307 bool deserialize(T& value, anch::json::ReaderContext& context) const;
308
309 private:
317 void serializeValue(const T& value, std::ostream& out, const anch::json::MappingOptions& options);
318 // Methods -
319
320 };
321
322} // anch::json
323
324#include "json/impl/mapper.hpp"
JSON mapper factory.
Definition factory.hpp:52
Generic mapper.
Definition genericMapper.hpp:45
JSON complex object mapper.
Definition mapper.hpp:73
bool serialize(const std::optional< T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool serialize(const T &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
ObjectMapper(ObjectMapper &&other)=delete
bool serialize(const std::list< T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool serialize(const T *const value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool serialize(const std::map< std::string, T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool serialize(const std::set< T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
bool deserialize(T &value, anch::json::ReaderContext &context) const
bool serialize(const std::vector< T > &value, std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field=EMPTY_FIELD)
ObjectMapper(const ObjectMapper &other)=delete
JSON reader context.
Definition readerContext.hpp:42
JSON namespace.
Definition constants.hpp:25
const std::optional< std::string > EMPTY_FIELD
void registerObject(anch::json::ObjectMapper< T > &mapper)
JSON mapping options.
Definition mappingOptions.hpp:35