BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdljsn_writestyle.h
Go to the documentation of this file.
1/// @file bdljsn_writestyle.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdljsn_writestyle.h -*-C++-*-
8#ifndef INCLUDED_BDLJSN_WRITESTYLE
9#define INCLUDED_BDLJSN_WRITESTYLE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdljsn_writestyle bdljsn_writestyle
15/// @brief Enumerate the formatting styles for a writing a JSON document.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdljsn
19/// @{
20/// @addtogroup bdljsn_writestyle
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdljsn_writestyle-purpose"> Purpose</a>
25/// * <a href="#bdljsn_writestyle-classes"> Classes </a>
26/// * <a href="#bdljsn_writestyle-description"> Description </a>
27/// * <a href="#bdljsn_writestyle-enumerators"> Enumerators </a>
28/// * <a href="#bdljsn_writestyle-usage"> Usage </a>
29/// * <a href="#bdljsn_writestyle-example-1-basic-syntax"> Example 1: Basic Syntax </a>
30///
31/// # Purpose {#bdljsn_writestyle-purpose}
32/// Enumerate the formatting styles for a writing a JSON document.
33///
34/// # Classes {#bdljsn_writestyle-classes}
35///
36/// - bdljsn::WriteStyle: namespace for styles for writing a JSON document
37///
38/// @see bdljsn_writeoptions
39///
40/// # Description {#bdljsn_writestyle-description}
41/// This component provides `bdljsn::WriteStyle`, a namespace for
42/// the `enum` type `bdljsn::WriteStyle::Enum`, which enumerates the set of
43/// format styles that can be used when writing a JSON document.
44///
45/// ## Enumerators {#bdljsn_writestyle-enumerators}
46///
47///
48/// @code
49/// Name Description
50/// ------------- -------------------------------------------------------
51/// e_PRETTY A human friendly format with configurable new lines and
52/// indentation
53///
54/// e_ONELINE A single-line format with a space after each comma and
55/// colon.
56///
57/// e_COMPACT A maximally compact format with no white space.
58/// @endcode
59///
60/// ## Usage {#bdljsn_writestyle-usage}
61///
62///
63/// This section illustrates intended use of this component.
64///
65/// ### Example 1: Basic Syntax {#bdljsn_writestyle-example-1-basic-syntax}
66///
67///
68/// The following snippets of code provide a simple illustration of using
69/// `bdljsn::WriteStyle`.
70///
71/// First, we create a variable `value` of type `bdljsn::WriteStyle::Enum` and
72/// initialize it with the enumerator value `bdljsn::WriteStyle::e_PRETTY`:
73/// @code
74/// bdljsn::WriteStyle::Enum value = bdljsn::WriteStyle::e_PRETTY;
75/// @endcode
76/// Now, we store the address of its ASCII representation in a pointer variable,
77/// `asciiValue`, of type `const char *`:
78/// @code
79/// const char *asciiValue = bdljsn::WriteStyle::toAscii(value);
80/// assert(0 == bsl::strcmp(asciiValue, "PRETTY"));
81/// @endcode
82/// Finally, we print `value` to `bsl::cout`.
83/// @code
84/// bsl::cout << value << bsl::endl;
85/// @endcode
86/// This statement produces the following output on `stdout`:
87/// @code
88/// PRETTY
89/// @endcode
90/// @}
91/** @} */
92/** @} */
93
94/** @addtogroup bdl
95 * @{
96 */
97/** @addtogroup bdljsn
98 * @{
99 */
100/** @addtogroup bdljsn_writestyle
101 * @{
102 */
103
104#include <bdlscm_version.h>
105
106#include <bsl_iosfwd.h>
107
108
109namespace bdljsn {
110 // =================
111 // struct WriteStyle
112 // =================
113
114/// This `struct` provides a namespace for enumerating the formatting styles
115/// that can be used for writing a JSON document. See `Enum` in the TYPES
116/// sub-section for details.
117///
118/// This class:
119/// * supports a complete set of *enumeration* operations
120/// - except for `bdex` serialization
121/// * is `const` *thread-safe*
122/// For terminology see @ref bsldoc_glossary .
123///
124/// See @ref bdljsn_writestyle
126
127 public:
128 // TYPES
129 enum Enum {
130 e_PRETTY, // human friendly, with indentation and spaces
131 e_ONELINE, // single-line format with whitespace for readability
132 e_COMPACT // maximally compact, no white-space
133 };
134
135 public:
136 // CLASS METHODS
137
138 /// Write the string representation of the specified enumeration `value`
139 /// to the specified output `stream`, and return a reference to
140 /// `stream`. Optionally specify an initial indentation `level`, whose
141 /// absolute value is incremented recursively for nested objects. If
142 /// `level` is specified, optionally specify `spacesPerLevel`, whose
143 /// absolute value indicates the number of spaces per indentation level
144 /// for this and all of its nested objects. If `level` is negative,
145 /// suppress indentation of the first line. If `spacesPerLevel` is
146 /// negative, format the entire output on one line, suppressing all but
147 /// the initial indentation (as governed by `level`). See `toAscii` for
148 /// what constitutes the string representation of a `WriteStyle::Enum`
149 /// value.
150 static bsl::ostream& print(bsl::ostream& stream,
151 WriteStyle::Enum value,
152 int level = 0,
153 int spacesPerLevel = 4);
154
155 /// Return the non-modifiable string representation corresponding to the
156 /// specified enumeration `value`, if it exists, and a unique (error)
157 /// string otherwise. The string representation of `value` matches its
158 /// corresponding enumerator name with the "e_" prefix elided. For
159 /// example:
160 /// @code
161 /// bsl::cout << WriteStyle::toAscii(WriteStyle::e_PRETTY);
162 /// @endcode
163 /// will print the following on standard output:
164 /// @code
165 /// PRETTY
166 /// @endcode
167 ///
168 /// \note Note that specifying a `value` that does not match any of the
169 /// enumerators will result in a string representation that is distinct
170 /// from any of those corresponding to the enumerators, but is otherwise
171 /// unspecified.
172 static const char *toAscii(WriteStyle::Enum value);
173};
174
175// FREE OPERATORS
176
177/// Write the string representation of the specified enumeration `value` to
178/// the specified output `stream` in a single-line format, and return a
179/// reference to `stream`. See `toAscii` for what constitutes the string representation of a `bdljsn::WriteStyle::Enum` value.
180///
181/// \note Note that this
182/// method has the same behavior as
183/// @code
184/// bdljsn::WriteStyle::print(stream, value, 0, -1);
185/// @endcode
186bsl::ostream& operator<<(bsl::ostream& stream, WriteStyle::Enum value);
187
188} // close package namespace
189
190// ============================================================================
191// INLINE DEFINITIONS
192// ============================================================================
193
194 // -----------------
195 // struct WriteStyle
196 // -----------------
197
198// FREE OPERATORS
199inline
200bsl::ostream& bdljsn::operator<<(bsl::ostream& stream, WriteStyle::Enum value)
201{
202 return WriteStyle::print(stream, value, 0, -1);
203}
204
205
206
207#endif
208
209// ----------------------------------------------------------------------------
210// Copyright 2022 Bloomberg Finance L.P.
211//
212// Licensed under the Apache License, Version 2.0 (the "License");
213// you may not use this file except in compliance with the License.
214// You may obtain a copy of the License at
215//
216// http://www.apache.org/licenses/LICENSE-2.0
217//
218// Unless required by applicable law or agreed to in writing, software
219// distributed under the License is distributed on an "AS IS" BASIS,
220// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
221// See the License for the specific language governing permissions and
222// limitations under the License.
223// ----------------------------- END-OF-FILE ----------------------------------
224
225/** @} */
226/** @} */
227/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdljsn_error.h:142
bsl::ostream & operator<<(bsl::ostream &stream, const Error &object)
Definition bdljsn_writestyle.h:125
static bsl::ostream & print(bsl::ostream &stream, WriteStyle::Enum value, int level=0, int spacesPerLevel=4)
static const char * toAscii(WriteStyle::Enum value)
Enum
Definition bdljsn_writestyle.h:129
@ e_ONELINE
Definition bdljsn_writestyle.h:131
@ e_PRETTY
Definition bdljsn_writestyle.h:130
@ e_COMPACT
Definition bdljsn_writestyle.h:132