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