nfx-serialization 0.9.3
Cross-platform C++ JSON serialization library with extensible trait capabilities
Loading...
Searching...
No Matches
Serializer.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
32
33#pragma once
34
35#include "Concepts.h"
37
38#include <nfx/json/Document.h>
39#include <nfx/json/Builder.h>
40
41namespace nfx::serialization::json
42{
43 //=====================================================================
44 // Serializer class
45 //=====================================================================
46
53 template <typename T>
54 class Serializer final
55 {
56 template <typename U>
57 friend struct SerializationTraits;
58
59 public:
60 //----------------------------------------------
61 // Serialization options and context
62 //----------------------------------------------
63
67 struct Options
68 {
69 bool includeNullFields = false;
70 bool prettyPrint = false;
72 bool escapeNonAscii = false;
73
77 Options() = default;
78
84 template <typename U>
85 inline void copyFrom( const typename Serializer<U>::Options& other );
86
93 template <typename U>
94 inline static Options createFrom( const typename Serializer<U>::Options& other );
95 };
96
97 //----------------------------------------------
98 // Type aliases
99 //----------------------------------------------
100
102 using value_type = T;
103
104 //----------------------------------------------
105 // Construction
106 //----------------------------------------------
107
111 Serializer() = default;
112
117 inline explicit Serializer( const Options& options ) noexcept;
118
119 //----------------------------------------------
120 // Options management
121 //----------------------------------------------
122
127 inline const Options& options() const noexcept;
128
133 inline void setOptions( const Options& options ) noexcept;
134
135 //----------------------------------------------
136 // Static convenience serialization methods
137 //----------------------------------------------
138
146 inline static std::string toString( const T& obj, const Options& options = {} );
147
155 inline static T fromString( std::string_view jsonStr, const Options& options = {} );
156
157 private:
158 //----------------------------------------------
159 // Private methods
160 //----------------------------------------------
161
168 template <typename U>
169 inline void serializeValue( const U& obj, nfx::json::Builder& builder ) const;
170
177 template <typename U>
178 inline void deserializeValue( const nfx::json::Document& doc, U& obj ) const;
179
180 //----------------------------------------------
181 // Member variables
182 //----------------------------------------------
183
184 Options m_options{};
185 };
186} // namespace nfx::serialization::json
187
188#include "nfx/detail/serialization/json/Serializer.inl"
Unified serialization traits for JSON serialization/deserialization.
Type traits for JSON serialization.
Serializer(const Options &options) noexcept
Constructor with options.
static T fromString(std::string_view jsonStr, const Options &options={})
Deserialize object from JSON string.
static std::string toString(const T &obj, const Options &options={})
Serialize object to JSON string.
const Options & options() const noexcept
Get current serialization options.
T value_type
The type being serialized/deserialized.
Definition Serializer.h:102
Serializer()=default
Default constructor.
void setOptions(const Options &options) noexcept
Set serialization options.
Serialization options and context.
Definition Serializer.h:68
Options()=default
Default constructor.
bool prettyPrint
Format output with indentation.
Definition Serializer.h:70
bool escapeNonAscii
Escape non-ASCII characters (> 127) as \uXXXX.
Definition Serializer.h:72
void copyFrom(const typename Serializer< U >::Options &other)
Copy values from another serializer's options.
bool includeNullFields
Include fields with null values in output.
Definition Serializer.h:69
static Options createFrom(const typename Serializer< U >::Options &other)
Create Options with values copied from another serializer's options.
bool validateOnDeserialize
Validate data during deserialization.
Definition Serializer.h:71