AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
format.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2026 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 <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <istream>
23#include <ostream>
24#include <string>
25#include <string_view>
26#include <type_traits>
27
28#include "json/json.hpp"
29#include "json/mappingOptions.hpp"
30#include "mapping.hpp"
31
32namespace anch::json {
33
47 template<typename T>
49 || std::is_enum_v<anch::json::detail::MapperType<T>>
50 || anch::Described<anch::json::detail::MapperType<T>>;
51
62 struct JsonFormat {
63
66
68 static constexpr std::string_view MEDIA_TYPE = "application/json";
69
75 static const Options& defaultOptions() noexcept;
76
86 template<anch::json::Mappable T>
87 static void serialize(const T& value, std::ostream& out, const Options& options);
88
98 template<anch::json::Mappable T>
99 static void deserialize(T& value, std::istream& input, const Options& options);
100
101 };
102
103}
104
105namespace anch::json {
106
107 inline const JsonFormat::Options&
111
112 template<anch::json::Mappable T>
113 void
114 JsonFormat::serialize(const T& value, std::ostream& out, const Options& options) {
115 if constexpr (std::is_same_v<T, std::string>) {
116 // std::string would match anch-json container overload (C<T> with T = char)
117 anch::json::serialize<std::string>(value, out, options);
118 } else {
119 anch::json::serialize(value, out, options);
120 }
121 }
122
123 template<anch::json::Mappable T>
124 void
125 JsonFormat::deserialize(T& value, std::istream& input, const Options& options) {
126 if constexpr (std::is_same_v<T, std::string>) {
127 anch::json::deserialize<std::string>(value, input, options);
128 } else {
129 anch::json::deserialize(value, input, options);
130 }
131 }
132
133}
Type described by an anch::Mapping specialization.
Definition mapping.hpp:66
Type which the JSON format maps.
Definition format.hpp:48
JSON namespace.
Definition constants.hpp:27
constexpr bool isPrimitive()
Check if type is a primitive JSON type.
void deserialize(T &value, std::istream &input, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS)
void serialize(const T &value, std::ostream &out, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS)
MappingOptions DEFAULT_MAPPING_OPTIONS
AnCH framework base namespace.
Definition app.hpp:30
JSON format: serialization and deserialization of values with options.
Definition format.hpp:62
static void deserialize(T &value, std::istream &input, const Options &options)
Definition format.hpp:125
static constexpr std::string_view MEDIA_TYPE
Definition format.hpp:68
static const Options & defaultOptions() noexcept
Definition format.hpp:108
static void serialize(const T &value, std::ostream &out, const Options &options)
Definition format.hpp:114
anch::json::MappingOptions Options
Definition format.hpp:65
JSON mapping options.
Definition mappingOptions.hpp:38