BDE 4.14.0 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 .
124
125 public:
126 // TYPES
127 enum Enum {
128 e_PRETTY, // human friendly, with indentation and spaces
129 e_ONELINE, // single-line format with whitespace for readability
130 e_COMPACT // maximally compact, no white-space
131 };
132
133 public:
134 // CLASS METHODS
135
136 /// Write the string representation of the specified enumeration `value`
137 /// to the specified output `stream`, and return a reference to
138 /// `stream`. Optionally specify an initial indentation `level`, whose
139 /// absolute value is incremented recursively for nested objects. If
140 /// `level` is specified, optionally specify `spacesPerLevel`, whose
141 /// absolute value indicates the number of spaces per indentation level
142 /// for this and all of its nested objects. If `level` is negative,
143 /// suppress indentation of the first line. If `spacesPerLevel` is
144 /// negative, format the entire output on one line, suppressing all but
145 /// the initial indentation (as governed by `level`). See `toAscii` for
146 /// what constitutes the string representation of a `WriteStyle::Enum`
147 /// value.
148 static bsl::ostream& print(bsl::ostream& stream,
149 WriteStyle::Enum value,
150 int level = 0,
151 int spacesPerLevel = 4);
152
153 /// Return the non-modifiable string representation corresponding to the
154 /// specified enumeration `value`, if it exists, and a unique (error)
155 /// string otherwise. The string representation of `value` matches its
156 /// corresponding enumerator name with the "e_" prefix elided. For
157 /// example:
158 /// @code
159 /// bsl::cout << WriteStyle::toAscii(WriteStyle::e_PRETTY);
160 /// @endcode
161 /// will print the following on standard output:
162 /// @code
163 /// PRETTY
164 /// @endcode
165 /// Note that specifying a `value` that does not match any of the
166 /// enumerators will result in a string representation that is distinct
167 /// from any of those corresponding to the enumerators, but is otherwise
168 /// unspecified.
169 static const char *toAscii(WriteStyle::Enum value);
170};
171
172// FREE OPERATORS
173
174/// Write the string representation of the specified enumeration `value` to
175/// the specified output `stream` in a single-line format, and return a
176/// reference to `stream`. See `toAscii` for what constitutes the string
177/// representation of a `bdljsn::WriteStyle::Enum` value. Note that this
178/// method has the same behavior as
179/// @code
180/// bdljsn::WriteStyle::print(stream, value, 0, -1);
181/// @endcode
182bsl::ostream& operator<<(bsl::ostream& stream, WriteStyle::Enum value);
183
184} // close package namespace
185
186// ============================================================================
187// INLINE DEFINITIONS
188// ============================================================================
189
190 // -----------------
191 // struct WriteStyle
192 // -----------------
193
194// FREE OPERATORS
195inline
196bsl::ostream& bdljsn::operator<<(bsl::ostream& stream, WriteStyle::Enum value)
197{
198 return WriteStyle::print(stream, value, 0, -1);
199}
200
201
202
203#endif
204
205// ----------------------------------------------------------------------------
206// Copyright 2022 Bloomberg Finance L.P.
207//
208// Licensed under the Apache License, Version 2.0 (the "License");
209// you may not use this file except in compliance with the License.
210// You may obtain a copy of the License at
211//
212// http://www.apache.org/licenses/LICENSE-2.0
213//
214// Unless required by applicable law or agreed to in writing, software
215// distributed under the License is distributed on an "AS IS" BASIS,
216// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
217// See the License for the specific language governing permissions and
218// limitations under the License.
219// ----------------------------- END-OF-FILE ----------------------------------
220
221/** @} */
222/** @} */
223/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdljsn_error.h:143
bsl::ostream & operator<<(bsl::ostream &stream, const Error &object)
Definition bdljsn_writestyle.h:123
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:127
@ e_ONELINE
Definition bdljsn_writestyle.h:129
@ e_PRETTY
Definition bdljsn_writestyle.h:128
@ e_COMPACT
Definition bdljsn_writestyle.h:130