BDE 4.39.x Production Release
Loading...
Searching...
No Matches
baljsn_convertfromjsonoptions.h
Go to the documentation of this file.
1/// @file baljsn_convertfromjsonoptions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// baljsn_convertfromjsonoptions.h -*-C++-*-
8#ifndef INCLUDED_BALJSN_CONVERTFROMJSONOPTIONS
9#define INCLUDED_BALJSN_CONVERTFROMJSONOPTIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT_RCSID(baljsn_convertfromjsonoptions_h, "$Id$ $CSID$")
14
15/// @defgroup baljsn_convertfromjsonoptions baljsn_convertfromjsonoptions
16/// @brief Provide an attribute class for `JsonConverter` "from" options.
17/// @addtogroup bal
18/// @{
19/// @addtogroup baljsn
20/// @{
21/// @addtogroup baljsn_convertfromjsonoptions
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#baljsn_convertfromjsonoptions-purpose"> Purpose</a>
26/// * <a href="#baljsn_convertfromjsonoptions-classes"> Classes </a>
27/// * <a href="#baljsn_convertfromjsonoptions-description"> Description </a>
28/// * <a href="#baljsn_convertfromjsonoptions-attributes"> Attributes </a>
29/// * <a href="#baljsn_convertfromjsonoptions-usage"> Usage </a>
30/// * <a href="#baljsn_convertfromjsonoptions-example-1-creating-and-populating-an-options-object"> Example 1: Creating and Populating an Options Object </a>
31///
32/// # Purpose {#baljsn_convertfromjsonoptions-purpose}
33/// Provide an attribute class for `JsonConverter` "from" options.
34///
35/// # Classes {#baljsn_convertfromjsonoptions-classes}
36///
37/// - baljsn::ConvertFromJsonOptions: options for `bdljsn::Json` to `bdlat`
38///
39/// # Description {#baljsn_convertfromjsonoptions-description}
40/// This component provides a single, simply constrained
41/// (value-semantic) attribute class, `baljsn::ConvertFromJsonOptions`, that is
42/// used to specify options for converting `bdljsn::Json` objects to
43/// `bdlat`-compliant objects.
44///
45/// ## Attributes {#baljsn_convertfromjsonoptions-attributes}
46///
47///
48/// @code
49/// Name Type Default Simple Constraints
50/// ---------------------- ------- ---------- ----------------------
51/// maxDepth int 512 >= 0
52/// skipUnknownElements bool true none
53/// @endcode
54/// * `maxDepth`: maximum allowed depth of the `Json` source object. If
55/// exceeded, the conversion fails.
56///
57/// * `skipUnknownElements`: flag specifying if elements of the `Json` source
58/// object that are unknown to the `bdlat` object should be quietly skipped.
59/// If `false` any unknown element fails the conversion.
60///
61/// The default values of `maxDepth` and `skipUnknownElements` are the *same* as
62/// the default values of the identically named `baljsn::DecoderOptions`
63/// options.
64///
65/// ## Usage {#baljsn_convertfromjsonoptions-usage}
66///
67///
68/// This section illustrates intended use of this component.
69///
70/// ### Example 1: Creating and Populating an Options Object {#baljsn_convertfromjsonoptions-example-1-creating-and-populating-an-options-object}
71///
72///
73/// This component is designed to be used at a higher level to set the options
74/// for JSON conversion of `bdlat` objects. This example shows how to create
75/// and populate an options object.
76///
77/// First, we default-construct a `baljsn::ConvertFromJsonOptions` object:
78/// @code
79/// baljsn::ConvertFromJsonOptions options;
80/// assert(512 == options.maxDepth());
81/// assert(true == options.skipUnknownElements());
82/// @endcode
83/// Next, we populate that object with custom values:
84/// @code
85/// options.setMaxDepth(10);
86/// assert(10 == options.maxDepth());
87///
88/// options.setSkipUnknownElements(false);
89/// assert(false == options.skipUnknownElements());
90/// @endcode
91/// @}
92/** @} */
93/** @} */
94
95/** @addtogroup bal
96 * @{
97 */
98/** @addtogroup baljsn
99 * @{
100 */
101/** @addtogroup baljsn_convertfromjsonoptions
102 * @{
103 */
104
105#include <bslh_hash.h>
106
107#include <bsls_assert.h>
108
109#include <bsl_iosfwd.h>
110
111
112namespace baljsn {
113
114 // ============================
115 // class ConvertFromJsonOptions
116 // ============================
117
119 // This simply constrained (value-semantic) attribute class specifies
120 // options for converting `bdljsn::Json` objects to `bdlat`-compliant
121 // objects. See the @ref baljsn_convertfromjsonoptions-attributes section for description of the
122 // options.
123
124 // CLASS DATA
125 static const int DEFAULT_INITIALIZER_MAX_DEPTH;
126
127 // DATA
128 int d_maxDepth; // maximum recursion depth
129
130 bool d_skipUnknownElements; // option to skip unknown elements
131
132 public:
133 // CREATORS
134
135 /// Create an object of type `ConvertFromJsonOptions` having the default
136 /// value.
138
139 /// Create a `ConvertFromJsonOptions` object having the value of the
140 /// specified `original` object.
142 /// = default;
143
144 /// Destroy this object.
145 ~ConvertFromJsonOptions() = default;
146
147 // MANIPULATORS
148
149 /// Assign to this object the value of the specified `rhs`.
151 /// = default;
152
153 /// Reset this object to the default value (i.e., its value upon default
154 /// construction).
155 void reset();
156
157 /// Set the `maxDepth` attribute of this object to the specified `value`.
158 ///
159 /// \pre The behavior is undefined unless `0 <= value`.
160 void setMaxDepth(int value);
161
162 /// Set the `skipUnknownElements` attribute of this object to the specified
163 /// `value`.
164 void setSkipUnknownElements(bool value);
165
166 // ACCESSORS
167
168 /// Format this object to the specified output `stream` at the optionally
169 /// specified indentation `level` and return a reference to the modifiable
170 /// `stream`. If `level` is specified, optionally specify
171 /// `spacesPerLevel`, the number of spaces per indentation level for this
172 /// and all of its nested objects. Each line is indented by the absolute
173 /// value of `level * spacesPerLevel`. If `level` is negative, suppress
174 /// indentation of the first line. If `spacesPerLevel` is negative,
175 /// suppress line breaks and format the entire output on one line. If
176 /// `stream` is initially invalid, this operation has no effect.
177 ///
178 /// \note Note that a trailing newline is provided in multiline mode only.
179 bsl::ostream& print(bsl::ostream& stream,
180 int level = 0,
181 int spacesPerLevel = 4) const;
182
183 /// Return the value of the `maxDepth` attribute of this object.
184 int maxDepth() const;
185
186 /// Return the value of the `skipUnknownElements` attribute of this object.
187 bool skipUnknownElements() const;
188
189 // HIDDEN FRIENDS
190
191 /// Return `true` if the specified `lhs` and `rhs` attribute objects have
192 /// the same value, and `false` otherwise. Two attribute objects have the
193 /// same value if each respective attribute has the same value.
194 friend bool operator==(const ConvertFromJsonOptions& lhs,
195 const ConvertFromJsonOptions& rhs)
196 {
197 return lhs.maxDepth() == rhs.maxDepth()
198 && lhs.skipUnknownElements() == rhs.skipUnknownElements();
199 }
200
201 /// Return `true` if the specified `lhs` and `rhs` attribute objects do
202 /// not have the same value, and `false` otherwise. Two attribute objects
203 /// do not have the same value if any respective attribute do not have the
204 /// same value.
205 friend bool operator!=(const ConvertFromJsonOptions& lhs,
206 const ConvertFromJsonOptions& rhs)
207 {
208 return lhs.maxDepth() != rhs.maxDepth()
209 || lhs.skipUnknownElements() != rhs.skipUnknownElements();
210 }
211
212 /// Format the specified `rhs` to the specified output `stream` and return
213 /// a reference to the modifiable `stream`.
214 friend bsl::ostream& operator<<(bsl::ostream& stream,
215 const ConvertFromJsonOptions& rhs)
216 {
217 return rhs.print(stream, 0, -1);
218 }
219
220 /// Pass the specified `object` to the specified `hashAlg`. This function
221 /// integrates with the `bslh` modular hashing system and effectively
222 /// provides a `bsl::hash` specialization for `ConvertFromJsonOptions`.
223 template <typename t_HASH_ALGORITHM>
224 friend void hashAppend(t_HASH_ALGORITHM& hashAlg,
225 const ConvertFromJsonOptions& object)
226 {
227 using bslh::hashAppend;
228 hashAppend(hashAlg, object.maxDepth());
229 hashAppend(hashAlg, object.skipUnknownElements());
230 }
231};
232
233} // close package namespace
234
235// ============================================================================
236// INLINE DEFINITIONS
237// ============================================================================
238
239namespace baljsn {
240
241 // ----------------------------
242 // class ConvertFromJsonOptions
243 // ----------------------------
244// CREATORS
245
246inline
248: d_maxDepth(DEFAULT_INITIALIZER_MAX_DEPTH)
249, d_skipUnknownElements(true)
250{
251}
252
253// MANIPULATORS
254
255inline
256void ConvertFromJsonOptions::reset()
257{
258 d_maxDepth = DEFAULT_INITIALIZER_MAX_DEPTH;
259 d_skipUnknownElements = true;
260}
261
262inline
264{
265 BSLS_ASSERT(0 <= value);
266
267 d_maxDepth = value;
268}
269
270inline
272{
273 d_skipUnknownElements = value;
274}
275
276// ACCESSORS
277inline
279{
280 return d_maxDepth;
281}
282
283inline
285{
286 return d_skipUnknownElements;
287}
288
289} // close package namespace
290
291#endif
292
293// ----------------------------------------------------------------------------
294// Copyright 2026 Bloomberg Finance L.P.
295//
296// Licensed under the Apache License, Version 2.0 (the "License");
297// you may not use this file except in compliance with the License.
298// You may obtain a copy of the License at
299//
300// http://www.apache.org/licenses/LICENSE-2.0
301//
302// Unless required by applicable law or agreed to in writing, software
303// distributed under the License is distributed on an "AS IS" BASIS,
304// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
305// See the License for the specific language governing permissions and
306// limitations under the License.
307// ----------------------------- END-OF-FILE ----------------------------------
308
309/** @} */
310/** @} */
311/** @} */
Definition baljsn_convertfromjsonoptions.h:118
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
friend void hashAppend(t_HASH_ALGORITHM &hashAlg, const ConvertFromJsonOptions &object)
Definition baljsn_convertfromjsonoptions.h:224
ConvertFromJsonOptions(const ConvertFromJsonOptions &original) ~ConvertFromJsonOptions()=default
= default;
ConvertFromJsonOptions()
Definition baljsn_convertfromjsonoptions.h:247
ConvertFromJsonOptions & operator=(const ConvertFromJsonOptions &rhs) void reset()
Assign to this object the value of the specified rhs.
int maxDepth() const
Return the value of the maxDepth attribute of this object.
Definition baljsn_convertfromjsonoptions.h:278
void setSkipUnknownElements(bool value)
Definition baljsn_convertfromjsonoptions.h:271
void setMaxDepth(int value)
Definition baljsn_convertfromjsonoptions.h:263
friend bool operator!=(const ConvertFromJsonOptions &lhs, const ConvertFromJsonOptions &rhs)
Definition baljsn_convertfromjsonoptions.h:205
friend bsl::ostream & operator<<(bsl::ostream &stream, const ConvertFromJsonOptions &rhs)
Definition baljsn_convertfromjsonoptions.h:214
bool skipUnknownElements() const
Return the value of the skipUnknownElements attribute of this object.
Definition baljsn_convertfromjsonoptions.h:284
friend bool operator==(const ConvertFromJsonOptions &lhs, const ConvertFromJsonOptions &rhs)
Definition baljsn_convertfromjsonoptions.h:194
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT_RCSID(tag, str)
BSLS_IDENT_RCSID() - insert ident str (specific to platform/compiler)
Definition bsls_ident.h:244
#define BSLS_IDENT_PRAGMA_ONCE
BSLS_IDENT_PRAGMA_ONCE - macro to avoid multiple inclusion
Definition bsls_ident.h:263
Definition baljsn_convertfromjsonoptions.h:112
bsl::enable_if<(bsl::is_integral< TYPE >::value||bsl::is_pointer< TYPE >::value||bsl::is_enum< TYPE >::value)&&!bsl::is_same< TYPE, bool >::value >::type hashAppend(HASH_ALGORITHM &hashAlg, TYPE input)
Definition bslh_hash.h:643