BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdljsn_writeoptions.h
Go to the documentation of this file.
1/// @file bdljsn_writeoptions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdljsn_writeoptions.h -*-C++-*-
8#ifndef INCLUDED_BDLJSN_WRITEOPTIONS
9#define INCLUDED_BDLJSN_WRITEOPTIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdljsn_writeoptions bdljsn_writeoptions
15/// @brief Provide options for writing a JSON document.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdljsn
19/// @{
20/// @addtogroup bdljsn_writeoptions
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdljsn_writeoptions-purpose"> Purpose</a>
25/// * <a href="#bdljsn_writeoptions-classes"> Classes </a>
26/// * <a href="#bdljsn_writeoptions-description"> Description </a>
27/// * <a href="#bdljsn_writeoptions-attributes"> Attributes </a>
28/// * <a href="#bdljsn_writeoptions-usage"> Usage </a>
29/// * <a href="#bdljsn_writeoptions-example-1-creating-and-populating-an-options-object"> Example 1: Creating and Populating an Options Object </a>
30///
31/// # Purpose {#bdljsn_writeoptions-purpose}
32/// Provide options for writing a JSON document.
33///
34/// # Classes {#bdljsn_writeoptions-classes}
35///
36/// - bdljsn::WriteOptions: options for writing a JSON document
37///
38/// @see bdljsn_jsonutil, bdljsn_json
39///
40/// # Description {#bdljsn_writeoptions-description}
41/// This component provides a single, simply constrained
42/// (value-semantic) attribute class, `bdljsn::WriteOptions`, that is used to
43/// specify options for writing a JSON document (see @ref bdljsn_jsonutil ).
44///
45/// ## Attributes {#bdljsn_writeoptions-attributes}
46///
47///
48/// @code
49/// Name Type Default Simple Constraints
50/// ------------------ ----------- ------- ------------------
51/// escapeForwardSlash bool true none
52/// initialIndentLevel int 0 >= 0
53/// sortMembers bool false none
54/// spacesPerLevel int 4 >= 0
55/// style WriteStyle e_COMPACT none
56/// @endcode
57/// * `escapeForwardSlash`: determines whether any `/` characters are output
58/// escaped (as `\/`) or not (as `/`) in names or strings.
59/// * `initialIndentLevel`: initial indent level for the top-most element. If
60/// `style` is `e_COMPACT`, or `spacesPerLevel` is 0, this option is ignored.
61/// * `sortMembers`: indicates whether the members of a object will be sorted
62/// in lexicographical order based on the member name.
63/// * `spacesPerLevel`: spaces per indent level. If this option is 0, no
64/// indentation is used. If `style` is `e_COMPACT` or `e_ONELINE`, this
65/// option is ignored.
66/// * `style`: the style used to encode the JSON data.
67///
68/// ## Usage {#bdljsn_writeoptions-usage}
69///
70///
71/// This section illustrates intended use of this component.
72///
73/// ### Example 1: Creating and Populating an Options Object {#bdljsn_writeoptions-example-1-creating-and-populating-an-options-object}
74///
75///
76/// This component is designed to be used at a higher level to set the options
77/// for writing `bdljsn::Json` objects in JSON format. This example shows how
78/// to create and populate an options object.
79///
80/// First, we default-construct a `bdljsn::WriteOptions` object:
81/// @code
82/// const int INITIAL_INDENT_LEVEL = 1;
83/// const int SPACES_PER_LEVEL = 4;
84///
85/// bdljsn::WriteOptions options;
86/// assert(true == options.escapeForwardSlash());
87/// assert(0 == options.initialIndentLevel());
88/// assert(4 == options.spacesPerLevel());
89/// assert(false == options.sortMembers());
90/// assert(bdljsn::WriteStyle::e_COMPACT == options.style());
91/// @endcode
92/// Finally, we populate that object using a pre-defined initial indent level
93/// and spaces per level:
94/// @code
95/// options.setInitialIndentLevel(INITIAL_INDENT_LEVEL);
96/// assert(INITIAL_INDENT_LEVEL == options.initialIndentLevel());
97///
98/// options.setSpacesPerLevel(SPACES_PER_LEVEL);
99/// assert(SPACES_PER_LEVEL == options.spacesPerLevel());
100/// @endcode
101/// @}
102/** @} */
103/** @} */
104
105/** @addtogroup bdl
106 * @{
107 */
108/** @addtogroup bdljsn
109 * @{
110 */
111/** @addtogroup bdljsn_writeoptions
112 * @{
113 */
114
115#include <bdlscm_version.h>
116
117#include <bdljsn_writestyle.h>
118
119#include <bsl_iosfwd.h>
120
121#include <bsls_assert.h>
122
123
124
125namespace bdljsn {
126
127 // ==================
128 // class WriteOptions
129 // ==================
130
131/// This simply constrained (value-semantic) attribute class specifies options
132/// for writing a JSON document. See the @ref bdljsn_writeoptions-attributes for information on the class attributes.
133///
134/// \note Note that the class invariants are identically the
135/// constraints on the individual attributes.
136///
137/// See @ref bdljsn_writeoptions
139
140 // INSTANCE DATA
141
142 // initial indentation level for the topmost element
143 int d_initialIndentLevel;
144
145 // whether to sort members of an object by member name
146 bool d_sortMembers;
147
148 // whether `/` characters should be escaped in output
149 bool d_escapeForwardSlash;
150
151 // spaces per additional level of indentation
152 int d_spacesPerLevel;
153
154 // write style used for formatting JSON text
156
157 public:
158 // CONSTANTS
160
162
164
166
167 static const bdljsn::WriteStyle::Enum
169
170 public:
171 // CREATORS
172
173 /// Create an object of type `WriteOptions` having the (default)
174 /// attribute values:
175 /// @code
176 /// escapeForwardSlash() == true
177 /// initialIndentLevel() == 0
178 /// sortMembers() == false
179 /// spacesPerLevel() == 4
180 /// style() == e_COMPACT
181 /// @endcode
183
184 /// Create an object of type `WriteOptions` having the value of the
185 /// specified `original` object.
186 WriteOptions(const WriteOptions& original);
187
188 /// Destroy this object.
190
191 // MANIPULATORS
192
193 /// Assign to this object the value of the specified `rhs` object and
194 /// return a non-`const` reference to this object.
196
197 /// Reset this object to the default value (i.e., its value upon default
198 /// construction) and return a non-`const` reference to this object.
200
201 /// Set the `escapeForwardSlash` attribute of this object to the specified
202 /// `value` and return a non-`const` reference to this object.
204
205 /// Set the `initialIndentLevel` attribute of this object to the specified
206 /// `value` and return a non-`const` reference to this object.
207 ///
208 /// \pre The behavior is undefined unless `0 <= value`.
210
211 /// Set the `sortMembers` attribute of this object to the specified `value`
212 /// and return a non-`const` reference to this object.
213 WriteOptions& setSortMembers(bool value);
214
215 /// Set the `spacesPerLevel` attribute of this object to the specified
216 /// `value` and return a non-`const` reference to this object.
217 ///
218 /// \pre The behavior is undefined unless `0 <= value`.
220
221 /// Set the `style` attribute of this object to the specified `value` and
222 /// return a non-`const` reference to this object.
224
225 // ACCESSORS
226
227 /// Return the `escapeForwardSlash` attribute of this object.
228 bool escapeForwardSlash() const;
229
230 /// Return the `initialIndentLevel` attribute of this object.
231 int initialIndentLevel() const;
232
233 /// Return the `sortMembers` attribute of this object.
234 bool sortMembers() const;
235
236 /// Return the `spacesPerLevel` attribute of this object.
237 int spacesPerLevel() const;
238
239 /// Return the `style` attribute of this object.
241
242 // Aspects
243
244 /// Format this object to the specified output `stream` at the
245 /// optionally specified indentation `level` and return a reference to
246 /// the modifiable `stream`. If `level` is specified, optionally
247 /// specify `spacesPerLevel`, the number of spaces per indentation level
248 /// for this and all of its nested objects. Each line is indented by
249 /// the absolute value of `level * spacesPerLevel`. If `level` is
250 /// negative, suppress indentation of the first line. If
251 /// `spacesPerLevel` is negative, suppress line breaks and format the
252 /// entire output on one line. If `stream` is initially invalid, this operation has no effect.
253 ///
254 /// \note Note that a trailing newline is provided
255 /// in multiline mode only. Also note that the format is not fully
256 /// specified, and can change without notice.
257 bsl::ostream& print(bsl::ostream& stream,
258 int level = 0,
259 int spacesPerLevel = 4) const;
260};
261
262// FREE OPERATORS
263
264/// Return `true` if the specified `lhs` and `rhs` attribute objects have the
265/// same value, and `false` otherwise. Two attribute objects have the same
266/// value if each respective attribute has the same value.
267inline
268bool operator==(const WriteOptions& lhs, const WriteOptions& rhs);
269
270/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
271/// have the same value, and `false` otherwise. Two attribute objects do not
272/// have the same value if one or more respective attributes differ in values.
273inline
274bool operator!=(const WriteOptions& lhs, const WriteOptions& rhs);
275
276/// Format the specified `rhs` to the specified output `stream` and return a
277/// non-`const` reference `stream`.
278inline
279bsl::ostream& operator<<(bsl::ostream& stream, const WriteOptions& rhs);
280
281// ============================================================================
282// INLINE DEFINITIONS
283// ============================================================================
284
285 // ------------------
286 // class WriteOptions
287 // ------------------
288
289// CREATORS
290inline
292: d_initialIndentLevel(original.d_initialIndentLevel)
293, d_sortMembers (original.d_sortMembers)
294, d_escapeForwardSlash(original.d_escapeForwardSlash)
295, d_spacesPerLevel (original.d_spacesPerLevel)
296, d_style (original.d_style)
297{
298}
299
300inline
302{
303 BSLS_ASSERT(0 <= d_initialIndentLevel);
304 BSLS_ASSERT(0 <= d_spacesPerLevel);
305}
306
307// MANIPULATORS
308inline
310{
311 d_initialIndentLevel = rhs.d_initialIndentLevel;
312 d_sortMembers = rhs.d_sortMembers;
313 d_escapeForwardSlash = rhs.d_escapeForwardSlash;
314 d_spacesPerLevel = rhs.d_spacesPerLevel;
315 d_style = rhs.d_style;
316
317 return *this;
318}
319
320inline
322{
323 d_escapeForwardSlash = value;
324 return *this;
325}
326
327inline
329{
330 BSLS_ASSERT(0 <= value);
331
332 d_initialIndentLevel = value;
333 return *this;
334}
335
336inline
338{
339 d_sortMembers = value;
340 return *this;
341}
342
343inline
345{
346 BSLS_ASSERT(0 <= value);
347
348 d_spacesPerLevel = value;
349 return *this;
350}
351
352inline
354{
355 d_style = value;
356 return *this;
357}
358
359// ACCESSORS
360inline
362{
363 return d_escapeForwardSlash;
364}
365
366inline
368{
369 return d_initialIndentLevel;
370}
371
372inline
374{
375 return d_sortMembers;
376}
377
378inline
380{
381 return d_spacesPerLevel;
382}
383
384inline
386{
387 return d_style;
388}
389
390} // close package namespace
391
392// FREE OPERATORS
393inline
395 const bdljsn::WriteOptions& rhs)
396{
397 return lhs.initialIndentLevel() == rhs.initialIndentLevel()
398 && lhs.sortMembers() == rhs.sortMembers()
399 && lhs.escapeForwardSlash() == rhs.escapeForwardSlash()
400 && lhs.spacesPerLevel() == rhs.spacesPerLevel()
401 && lhs.style() == rhs.style();
402}
403
404inline
406 const bdljsn::WriteOptions& rhs)
407{
408 return lhs.initialIndentLevel() != rhs.initialIndentLevel()
409 || lhs.sortMembers() != rhs.sortMembers()
410 || lhs.escapeForwardSlash() != rhs.escapeForwardSlash()
411 || lhs.spacesPerLevel() != rhs.spacesPerLevel()
412 || lhs.style() != rhs.style();
413}
414
415inline
416bsl::ostream& bdljsn::operator<<(bsl::ostream& stream,
417 const bdljsn::WriteOptions& rhs)
418{
419 return rhs.print(stream, 0, -1);
420}
421
422
423
424#endif
425
426// ----------------------------------------------------------------------------
427// Copyright 2022 Bloomberg Finance L.P.
428//
429// Licensed under the Apache License, Version 2.0 (the "License");
430// you may not use this file except in compliance with the License.
431// You may obtain a copy of the License at
432//
433// http://www.apache.org/licenses/LICENSE-2.0
434//
435// Unless required by applicable law or agreed to in writing, software
436// distributed under the License is distributed on an "AS IS" BASIS,
437// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
438// See the License for the specific language governing permissions and
439// limitations under the License.
440// ----------------------------- END-OF-FILE ----------------------------------
441
442/** @} */
443/** @} */
444/** @} */
Definition bdljsn_writeoptions.h:138
WriteOptions & setSortMembers(bool value)
Definition bdljsn_writeoptions.h:337
WriteOptions & setSpacesPerLevel(int value)
Definition bdljsn_writeoptions.h:344
int initialIndentLevel() const
Return the initialIndentLevel attribute of this object.
Definition bdljsn_writeoptions.h:367
bool escapeForwardSlash() const
Return the escapeForwardSlash attribute of this object.
Definition bdljsn_writeoptions.h:361
static const int s_DEFAULT_INITIALIZER_INITIAL_INDENT_LEVEL
Definition bdljsn_writeoptions.h:159
static const int s_DEFAULT_INITIALIZER_SPACES_PER_LEVEL
Definition bdljsn_writeoptions.h:165
WriteOptions & reset()
static const bool s_DEFAULT_INITIALIZER_ESCAPE_FORWARD_SLASH
Definition bdljsn_writeoptions.h:163
WriteOptions & setInitialIndentLevel(int value)
Definition bdljsn_writeoptions.h:328
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
bool sortMembers() const
Return the sortMembers attribute of this object.
Definition bdljsn_writeoptions.h:373
~WriteOptions()
Destroy this object.
Definition bdljsn_writeoptions.h:301
int spacesPerLevel() const
Return the spacesPerLevel attribute of this object.
Definition bdljsn_writeoptions.h:379
WriteOptions & setStyle(bdljsn::WriteStyle::Enum value)
Definition bdljsn_writeoptions.h:353
WriteOptions & setEscapeForwardSlash(bool value)
Definition bdljsn_writeoptions.h:321
bdljsn::WriteStyle::Enum style() const
Return the style attribute of this object.
Definition bdljsn_writeoptions.h:385
static const bool s_DEFAULT_INITIALIZER_SORT_MEMBERS
Definition bdljsn_writeoptions.h:161
WriteOptions & operator=(const WriteOptions &rhs)
Definition bdljsn_writeoptions.h:309
static const bdljsn::WriteStyle::Enum s_DEFAULT_INITIALIZER_STYLE
Definition bdljsn_writeoptions.h:168
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdljsn_error.h:142
bool operator!=(const Error &lhs, const Error &rhs)
bool operator==(const Error &lhs, const Error &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const Error &object)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Enum
Definition bdljsn_writestyle.h:129