nfx-serialization 0.3.0
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 "Document.h"
36#include "SerializationTraits.h"
37
38namespace nfx::serialization::json
39{
40 //=====================================================================
41 // Serializer class
42 //=====================================================================
43
50 template <typename T>
51 class Serializer final
52 {
53 public:
54 //----------------------------------------------
55 // Serialization options and context
56 //----------------------------------------------
57
61 struct Options
62 {
63 bool includeNullFields = false;
64 bool prettyPrint = false;
66
70 Options() = default;
71
77 template <typename U>
78 inline void copyFrom( const typename Serializer<U>::Options& other );
79
86 template <typename U>
87 inline static Options createFrom( const typename Serializer<U>::Options& other );
88 };
89
90 //----------------------------------------------
91 // Type aliases
92 //----------------------------------------------
93
95 using value_type = T;
96
97 //----------------------------------------------
98 // Construction
99 //----------------------------------------------
100
104 Serializer() = default;
105
110 inline explicit Serializer( const Options& options ) noexcept;
111
112 //----------------------------------------------
113 // Options management
114 //----------------------------------------------
115
120 inline const Options& options() const noexcept;
121
126 inline void setOptions( const Options& options ) noexcept;
127
128 //----------------------------------------------
129 // Static convenience serialization methods
130 //----------------------------------------------
131
139 inline static std::string toString( const T& obj, const Options& options = {} );
140
148 inline static T fromString( std::string_view jsonStr, const Options& options = {} );
149
150 //----------------------------------------------
151 // Instance serialization methods
152 //----------------------------------------------
153
160 inline Document serialize( const T& obj ) const;
161
168 inline T deserialize( const Document& doc ) const;
169
170 private:
171 //----------------------------------------------
172 // Private methods
173 //----------------------------------------------
174
181 template <typename U>
182 inline void serializeValue( const U& obj, Document& doc ) const;
183
190 template <typename U>
191 inline void deserializeValue( const Document& doc, U& obj ) const;
192
193 //----------------------------------------------
194 // Member variables
195 //----------------------------------------------
196
197 Options m_options{};
198 };
199} // namespace nfx::serialization::json
200
201#include "nfx/detail/serialization/json/Serializer.inl"
Generic document abstraction for JSON serialization.
Serialization traits and type specializations for JSON serialization.
Generic JSON document abstraction for serialization.
Definition Document.h:59
Document serialize(const T &obj) const
Serialize object to JSON document.
Serializer(const Options &options) noexcept
Constructor with options.
T deserialize(const Document &doc) const
Deserialize object from JSON document.
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:95
Serializer()=default
Default constructor.
void setOptions(const Options &options) noexcept
Set serialization options.
Serialization options and context.
Definition Serializer.h:62
Options()=default
Default constructor.
bool prettyPrint
Format output with indentation (2 spaces per level).
Definition Serializer.h:64
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:63
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:65